3.8 quick sort

40
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)

Transcript of 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)

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

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];

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

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]

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);

}

}

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∪ ∪

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)

Do by Yourself

45 70 75 80 85 60 55 50 65

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

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))

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.

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

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

After processing array[3] the situation is:

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

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].

As a result the situation is:

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

Do by Yourself

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

                                                                                             

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?

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.

GUIDED READING

ASSESSMENTA ____________ more efficient exchange sorting scheme than bubble sort.

a.       quick sort

b.      merge sort

c.       heap sort

d.      radix sort

CONTD..

A __________ uses a divide and conquer strategy

a.       quick sort

b.      merge sort

c.       both a & b

d.      heap sort

CONTD..

Pivot element is used in _____ sort

a.       merge sort

b.      quick sort

c.       heap sort

d.      selection sort

CONTD..

__________ is the fastest sorting algorithm

a.       merge sort

b.      quick sort

c.       bubble sort

d.      insertion sort

CONTD..

The _________ sort is faster than merge sort.

a.       insertion

b.      heap

c.       quick

d.      radix