## Reklamlar Hakkında - Duyuru

Ilginiz icin teşekkürler

# Quicksort

Quicksort is a fast sorting algorithm, which is used not only for educational purposes, but widely applied in practice. On the average, it has O(n log n) complexity, making quicksort suitable for sorting big data volumes. The idea of the algorithm is quite simple and once you realize it, you can write quicksort as fast as bubble sort.

## Algorithm

The divide-and-conquer strategy is used in quicksort. Below the recursion step is described:
1. Choose a pivot value. We take the value of the middle element as pivot value, but it can be any value, which is in range of sorted values, even if it doesn't present in the array.
2. Partition. Rearrange elements in such a way, that all elements which are lesser than the pivot go to the left part of the array and all elements greater than the pivot, go to the right part of the array. Values equal to the pivot can stay in any part of the array. Notice, that array may be divided in non-equal parts.
3. Sort both parts. Apply quicksort algorithm recursively to the left and the right parts.

### Partition algorithm in detail

There are two indices i and j and at the very beginning of the partition algorithm i points to the first element in the array and j points to the last one. Then algorithm moves i forward, until an element with value greater or equal to the pivot is found. Index j is moved backward, until an element with value lesser or equal to the pivot is found. If i ≤ j then they are swapped and i steps to the next position (i + 1), j steps to the previous one (j - 1). Algorithm stops, when i becomes greater than j.
After partition, all values before i-th element are less or equal than the pivot and all values after j-th element are greater or equal to the pivot.
Example. Sort {1, 12, 5, 26, 7, 14, 3, 7, 2} using quicksort.
Notice, that we show here only the first recursion step, in order not to make example too long. But, in fact, {1, 2, 5, 7, 3} and {14, 7, 26, 12} are sorted then recursively.

## Why does it work?

On the partition step algorithm divides the array into two parts and every element a from the left part is less or equal than every element b from the right part. Also a and b satisfy a ≤ pivot ≤ b inequality. After completion of the recursion calls both of the parts become sorted and, taking into account arguments stated above, the whole array is sorted.

## Complexity analysis

On the average quicksort has O(n log n) complexity, but strong proof of this fact is not trivial and not presented here. Still, you can find the proof in . In worst case, quicksort runs O(n2) time, but on the most "practical" data it works just fine and outperforms other O(n log n) sorting algorithms.

## Code snippets

Partition algorithm is important per se, therefore it may be carried out as a separate function. The code for C++ contains solid function for quicksort, but Java code contains two separate functions for partition and sort, accordingly.

### Java

int partition(int arr[], int left, int right)
{
int i = left, j = right;
int tmp;
int pivot = arr[(left + right) / 2];
while (i <= j) {
while (arr[i] < pivot)
i++;
while (arr[j] > pivot)
j--;
if (i <= j) {
tmp = arr[i];
arr[i] = arr[j];
arr[j] = tmp;
i++;
j--;
}
};
return i;
}
void quickSort(int arr[], int left, int right) {
int index = partition(arr, left, right);
if (left < index - 1)
quickSort(arr, left, index - 1);
if (index < right)
quickSort(arr, index, right);
}

### C++

void quickSort(int arr[], int left, int right) {
int i = left, j = right;
int tmp;
int pivot = arr[(left + right) / 2];
/* partition */
while (i <= j) {
while (arr[i] < pivot)
i++;
while (arr[j] > pivot)
j--;
if (i <= j) {
tmp = arr[i];
arr[i] = arr[j];
arr[j] = tmp;
i++;
j--;
}
};
/* recursion */
if (left < j)
quickSort(arr, left, j);
if (i < right)
quickSort(arr, i, right);
}

#### 0 yorum:

Yorumlar Hakkında:

Yorum yapmaktan çekinmeyin, Yorumlar hem benim için teşvik, sizin içinde kendinizi ifade edebileceğiniz bir ortam! İyi veya kötü her türlü yoruma açığım. Ha bu arada unutmadan, yaptığınız yorumda bana cevap vermemi beklediğiniz birşey yazdıysanız, lütfen verdiğim cevabı okumak için 1-2 içinde blogumu tekrar kontrol edin.

## Etiketgiller

.NET (1) .nomedia (1) 8 mart nedir (1) açamıyorum (1) araba (6) bowling skor (1) bozuluyor (1) Call of Duty (1) clash royale (1) cs 1.6 (1) çalışmıyor (2) çalıştırma (1) çözüldü (1) çözüm (4) Çözümü (7) ders notu (1) deyimler (2) dll (2) dll dosyası (1) dll hatası (2) DNS (1) dns ayarları (3) DNS listesi (1) dowland (1) e-devlet (1) En Hızlı DNS (1) ets2 (3) FIFA 18 (1) FM 2018 (1) Fortnite (1) giremiyorum (1) Google (8) göremiyorum (1) görünmesin (1) gösterimi (1) göstermek (1) göstermiyor (2) gözükmesin (1) GTA 5 (2) güncelleme (2) haberi (1) haberleri (3) hack (2) hata veriyor (1) hatası (3) hatchback (1) ie7 (1) ie8 (1) ilginç (1) ilk kim (1) inceleme (5) indir (8) indirim kodu (1) instagram (3) ios (6) ios 11 (1) istanbul (3) javascript (1) kadın (1) kaldırır mı (1) kapandı (1) kız arkadaş (1) kız arkadaşı (1) kız tavlama (1) kilitleniyor (1) kimdir (10) korkunç (1) kumanda (1) kurucusu kim (1) kurulumu (2) library (1) Masterchef (1) Mazda 3 2018 (1) mp3 (4) MSVCR100.DLL (1) Mustafa (1) nasıl (23) ne (7) ne demek (11) ne işe yarar (10) ne kadar (1) ne zaman (11) neden (4) nfs (5) nfs the run (1) niçin (1) niye (8) oyun (27) oyun hilesi (1) örneği (1) örnek proje (1) özellikleri (2) pes 2011 (1) philips tv (1) php (4) porna (1) programı (3) pugb (3) QR Kod Nedir (1) resimleri (1) samsung (8) seditio (6) Serel Yereli (1) Sevgili (2) sevgilim (1) sorun (3) sorun çözümü (24) sorunları (1) sorunu (7) sosyal medya (3) sözleri (4) steam (9) steam hatası (4) strike nedir (1) şarj sorunu (1) teknoloji (2) telefon (16) Toyota Auris (2) troubleshooter (21) truckersMP (1) türkçe (3) türkçe yama (2) update (2) video (2) videoları (2) web dizayn (1) web tasarım (1) win 10 (1) win10 (3) win7 (4) windows (6) windows 10 (6) windows 7 (5) zaman (1)