Docsity

Quicksort Algorithm: Implementation and Analysis, Study notes of Design and Analysis of Algorithms

Partial preview of the text

Download Quicksort Algorithm: Implementation and Analysis and more Study notes Design and Analysis of Algorithms in PDF only on Docsity!

Quicksort

Quicksort Algorithm

Pick Pivot Element

There are a number of ways to pick the pivot element. In thisexample, we will use the first element in the array:

Partitioning Array

Given a pivot, partition the elements of the array

such that the resulting array consists of:1.^ One sub-array that contains elements >= pivot2.^ Another sub-array that contains elements < pivot

The sub-arrays are stored in the original data array.Partitioning loops through, swapping elements

below/above pivot.

[0]^

[1]^

[2]^

[3]^

[4]^

[5]^

[6]^

[7]^

too_big_index

too_small_index

1.^

While data[too_big_index] <= data[pivot] ++too_big_index

[0]^

[1]^

[2]^

[3]^

[4]^

[5]^

[6]^

[7]^

too_big_index

too_small_index

1.^

While data[too_big_index] <= data[pivot] ++too_big_index

[0]^

[1]^

[2]^

[3]^

[4]^

[5]^

[6]^

[7]^

too_big_index

too_small_index

1.^

While data[too_big_index] <= data[pivot] ++too_big_index

2.^ While data[too_small_index] > data[pivot] --too_small_index

[0]^

[1]^

[2]^

[3]^

[4]^

[5]^

[6]^

[7]^

too_big_index

too_small_index

1.^

While data[too_big_index] <= data[pivot] ++too_big_index

2.^ While data[too_small_index] > data[pivot] --too_small_index

[0]^

[1]^

[2]^

[3]^

[4]^

[5]^

[6]^

[7]^

too_big_index

too_small_index

1.^

While data[too_big_index] <= data[pivot] ++too_big_index

2.^ While data[too_small_index] > data[pivot] --too_small_index 3.^ If too_big_index < too_small_index swap data[too_big_index] and data[too_small_index]

[0]^

[1]^

[2]^

[3]^

[4]^

[5]^

[6]^

[7]^

too_big_index

too_small_index

1.^

While data[too_big_index] <= data[pivot] ++too_big_index

2.^ While data[too_small_index] > data[pivot] --too_small_index 3.^ If too_big_index < too_small_index swap data[too_big_index] and data[too_small_index] 4.^ While too_small_index > too_big_index, go to 1.

[0]^

[1]^

[2]^

[3]^

[4]^

[5]^

[6]^

[7]^

too_big_index

too_small_index

1.^

While data[too_big_index] <= data[pivot] ++too_big_index

2.^ While data[too_small_index] > data[pivot] --too_small_index 3.^ If too_big_index < too_small_index swap data[too_big_index] and data[too_small_index] 4.^ While too_small_index > too_big_index, go to 1.

[0]^

[1]^

[2]^

[3]^

[4]^

[5]^

[6]^

[7]^

too_big_index

too_small_index

1.^

While data[too_big_index] <= data[pivot] ++too_big_index

2.^ While data[too_small_index] > data[pivot] --too_small_index 3.^ If too_big_index < too_small_index swap data[too_big_index] and data[too_small_index] 4.^ While too_small_index > too_big_index, go to 1.

[0]^

[1]^

[2]^

[3]^

[4]^

[5]^

[6]^

[7]^

too_big_index

too_small_index

1.^

While data[too_big_index] <= data[pivot] ++too_big_index

2.^ While data[too_small_index] > data[pivot] --too_small_index 3.^ If too_big_index < too_small_index swap data[too_big_index] and data[too_small_index] 4.^ While too_small_index > too_big_index, go to 1.

1.^

While data[too_big_index] <= data[pivot] ++too_big_index

2.^ While data[too_small_index] > data[pivot] --too_small_index 3.^ If too_big_index < too_small_index swap data[too_big_index] and data[too_small_index] 4.^ While too_small_index > too_big_index, go to 1.

[0]^

[1]^

[2]^

[3]^

[4]^

[5]^

[6]^

[7]^

too_big_index

too_small_index