// 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 printMas(ElementType[] mas, int size) { for (int i = 0; i < size; i++) { for (int j = 0; j < size; j++) mas[j].Print(i); Console.WriteLine(); } Console.WriteLine(); }
static void Main(string[] args) { Console.WriteLine("Enter size array:"); int sizeArray = Convert.ToInt32(Console.ReadLine()); ElementType[] mas = new ElementType[sizeArray]; Random rand = new Random(DateTime.Now.Millisecond); for (int i = 0; i < sizeArray; i++) mas[i] = new ElementType(sizeArray, rand); Console.WriteLine("Not sort array:"); printMas(mas, sizeArray); QSort.Qsort(mas, 0, sizeArray - 1); Console.WriteLine("Sorted array to the first element of the column"); printMas(mas, sizeArray); }
public static void Swap(ref ElementType swapOne, ref ElementType swapTwo) { int[] swapSwap = swapOne.mas; swapOne.mas = swapTwo.mas; swapTwo.mas = swapSwap; }