none
Write a quicksort for a list of 3 letter codes like; CEH, 2BC, HHX

    Question

  • Hi

    I want to sort a list I have of 3 letter abbreviations like, CEH, 2BC, HHX and so on. I understand quicksort from an array of integers point of view but I am unsure how to do it for a list. Here is what I was doing for integers;

    class quickSort
        {
     
            private int[] array = new int[20];
            private int len;
     
            public void QuickSort()
            {
                sort(0, len - 1);
            }
     
            public void sort(int left, int right)
            {
                int pivot, leftend, rightend;
     
                leftend = left;
                rightend = right;
                pivot = array[left];
     
                while (left < right)
                {
                    while ((array[right] >= pivot) && (left < right))
                    {
                        right--;
                    }
     
                    if (left != right)
                    {
                        array[left] = array[right];
                        left++;
                    }
     
                    while ((array[left] <= pivot) && (left < right))
                    {
                        left++;
                    }
     
                    if (left != right)
                    {
                        array[right] = array[left];
                        right--;
                    }
                }
     
                array[left] = pivot;
                pivot = left;
                left = leftend;
                right = rightend;
     
                if (left < pivot)
                {
                    sort(left, pivot - 1);
                }
     
                if (right > pivot)
                {
                    sort(pivot + 1, right);
                }
            }
     
            public static void Main()
            {
                quickSort q_Sort = new quickSort();
     
                int[] array = { 4, 3, 1, 4, 6, 7, 5, 4, 32, 5, 26, 187, 8 };
                q_Sort.array = array;
                q_Sort.len = q_Sort.array.Length;
                q_Sort.QuickSort();
     
                for (int j = 0; j < q_Sort.len; j++)
                {
                    Console.WriteLine(q_Sort.array[j]);
                }
                Console.ReadKey();
            }
        }
    }

    How can I write a quick sort to sort a list of string that look like this;

      List<string> Track= new List<string>();
                Tracks.Add("CEH");
                Tracks.Add("FBM");
                Tracks.Add("CEH");
                Tracks.Add("IVM");
                Tracks.Add("2BN");
                Tracks.Add("XXX");
                Tracks.Add("ZZZ");
                Tracks.Add("JJJ");
                Tracks.Add("HGT");
                Tracks.Add("CEH");
                Tracks.Add("HGT");
                Tracks.Add("CEH");
                Tracks.Add("AA1");
                Tracks.Add("2CD");

    Many Thanks



    Tuesday, April 18, 2017 9:33 AM

Answers

  • String supports IComparable too so your code will work properly. Of course the strings will be compared case sensitive but otherwise should work correctly.  Just replace the int types with string.

    Note also that if this isn't a homework assignment then there really is no reason to implement it. Sorting is already built into .NET. You don't need to build your own version.

    Michael Taylor
    http://www.michaeltaylorp3.net

    Tuesday, April 18, 2017 1:49 PM
    Moderator

All replies

  • String supports IComparable too so your code will work properly. Of course the strings will be compared case sensitive but otherwise should work correctly.  Just replace the int types with string.

    Note also that if this isn't a homework assignment then there really is no reason to implement it. Sorting is already built into .NET. You don't need to build your own version.

    Michael Taylor
    http://www.michaeltaylorp3.net

    Tuesday, April 18, 2017 1:49 PM
    Moderator
  • Thank you

    It is just to learn writing quicksort for understanding purposes. I know there are lots of built-in solutions but I don't learn a great deal from them.




    Tuesday, April 18, 2017 2:49 PM