• No results found

QuickSort

N/A
N/A
Protected

Academic year: 2020

Share "QuickSort"

Copied!
40
0
0

Loading.... (view fulltext now)

Full text

(1)
(2)

Quicksort Advantages

Good performance

Variation (Dual Pivot Quicksort) is typically

the fastest sorting algorithm

(3)

Quicksort Disadvantages

Unstable

(we will learn what a stable sort is later)

Poor worst case performance

(4)

Quicksort Code

(5)

Choose first element as pivot

Break list into two sub lists (<= pivot and > pivot)

2 8 5 1 3 4 6

(6)

8 5 1 3 4 6 Pivot = 2

Position 1 = 0, Value = 2 Position 2 = 6, Value = 6

Since 6 is not <= 2

Decrement position 2 Min position = 0

(7)

8 5 1 3 4 6 Pivot = 2

Position 1 = 0, Value = 2 Position 2 = 5, Value = 4

Since 4 is not <= 2

Decrement position 2 Min position = 0

(8)

8 5 1 3 4 6 Pivot = 2

Position 1 = 0, Value = 2 Position 2 = 3, Value = 3

Since 3 is not <= 2

Decrement position 2 Min position = 0

(9)

8 5 1 3 4 6 Pivot = 2

Position 1 = 0, Value = 2 Position 2 = 3, Value = 1

Since 1 <= 2

Move 1 into position 1 Min position = 0

(10)

1 8 5 3 4 6 Pivot = 2

Position 1 = 1, Value = 8

Position 2 = 3, Value = 1

Increment position 1 Switch swap criteria Switch to position 1 Min position = 0

(11)

1 8 5 3 4 6 Pivot = 2

Position 1 = 1, Value = 8

Position 2 = 3, Value = 1

Since 8 > 2

Move 8 into position 2 Min position = 0

(12)

1 5 8 3 4 6 Pivot = 2

Position 1 = 1, Value = 8 Position 2 = 2, Value = 5

Decrement position 2 Switch swap criteria Switch to position 2 Min position = 0

(13)

1 5 8 3 4 6 Pivot = 2

Position 1 = 1, Value = 8 Position 2 = 2, Value = 5

Min position = 0

Max position = 6 Swap if value <= pivot

Since 5 is not <= 2

(14)

1 2 5 8 3 4 6 Pivot = 2

Position 1 = 1, Value = 2 Position 2 = 2, Value = 2

Min position = 0

Max position = 6 Swap if value <= pivot

Since position 1 = position 2 Place pivot

(15)

Choose first element as pivot

Break list into two sub lists (<= pivot and > pivot)

2 8 5 1 3 4 6

<= 2 2

(16)

Pivot = 1

Position 1 = 0, Value = 1 Position 2 = 0, Value = 1

Min position = 0

Max position = 0 Swap if value <= pivot

1 2 5 8 3 4 6

Since position 1 = position 2 Place pivot

(17)

Choose first element as pivot

Break list into two sub lists (<= pivot and > pivot)

2 8 5 1 3 4 6

<= 2 2

1 2 5 8 3 4 6 > 2

(18)

Pivot = 5

Position 1 = 2, Value = 5 Position 2 = 6, Value = 6

Min position = 2

Max position = 6 Swap if value <= pivot

1 2 8 3 4 6

Since 6 is not <= 5

(19)

Pivot = 5

Position 1 = 2, Value = 5 Position 2 = 5, Value = 4

Min position = 2

Max position = 6 Swap if value <= pivot

1 2 8 3 4 6

Since 4 <= 5

(20)

Pivot = 5

Position 1 = 3, Value = 8

Position 2 = 5, Value = 4 Min position = 2

Max position = 6 Swap if value > pivot

1 2 4 8 3 6

(21)

Pivot = 5

Position 1 = 3, Value = 8

Position 2 = 5, Value = 4 Min position = 2

Max position = 6 Swap if value > pivot

1 2 4 8 3 6

Since 8 > 5

(22)

Pivot = 5

Position 1 = 3, Value = 8 Position 2 = 4, Value = 3

Min position = 2

Max position = 6 Swap if value <= pivot

1 2 4 3 8 6

(23)

Pivot = 5

Position 1 = 3, Value = 8 Position 2 = 4, Value = 3

Min position = 2

Max position = 6 Swap if value <= pivot

1 2 4 3 8 6

Since 3 <= 5

(24)

Pivot = 5

Position 1 = 4, Value = 3

Position 2 = 4, Value = 3 Min position = 2

Max position = 6 Swap if value > pivot

1 2 4 3 8 6

(25)

Pivot = 5

Position 1 = 4, Value = 5 Position 2 = 4, Value = 5

Min position = 2

Max position = 6 Swap if value > pivot

1 2 4 3 5 8 6

(26)

2 8 5 1 3 4 6 Choose first element as pivot

Break list into two sub lists (<= pivot and > pivot)

<= 2

> 5 2

1 2 5 8 3 4 6

<= 5 5 > 2

4 3 5 8 6

(27)

Pivot = 4

Position 1 = 2, Value = 4 Position 2 = 3, Value = 3

Min position = 2

Max position = 3 Swap if value <= pivot

1 2 3 5 8 6

Since 3 <= 4

(28)

Pivot = 4

Position 1 = 2, Value = 3

Position 2 = 3, Value = 3 Min position = 2

Max position = 3 Swap if value > pivot

1 2 3 5 8 6

(29)

Pivot = 4

Position 1 = 2, Value = 3 Position 2 = 3, Value = 3

Min position = 2

Max position = 3 Swap if value > pivot

1 2 3 4 5 8 6

(30)

2 8 5 1 3 4 6 Choose first element as pivot

Break list into two sub lists (<= pivot and > pivot)

<= 2

> 5 2

1 2 5 8 3 4 6

<= 5 5 > 2

4 3 5 8 6

(31)

Pivot = 3

Position 1 = 2, Value = 3 Position 2 = 2, Value = 3

Min position = 2

Max position = 2 Swap if value <= pivot

1 2 3 4 5 8 6

(32)

2 8 5 1 3 4 6 Choose first element as pivot

Break list into two sub lists (<= pivot and > pivot)

<= 2

> 5 2

1 2 5 8 3 4 6

<= 5 5 > 2

4 3 5 8 6

(33)

Pivot = 8

Position 1 = 5, Value = 8 Position 2 = 6, Value = 6

Min position = 5

Max position = 6 Swap if value <= pivot

1 2 4 3 5 6

Since 6 <= 8

(34)

Pivot = 8

Position 1 = 6, Value = 6

Position 2 = 6, Value = 6 Min position = 5

Max position = 6 Swap if value > pivot

1 2 4 3 5 6

(35)

Pivot = 8

Position 1 = 6, Value = 8 Position 2 = 6, Value = 8

Min position = 5

Max position = 6 Swap if value > pivot

1 2 4 3 5 6 8

(36)

2 8 5 1 3 4 6 Choose first element as pivot

Break list into two sub lists (<= pivot and > pivot)

<= 2

> 5 2

1 2 5 8 3 4 6

<= 5 5 > 2

4 3 5 8 6

(37)

Pivot = 6

Position 1 = 5, Value = 6 Position 2 = 5, Value = 6

Min position = 5

Max position = 5 Swap if value <= pivot

1 2 3 4 5 6 8

(38)

2 8 5 1 3 4 6 Choose first element as pivot

Break list into two sub lists (<= pivot and > pivot)

<= 2

> 5 2

1 2 5 8 3 4 6

<= 5 5 > 2

4 3 5 8 6

(39)

Done!

(40)

Quicksort

Video

References

Related documents

Like in Oracle’s library implementation we incorporate the use of Insertionsort on small subproblems and show that it indeed speeds up Yaroslavskiy’s Quicksort in terms of

To optimize the algorithm, for an array smaller than 7, the pivot is chosen as the middle key or sort with the standard Quicksort, for mid-sized arrays( for an

Comparision study is also made with the OpenMP parallel quicksort implemented using C and the results shows that JOMP gives better result with respect to time complexity than OpenMP

After quicksort is done, sort entire array with insertion sort. } It’s efficient on

To achieve this we implement a tool to do both sequential and parallel quicksort on randomly generated arrays of different size in several runs to provide us with enough data

algorithm-- RCVO-NSGA-IIRandom Cross Variation Operator - nondominated sorting genetic algorithm II which based on randomized crossover operator and mutation operator for

General (non-string) comparison-based sorting algorithms are not optimal for sorting strings because of an imbalance between effort and result in a string comparison: it can take a

2 Choose pivot to be the element with lowest priority in subproblem.... Assign every element in the array a random priority (say in