// quick sort массива mas с begin элемента до end. public static void Qsort(ElementType[] mas, int begin, int end) { int general = 0; int i = 0; for (i = 0; i < end - begin && mas[begin + i].Value() == mas[begin].Value(); i++) ; if (mas[begin].Value() == mas[begin + i].Value()) return; else if (mas[begin].Value() < mas[begin + i].Value()) general = mas[begin + i].Value(); else general = mas[begin].Value(); int left = begin; int right = end; while (left < right) { while (mas[left].Value() < general) left++; while (mas[right].Value() > general || mas[right].Value() == general) right--; if (left >= right) break; ElementType.Swap(ref mas[left], ref mas[right]); left++; right--; } Qsort(mas, begin, right); Qsort(mas, left, end); }
static void Main(string[] args) { System.Console.WriteLine("Enter size of array:"); string input = Console.ReadLine(); int sizeArray = Convert.ToInt32(input); ElementType[] mas = new ElementType[sizeArray]; Random rand = new Random(DateTime.Now.Millisecond); for (int i = 0; i < sizeArray; i++) mas[i] = new ElementType(rand.Next(0,100)); Console.WriteLine("Not sort mas:"); printMas(mas, sizeArray); QSort.Qsort(mas, 0, sizeArray - 1); Console.WriteLine("Sort mas:"); printMas(mas, sizeArray); }
static void printMas(ElementType[] mas, int size) { for (int i = 0; i < size; i++) mas[i].Print(); Console.WriteLine(); }
public static void Swap(ref ElementType swapOne, ref ElementType swapTwo) { int swapSwap = swapOne.val; swapOne.val = swapTwo.val; swapTwo.val = swapSwap; }