Advertisement
APXOHT

Untitled

Jan 6th, 2013
950
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 1.61 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3.  
  4. class QuickSortAlgorithm
  5. {
  6.     static List<int> QuickSort(List<int> array)
  7.     {
  8.         if (array.Count <= 1)
  9.         {
  10.             return array;
  11.         }
  12.  
  13.         int pivot = array[array.Count / 2];
  14.         List<int> less = new List<int>();
  15.         List<int> greater = new List<int>();
  16.  
  17.         for (int i = 0; i < array.Count; i++)
  18.         {
  19.             if (i != (array.Count / 2))
  20.             {
  21.                 if (array[i] <= pivot)
  22.                 {
  23.                     less.Add(array[i]);
  24.                 }
  25.                 else
  26.                 {
  27.                     greater.Add(array[i]);
  28.                 }
  29.             }
  30.         }
  31.  
  32.         return ConcatenateArrays(QuickSort(less), pivot, QuickSort(greater));
  33.     }
  34.  
  35.     static List<int> ConcatenateArrays(List<int> less, int pivot, List<int> greater)
  36.     {
  37.         List<int> result = new List<int>();
  38.  
  39.         for (int i = 0; i < less.Count; i++)
  40.         {
  41.             result.Add(less[i]);
  42.         }
  43.  
  44.         result.Add(pivot);
  45.  
  46.         for (int i = 0; i < greater.Count; i++)
  47.         {
  48.             result.Add(greater[i]);
  49.         }
  50.  
  51.         return result;
  52.     }
  53.  
  54.     static void PrintList(List<int> list)
  55.     {
  56.         for (int i = 0; i < list.Count; i++)
  57.         {
  58.             Console.Write(list[i] + " ");
  59.         }
  60.         Console.WriteLine();
  61.     }
  62.  
  63.     static void Main()
  64.     {
  65.         List<int> arrayOfIntegers = new List<int>() { 1, 5, 7, 8, 9, 3, 5, 6, 7 };
  66.         List<int> sortedArray = QuickSort(arrayOfIntegers);
  67.  
  68.         PrintList(sortedArray);
  69.     }
  70. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement