### Quicksort: Divide and Conquer

- Find a pivot in the middle;
`4 5 [3] 1 2`

- Swap pivot with the last element;
`4 5 <2> 1 <3>`

- Set pivot index (pi) to the start index of array;
`pi = 0`

- Loop from the start to the end of array;
`i = 0, 1, 2, 3`

let pi = start index = 0

if element < pivot, swap element with pi, increment pi

a. at i = 0, 4 < 3 is false , so skip;`P{4} 5 2 1 3`

b. at i = 1, 5 < 3 is false, so skip;`P{4} 5 2 1 3`

c. at i = 2, 2 < 3 is true, swap arr[i] with arr[pi], pi++ ;`<2> P{5} <4> 1 3`

d. at i = 3, 1 < 3 is true, swap arr[i] with arr[pi], pi++ ;`2 <1> P{4} <5> 3`

- Swap pi with last element;
`2 1 <3> 5 <4>`

- Return pivot index (pi) = 2 with array sorted by first partition call;
`2 1 [3] 5 4`

- Anything smaller than pivot value 3 will be on the left side, and bigger on the right side
- Do Step 1-7 on the subset of array on left side of pivot and same for the right side
- Recursion
- Doned

1 | swap = (arr, i, j) => { |

### Console output:

1 | Original Array: [ 33, 19, 25, 63, 66, 66, 67, 34, 76, 56 ] |

# Related Questions

[aka relatives aka family aka never ending drama]

Dive into

**Quicksort's time complexity**where you discover the truth on*do I even math*.Just find out about

`Hoare`

and`Lomuto`

partition. What the.**Quicksort with different partitions**And of cos,

**Interview questions on quicksort**