Quick Sort (Tezroq saralash):
Massivdan bir element tanlaydi (pivot element) va massivni bu pivot element bo‘yicha ikki qisimga bo‘ladi.
Chap tomondagi qismini va o‘ng tomondagi qismini alohida saralaydi.
O‘nlik saralangan qismlarni biriktiradi.
Misol:
static void HoareSort(int[] array, int start, int end)
{
if (end == start) return;
var pivot = array[end];
var storeIndex = start;
for (int i = start; i <= end - 1; i++)
if (array[i] <= pivot)
{
var t = array[i];
array[i] = array[storeIndex];
array[storeIndex] = t;
storeIndex++;
}
var n = array[storeIndex];
array[storeIndex] = array[end];
array[end] = n;
if (storeIndex > start) HoareSort(array, start, storeIndex - 1);
if (storeIndex < end) HoareSort(array, storeIndex + 1, end);
}
static void HoareSort(int[] array)
{
HoareSort(array, 0, array.Length - 1);
}
static Random random = new Random();
public static void Main()
{
int [] array = {3,2,5,7,8,1,9 };
HoareSort(array);
foreach (var e in array)
Console.WriteLine(e);
Console.ReadKey();
}
Yana bir misol: class Program { static void Main(string[] args) { int[] i = {4,3,7,23,6,8,123,6,32 }; QuickSort(i,0,8); foreach(int w in i) { Console.WriteLine(w); } Console.ReadKey(); } private static int[] QuickSort(int[] a, int i, int j) { if (i < j) { int q = Partition(a, i, j); a = QuickSort(a, i, q); a = QuickSort(a, q + 1, j); } return a; } private static int Partition(int[] a, int p, int r) {