1. Algrithom

 ◾Given an array of values, pick a value as a pivot value

 ◾Check each value against the pivot value and

   - bring each value higher than the pivot value to the right of the pivot value: GreaterList

   - bring each value lower than or equal to the pivot to the left of the pivot value: LessList

 ◾ Recursively call the algorithm for the array left and the array right of the pivot (which is now in the right spot).

 ◾ Combine LessList + pivot + GreaterList.

 

2. Implement

  public static void TestSimpleQuickSort()
        {
            List<int> intArray = TestData.GetListFromString();
            List<int> result = new List<int>();
            TimeWatcher.StartWatchFunc(SimpleQuickSort, intArray, out result);
            //TimeWatcher.StartWatchDelegate(SimpleQuickSort, intArray, out result);
        }
        
        public static List<int> SimpleQuickSort(List<int> a)
        {
            Random r = new Random();
            List<int> less = new List<int>();
            List<int> greater = new List<int>();
            if (a.Count <= 1)
                return a;
            //int pos = r.Next(a.Count);
            int pos = a.Count/2;

            int pivot = a[pos];
            a.RemoveAt(pos);
            foreach (int x in a)
            {
                if (x <= pivot)
                {
                    less.Add(x);
                }
                else
                {
                    greater.Add(x);
                }
            }
            return Concat(SimpleQuickSort(less), pivot, SimpleQuickSort(greater));
        }

        private static List<int> Concat(List<int> less, int pivot, List<int> greater)
        {
            List<int> sorted = new List<int>(less);
            sorted.Add(pivot);
            foreach (int i in greater)
            {
                sorted.Add(i);
            }

            return sorted;
        }

       
View Code

相关文章:

  • 2021-10-17
  • 2021-11-19
  • 2021-07-11
猜你喜欢
  • 2021-08-28
  • 2021-10-11
  • 2021-12-28
  • 2021-10-23
  • 2021-06-27
  • 2021-11-08
相关资源
相似解决方案