Implementing quicksort algorithm

前端 未结 7 1419
走了就别回头了
走了就别回头了 2021-02-01 19:48

I found quicksort algorithm from this book

\"\"

This is the algorithm

QUICKSORT (A, p, r)
i         


        
7条回答
  •  没有蜡笔的小新
    2021-02-01 20:02

    A simple generic C# implementation of QuickSort, can use first or last value or any other intermediate value for pivot

    using System;
    
    namespace QuickSort
    {
        class Program
        {
            static void Main(string[] args)
            {
                int[] arInt = { 6, 4, 2, 8, 4, 5, 4, 5, 4, 5, 4, 8, 11, 1, 7, 4, 13, 5, 45, -1, 0, -7, 56, 10, 57, 56, 57, 56 };
                GenericQuickSort.QuickSort(arInt, 0, arInt.Length - 1);
    
                string[] arStr = { "Here", "Is", "A", "Cat", "Really", "Fast", "And", "Clever" };
                GenericQuickSort.QuickSort(arStr, 0, arStr.Length - 1); ;
    
                Console.WriteLine(String.Join(',', arInt));
                Console.WriteLine(String.Join(',', arStr));
    
                Console.ReadLine();
            }
    
        }
    
        class GenericQuickSort where T : IComparable
        {
    
            public static void QuickSort(T[] ar, int lBound, int uBound)
            {
                if (lBound < uBound)
                {
                    var loc = Partition(ar, lBound, uBound);
                    QuickSort(ar, lBound, loc - 1);
                    QuickSort(ar, loc + 1, uBound);
                }
            }
    
            private static int Partition(T[] ar, int lBound, int uBound)
            {
                var start = lBound;
                var end = uBound;
    
                var pivot = ar[uBound];
    
                // switch to first value as pivot
                // var pivot = ar[lBound];
    
                while (start < end)
                {
    
                    while (ar[start].CompareTo(pivot) < 0)
                    {
                        start++;
                    }
    
                    while (ar[end].CompareTo(pivot) > 0)
                    {
                        end--;
                    }
    
                    if (start < end)
                    {
                        if (ar[start].CompareTo(ar[end]) == 0)
                        {
                            start++;
                        }
                        else
                        {
                            swap(ar, start, end);
                        }
                    }
                }
    
                return end;
            }
    
            private static void swap(T[] ar, int i, int j)
            {
                var temp = ar[i];
                ar[i] = ar[j];
                ar[j] = temp;
            }
        }
    }
    

    Output:

    -7,-1,0,1,2,4,4,4,4,4,4,5,5,5,5,6,7,8,8,10,11,13,45,56,56,56,57,57

    A,And,Cat,Clever,Fast,Here,Is,Really

    One important thing to notice here is that this optimized and simple code properly handles duplicates. I tried several posted quick sort code. Those do not give correct result for this (integer array) input or just hangs, such as https://www.w3resource.com/csharp-exercises/searching-and-sorting-algorithm/searching-and-sorting-algorithm-exercise-9.php and http://www.softwareandfinance.com/CSharp/QuickSort_Iterative.html. Therefore, if author also wants to use the code which handles duplicates this would be a good reference.

提交回复
热议问题