quick sort algorithm

49
Quick sort Algorithm By: HALIMA KHAN NIAZI

Upload: halima-khan

Post on 14-Apr-2017

206 views

Category:

Engineering


1 download

TRANSCRIPT

Page 1: Quick sort algorithm

Quick sort Algorithm

By: HALIMA KHAN NIAZI

Page 2: Quick sort algorithm

Sorting

• Sorting is the process of arranging data in some logical order

• This logical order may be in ascending or descending in case of numeric value or dictionary order in case of alphanumeric values.

• Two types of sorting -Internal sorting -External sorting

Page 3: Quick sort algorithm

Quick sort

This sorting algorithm uses the idea of divide and conquer.It finds the element called PIVOT which divides the array into two halves in such a way that the elements in the left half are smaller than Pivot and elements in the right half greater than the Pivot .

Page 4: Quick sort algorithm

We will follow three steps recursively:

1.Find pivot that divides the array into two halves

2.Quick sort the left half3.Quick sort the right half

Page 5: Quick sort algorithm

Example

• Consider an array having 6 elements

5 2 6 1 3 4Arrange the elements in ascending order using quick sort algorithm

Page 6: Quick sort algorithm

This is our unsorted array

0 1 2 3 4 55 2 6 1 3 4

initially pointing to the first element

pivot

Array IndexArray element’s Left Initially pointing to

the first element

Right Initially pointing to the last element of the array

We will quick sort this array

Page 7: Quick sort algorithm

pivot

left right

Remember this rule:

All elements to the right of pivot must be greater than pivotAll elements to the left of pivot must be smaller than pivot

Page 8: Quick sort algorithm

pivot

left right

As the pivot is pointing at the left

So we will start from right

And move towards left

Page 9: Quick sort algorithm

pivot

left right

In previousPivot =5Right=4

As pivot<right? NO So we swap pivot and Right

4 5

pivot

Page 10: Quick sort algorithm

pivot

left right

So we will start from left

Now the pivot is pointing to the right

0 1 2 3 4 54 2 6 1 3 5

And move towards right

Page 11: Quick sort algorithm

0 1 2 3 4 54 2 6 1 3 5

pivot

left right

Pivot=5Left=4

Is pivot > left? YesSo we move left one position towards right

Page 12: Quick sort algorithm

0 1 2 3 4 54 2 6 1 3 5

left right

pivot

Pivot =5Left=2

Is pivot > left? Yes So we move left one position

towards right

Page 13: Quick sort algorithm

0 1 2 3 4 54 2 6 1 3 5

left right

pivot

Pivot =5Left=6

Is pivot > left? NO So we swap pivot and left

And move the pivot to left

Page 14: Quick sort algorithm

0 1 2 3 4 54 2 5 1 3 6

left right

pivot

Now the pivot is pointing at left

So we will start from right

And move towards left

Page 15: Quick sort algorithm

0 1 2 3 4 54 2 5 1 3 6

pivot

left right

Pivot=5Right=6 Is pivot < right ?

Yes So we move one position

towards left

Page 16: Quick sort algorithm

0 1 2 3 4 54 2 5 1 3 6

pivot

left right

Is pivot < right ? Yes

So we move one position towards left

Pivot=5Right=6

Page 17: Quick sort algorithm

0 1 2 3 4 54 2 5 1 3 6

pivot

left right

Pivot=5Right=3Is pivot < right?

No So we swap pivot and right

Page 18: Quick sort algorithm

0 1 2 3 4 54 2 3 1 5 6

pivot

left right

Is pivot < right? No

So we swap pivot and right

And move the pivot to right

Page 19: Quick sort algorithm

0 1 2 3 4 54 2 3 1 5 6

pivot

left right

Now the pivot is pointing at right

So we will start from left

And move towards right

Page 20: Quick sort algorithm

0 1 2 3 4 54 2 3 1 5 6

left right

pivot

Pivot =5Left=3Is pivot > left?

YESSo we move left one position towards right

Page 21: Quick sort algorithm

0 1 2 3 4 54 2 3 1 5 6

left right

pivot

Is pivot > left? YESSo we move left one position towards right

Pivot =5Left=1

Page 22: Quick sort algorithm

0 1 2 3 4 54 2 3 1 5 6

left right

Now both left and right are pointing at the same element of the array

This means 5 is the pivot and it is at the sorted position

Elements left of pivot are smaller Elements right of pivot are greater

So pivot divided the array into two sub arrays

pivot

Left sub array Right sub array

We will now quick sort the left sub array

Page 23: Quick sort algorithm

0 1 2 3 4 54 2 3 1 5 6

Array Index

Array element

Initially pointing to the first PIVOT

left right

Initially pointing to the first element of the sub array

Initially pointing to the last element of the sub array

Page 24: Quick sort algorithm

0 1 2 3 4 54 2 3 1 5 6

left right

PIVOT

As the pivot pointing at left

So we will start from right

And move towards left

Page 25: Quick sort algorithm

0 1 2 3 4 54 2 3 1 5 6

left right

PIVOT

Pivot=4Right=1

Is pivot < right? NOSo we swap pivot and right

Page 26: Quick sort algorithm

0 1 2 3 4 51 2 3 4 5 6

left right

PIVOT

Is pivot < right? NOSo we swap pivot and right

Now move the pivot to right

Page 27: Quick sort algorithm

0 1 2 3 4 51 2 3 4 5 6

left right

PIVOTNow the pivot pointing at right

So we will start from left

And move towards right

Page 28: Quick sort algorithm

0 1 2 3 4 51 2 3 4 5 6

left right

PIVOT

Pivot=4Left=1

Is pivot > left? YESSo we move left one position towards right

Page 29: Quick sort algorithm

0 1 2 3 4 51 2 3 4 5 6

left right

PIVOT

Pivot=4Left=2

Is pivot > left? YESSo we move left one position towards right

Page 30: Quick sort algorithm

0 1 2 3 4 51 2 3 4 5 6

left right

PIVOT

Is pivot > left? YESSo we move left one position towards right

Pivot=4Left=3

Page 31: Quick sort algorithm

0 1 2 3 4 51 2 3 4 5 6

left right

PIVOT

Now both left and right are pointing at the same element of the array

This means 4 is the pivot and it is at the sorted position

Elements left of pivot are smaller

Pivot has divided the array into left sub array as there is a wall at the right side of 4

Left sub array

So we will quick sort the left sub array

Page 32: Quick sort algorithm

pivot

left right

0 1 2 3 4 5

1 2 3 4 5 6

Initially pointing to the first element of the sub array

Initially pointing to the last element of the array

Array index

Array element

Initially pointing to the first element

Page 33: Quick sort algorithm

left right

0 1 2 3 4 5

1 2 3 4 5 6

pivot

As the pivot is pointing at left

So we will start from right

And move towards left

Page 34: Quick sort algorithm

0 1 2 3 4 5

1 2 3 4 5 6

rightleft

pivot

Pivot =1Right =3

Is pivot < right? yes So we move right one position towards left

Page 35: Quick sort algorithm

0 1 2 3 4 5

1 2 3 4 5 6

rightleft

pivot

Pivot =1Right =2

Is pivot < right? yes So we move right one position towards left

Page 36: Quick sort algorithm

0 1 2 3 4 5

1 2 3 4 5 6

rightleft

pivot

Pivot =1Right =1

Now both left and right are pointing at same element of the array This means 1 is the pivot and it is at the sorted position

Elements right of pivot are greater

Pivot has divided the array into right sub array as there is no element to the left of 1.

So we will quick sort out the right sub array .

Right sub array

Page 37: Quick sort algorithm

0 1 2 3 4 5

1 2 3 4 5 6

left Right

Initially pointing to the first element of the sub array Initially pointing to the last

element of the sub array

Array index

Array element

Initially pointing to the first element

Pivot

Page 38: Quick sort algorithm

0 1 2 3 4 5

1 2 3 4 5 6

left Right

Pivot

Now the pivot is pointing at left

So we will start from right and move towards left

Page 39: Quick sort algorithm

0 1 2 3 4 5

1 2 3 4 5 6

Pivot

left Right

Pivot =2Right= 3

Is pivot < right ? YES

Page 40: Quick sort algorithm

So we move right one position towards left

0 1 2 3 4 5

1 2 3 4 5 6

left Right

Pivot

Page 41: Quick sort algorithm

0 1 2 3 4 5

1 2 3 4 5 6

left Right

Pivot

Now both left and right are pointing at same element of the array This means 2 is the pivot and it is at the sorted position

Elements right of pivot are greater

Pivot has divided the array into right sub array as there is a wall at the left side 2

Right sub array

So we will quick sort the right sub array

Page 42: Quick sort algorithm

0 1 2 3 4 5

1 2 3 4 5 6

Left Right

Pivot

Now both left and right are pointing at same element of the array This means 3 is the pivot and it is at the sorted position

Pivots wall on both side’s of the array so we are done with the left sub array

Remember 5 was our first pivot and it divided the array into two halves

Page 43: Quick sort algorithm

0 1 2 3 4 5

1 2 3 4 5 6

Left Right

Pivot

Now both left and right are pointing at same element of the array This means 3 is the pivot and it is at the sorted position

Pivots wall on both side’s of the array so we are done with the left sub array

Remember 5 was our first pivot and it divided the array into two halves

Left sub array Right sub array

Page 44: Quick sort algorithm

0 1 2 3 4 5

1 2 3 4 5 6

Left Right

Pivot

Now both left and right are pointing at the same element of the array this mean’s 6 is pivot and it is at sorted position

Page 45: Quick sort algorithm

0 1 2 3 4 5

1 2 3 4 5 6

THIS IS ARRAY SORTED!

Page 46: Quick sort algorithm

About quick sort

• Quick sort follow recursive algorithm• We find the pivot which divides the array into

two halves• We then find pivot for these halves which

ultimately gives us a sorted array

Page 47: Quick sort algorithm

Algorithm• /*a[0:n-1] is an array of n elementBeg=first index of an array End=last index of an array*/• Quick sort( a, beg, end)Begin If beg < end then Call partition Array(a, beg, end, pivotLOC); // this will find pivot location Call quicksort(a,beg,pivotLoc-1); // Quick sort left sub array Call quicksort(a,pivotLoc+1,end); // Quick sort right sub array End ifEnd

Page 48: Quick sort algorithm

Order of quick sort

• For array of n elements• Order of quick sort is • f(n)=O(

Page 49: Quick sort algorithm