Top Banner
Quicksort Algorithm Input: A[1, …, n] Output: A[1, .., n], where A[1]<=A[2]…<=A[n] Quicksort: 1. if(n<=1) return; 2. Choose the pivot p = A[n] 3. Put all elements less than p on the left; put all elements lager than p on the right; put p at the middle. (Partition) 4. Quicksort(the array on the left of p) 5. Quicksort(the array on the right of p)
40

3.8 quick sort

Aug 14, 2015

Download

Education

Krish_ver2
Welcome message from author
This document is posted to help you gain knowledge. Please leave a comment to let me know what you think about it! Share it to your friends and learn new things together.
Transcript
Page 1: 3.8 quick sort

Quicksort Algorithm

• Input: A[1, …, n]• Output: A[1, .., n], where A[1]<=A[2]…<=A[n]• Quicksort:

1. if(n<=1) return;

2. Choose the pivot p = A[n]

3. Put all elements less than p on the left; put all elements lager than p on the right; put p at the middle. (Partition)

4. Quicksort(the array on the left of p)

5. Quicksort(the array on the right of p)

Page 2: 3.8 quick sort

Quicksort Algorithm Quicksort example

2 8 7 1 3 5 6 4

2 871 3 5 64

2 871 3 5 64

Current pivots

Previous pivots

Quicksort

Hi, I am nothing Nothing Jr.

Nothing 3rd

2 871 3 5 64

2 871 3 5 64

2 871 3 5 64

Page 3: 3.8 quick sort

Quicksort Algorithm

• More detail about partition• Input: A[1, …, n] (p=A[n])• Output: A[1,…k-1, k, k+1, … n], where A[1, …, k-1]<A[k] and

A[k+1, … n] > A[k], A[k]=p• Partition:

1. t = the tail of smaller array2. from i= 1 to n-1{

if(A[i]<p) {exchange A[t+1] with A[i];update t to the new tail;

}

3. exchange A[t+1] with A[n];

Page 4: 3.8 quick sort

Quicksort Algorithm Partition example

2 8 7 1 3 5 6 4

2 8 7 1 3 5 6 4

2 8 7 1 3 5 6 4

tail

2 8 7 1 3 5 6 4

tail

tail

tail

Exchange 2 with A[tail+1]

Do nothing

Do nothing

Page 5: 3.8 quick sort

Quicksort Algorithm Partition example

2 8 7 1 3 5 6 4

2 871 3 5 6 4

2 8 71 3 5 6 4

tail

tail

Exchange 1 with A[tail+1]

tail

Exchange 3 with A[tail+1]

Do nothing

2 8 71 3 5 6 4

tail

Do nothing

2 871 3 5 64 Final step: exchange A[n] with A[tail+1]

Page 6: 3.8 quick sort

Quicksort Algorithm

The final version of quick sort:

Quicksort(A, p, r){if(p<r){ //if(n<=1) return;

q = partition(A, p, r); //small ones on left;

//lager ones on right

Quicksort(A, p, q-1);

Quicksort(A, q+1, r);

}

}

Page 7: 3.8 quick sort

Quicksort AlgorithmInput : An array A containing n elements

Call QuickSort(A)

QuickSort(A)

if A contains one element

return A

else

select a pivot, p

divide A into A1, A2 such that A1 < p and A2 > p

R1 = QuickSort(A1)

R2 = QuickSort(A2)

return R1 p R2∪ ∪

Page 8: 3.8 quick sort

QuickSort Pseudo codeinput: An array A of n numbers

call QuickSort(1, n)

Quicksort(low,high)

i = low

j = high + 1

p = A [ i ]

While ( i < j )

i = i + 1

while A[ i ] < p

i = i + 1

j = j - 1

while A[ j ] > p

j = j - 1

if ( i < j ) swap ( A [ i ] , A [ j ] )

Swap( A[ Low ] , A [ j ] )

if (low < j-1) QuickSort(Low, j-1)

if (j+1 < High) QuickSort( j+1, high)

Page 9: 3.8 quick sort

Do by Yourself

45 70 75 80 85 60 55 50 65

38 81 22 48 13 69 93 14 45 58 79 72

Page 10: 3.8 quick sort

HEAP SORT

1. A heap is a binary tree that has special structure compared to a general binary tree: The root is greater than any value in a subtree

this means the highest priority is at the root this is less information than a BST and this is why it can be easier

and faster

2.   It is a complete tree the height is always log(n) so all operations are O(log(n))

Page 11: 3.8 quick sort

STEPS1. Consider the values of the elements as

priorities and build the heap tree.

2. Start deleteMin operations, storing each deleted element at the end of the heap array.

It is more efficient because we use only one array , treating its parts differently:

when building the heap tree, part of the array will be considered as the heap, and the rest part - the original array.

when sorting, part of the array will be the heap, and the rest part - the sorted array.

Page 12: 3.8 quick sort

Here is the array: 15, 19, 10, 7, 17, 6Building the heap treeThe array represented as a tree, complete but not ordered:

Page 13: 3.8 quick sort

Start with the rightmost node.It has one greater child and has to be percolated down:

Page 14: 3.8 quick sort

After processing array[3] the situation is:

Page 15: 3.8 quick sort

Next comes array[2]. Its children are smaller, so no percolation is needed.

Page 16: 3.8 quick sort

The last node to be processed is array[1]. Its left child is the greater of the children.The item at array[1] has to be percolated down to the left, swapped with array[2].

Page 17: 3.8 quick sort

As a result the situation is:

Page 18: 3.8 quick sort

The children of array[2] are greater, and item 15 has to be moved down further, swapped with array[5].

Page 19: 3.8 quick sort
Page 20: 3.8 quick sort
Page 21: 3.8 quick sort
Page 22: 3.8 quick sort
Page 23: 3.8 quick sort
Page 24: 3.8 quick sort
Page 25: 3.8 quick sort
Page 26: 3.8 quick sort
Page 27: 3.8 quick sort
Page 28: 3.8 quick sort
Page 29: 3.8 quick sort
Page 30: 3.8 quick sort
Page 31: 3.8 quick sort

Do by Yourself

6, 5, 3, 1, 8, 7, 2, 4  53, 44, 25, 15, 21, 13

Page 32: 3.8 quick sort

                                                                                             

How many squares can you create in this figure by connecting any 4 dots (the corners of a square must lie upon a grid dot?

TRIANGLES: 

How many triangles are located in the image below?

Page 33: 3.8 quick sort

There are 11 squares total; 5 small, 4 medium, and 2 large.

27 triangles.  There are 16 one-cell triangles, 7 four-cell triangles, 3 nine-cell triangles, and 1 sixteen-cell triangle.

Page 34: 3.8 quick sort

GUIDED READING

Page 35: 3.8 quick sort
Page 36: 3.8 quick sort

ASSESSMENTA ____________ more efficient exchange sorting scheme than bubble sort.

a.       quick sort

b.      merge sort

c.       heap sort

d.      radix sort

Page 37: 3.8 quick sort

CONTD..

A __________ uses a divide and conquer strategy

a.       quick sort

b.      merge sort

c.       both a & b

d.      heap sort

Page 38: 3.8 quick sort

CONTD..

Pivot element is used in _____ sort

a.       merge sort

b.      quick sort

c.       heap sort

d.      selection sort

Page 39: 3.8 quick sort

CONTD..

__________ is the fastest sorting algorithm

a.       merge sort

b.      quick sort

c.       bubble sort

d.      insertion sort

Page 40: 3.8 quick sort

CONTD..

The _________ sort is faster than merge sort.

a.       insertion

b.      heap

c.       quick

d.      radix