Quick Sort
Begin with an array arr.
- Pick a pivot index.
Often at the end of the array.or find using a median of three algorithm - Declare two indices
jandi; and a temporary variabletempjis at the start of the arrayiis one less than the beginning of the array
- Do while
j < pivot:- If
arr[j] < arr[pivot]:i++- swap
arr[i]andarr[j]using temp variable- assign
temp = arr[i] - assign
arr[i] = arr[j] - assign
arr[j] = temp
- assign
j++
- If
- Now
j = i. Soi++. - Swap
arr[i]andarr[j]. Setpivot = i.- Now the
arr[pivot]is in the right spot. i.e. all items left of pivot are less than the pivot and all items greater than the pivot are greater than the pivot.
- Now the
- Finally, recursively return
quicksort(arr[:pivot - 1]) + [arr[pivot]] + quicksort(arr[pivot + 1:])