74
QUICK SORT

Quick sort-Data Structure

Embed Size (px)

Citation preview

Page 1: Quick sort-Data Structure

QUICK SORT

Page 2: Quick sort-Data Structure

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 elements in the left half are smaller than pivot and elements in the

right half are greater than pivot.

QUICK SORT

Page 3: Quick sort-Data Structure

Three steps

Find pivot that divides the array into two halves.

Quick sort the left half.

Quick sort the right half.

QUICK SORT

Page 4: Quick sort-Data Structure

Example

Consider an array having 6 elements

5 2 6 1 3 4

Arrange the elements in ascending order using quick sort algorithm

Page 5: Quick sort-Data Structure

0 1 2 3 4 5

5 2 6 1 3 4Array index

Array element

This is our unsorted array

Page 6: Quick sort-Data Structure

0 1 2 3 4 5

5 2 6 1 3 4Array index

Array element

This is our unsorted array

Left

Page 7: Quick sort-Data Structure

0 1 2 3 4 5

5 2 6 1 3 4Array index

Array element

This is our unsorted array

LeftInitially pointing to the

First element of the array

Page 8: Quick sort-Data Structure

0 1 2 3 4 5

5 2 6 1 3 4Array index

Array element

This is our unsorted array

LeftInitially pointing to the

First element of the array

Right

Page 9: Quick sort-Data Structure

0 1 2 3 4 5

5 2 6 1 3 4Array index

Array element

This is our unsorted array

LeftInitially pointing to the

First element of the array

RightInitially pointing to the

Last element of the array

Page 10: Quick sort-Data Structure

0 1 2 3 4 5

5 2 6 1 3 4Array index

Array element

This is our unsorted array

LeftInitially pointing to the

First element of the array

RightInitially pointing to the

Last element of the array

Pivot

Page 11: Quick sort-Data Structure

0 1 2 3 4 5

5 2 6 1 3 4Array index

Array element

This is our unsorted array

LeftInitially pointing to the

First element of the array

RightInitially pointing to the

Last element of the array

Pivot

Initially pointing to theFirst element

Page 12: Quick sort-Data Structure

0 1 2 3 4 5

5 2 6 1 3 4Array index

Array element

This is our unsorted array

LeftInitially pointing to the

First element of the array

RightInitially pointing to the

Last element of the array

Pivot

Initially pointing to theFirst element

We will quick sort this array

Page 13: Quick sort-Data Structure

0 1 2 3 4 5

5 2 6 1 3 4

Left Right

Pivot

Page 14: Quick sort-Data Structure

0 1 2 3 4 5

5 2 6 1 3 4

Left Right

Pivot

Remember this rule:

Page 15: Quick sort-Data Structure

0 1 2 3 4 5

5 2 6 1 3 4

Left Right

Pivot

Remember this rule:

All element to the RIGHT of pivot be GREATER than pivot.

Page 16: Quick sort-Data Structure

0 1 2 3 4 5

5 2 6 1 3 4

Left Right

Pivot

Remember this rule:

All element to the RIGHT of pivot be GREATER than pivot.All element to the LEFT of pivot be SMALLER than pivot.

Page 17: Quick sort-Data Structure

0 1 2 3 4 5

5 2 6 1 3 4

Left Right

Pivot

As the pivotis pointing at

left

Page 18: Quick sort-Data Structure

0 1 2 3 4 5

5 2 6 1 3 4

Left Right

Pivot

As the pivotis pointing at

left So we will startfrom right

Page 19: Quick sort-Data Structure

0 1 2 3 4 5

5 2 6 1 3 4

Left Right

Pivot

As the pivotis pointing at

left So we will startfrom right And move towards left

Page 20: Quick sort-Data Structure

0 1 2 3 4 5

5 2 6 1 3 4

Left Right

Pivot

Pivot = 5Right =4

Page 21: Quick sort-Data Structure

0 1 2 3 4 5

5 2 6 1 3 4

Left Right

Pivot

Pivot = 5Right =4

Is Pivot < Right

( 5 < 4)

Page 22: Quick sort-Data Structure

0 1 2 3 4 5

5 2 6 1 3 4

Left Right

Pivot

Pivot = 5Right =4NO

Is Pivot < Right

( 5 < 4)

Page 23: Quick sort-Data Structure

0 1 2 3 4 5

5 2 6 1 3 4

Left Right

Pivot

Pivot = 5Right =4

So we swap pivot and rightNO

Is Pivot < Right

( 5 < 4)

Page 24: Quick sort-Data Structure

0 1 2 3 4 5

5 2 6 1 3 4

Left Right

Pivot

Pivot = 5Right =4

So we swap pivot and rightNO

Is Pivot < Right

( 5 < 4)

Page 25: Quick sort-Data Structure

0 1 2 3 4 5

4 2 6 1 3 5

Left Right

Pivot

Pivot = 5Right =4

So we swap pivot and rightNO

Is Pivot < Right

( 5 < 4)

Page 26: Quick sort-Data Structure

0 1 2 3 4 5

4 2 6 1 3 5

Left Right

Pivot

Pivot = 5Left =4

Is Pivot < Left

Now move pivot to right

Page 27: Quick sort-Data Structure

0 1 2 3 4 5

4 2 6 1 3 5

Left Right

Pivot

Pivot = 5Left =4

Is Pivot < Left

NO

So we swap pivot to the right

Page 28: Quick sort-Data Structure

0 1 2 3 4 5

4 2 6 1 3 5

Left Right

Pivot

Now the pivotis pointing at

right

Page 29: Quick sort-Data Structure

0 1 2 3 4 5

4 2 6 1 3 5

Left Right

Pivot

Now the pivotis pointing at

rightSo we will start from left

Page 30: Quick sort-Data Structure

0 1 2 3 4 5

4 2 6 1 3 5

Left Right

Pivot

Now the pivotis pointing at

rightSo we will start from left

And move towards right

Page 31: Quick sort-Data Structure

0 1 2 3 4 5

4 2 6 1 3 5

Left Right

Pivot

Pivot = 5Left =4

Is Pivot > Left( 5 > 4)

Page 32: Quick sort-Data Structure

0 1 2 3 4 5

4 2 6 1 3 5

Left Right

Pivot

Pivot = 5Left =4

Is Pivot > Left

YES

( 5 > 4)

Page 33: Quick sort-Data Structure

0 1 2 3 4 5

4 2 6 1 3 5

Left Right

Pivot

Pivot = 5Left =4

Is Pivot > Left

YES

( 5 > 4)

So we move left one position towards right

Page 34: Quick sort-Data Structure

0 1 2 3 4 5

4 2 6 1 3 5

Left Right

Pivot

Pivot = 5Left =2

Is Pivot > Left( 5 > 2)

Page 35: Quick sort-Data Structure

0 1 2 3 4 5

4 2 6 1 3 5

Left Right

Pivot

Pivot = 5Left =2

Is Pivot > Left( 5 > 2)

YES

Page 36: Quick sort-Data Structure

0 1 2 3 4 5

4 2 6 1 3 5

Left Right

Pivot

Pivot = 5Left =2

Is Pivot > Left( 5 > 2)

YESSo we move left one position

towards right

Page 37: Quick sort-Data Structure

0 1 2 3 4 5

4 2 6 1 3 5

Left Right

Pivot

Pivot = 5Left =6

Is Pivot > Left( 5 > 6)

Page 38: Quick sort-Data Structure

0 1 2 3 4 5

4 2 6 1 3 5

Left Right

Pivot

Pivot = 5Left =6

Is Pivot > Left( 5 > 6)

NO

Page 39: Quick sort-Data Structure

0 1 2 3 4 5

4 2 6 1 3 5

Left Right

Pivot

Pivot = 5Left =6

Is Pivot > Left( 5 > 6)

NO

So we swap pivot and left

Page 40: Quick sort-Data Structure

0 1 2 3 4 5

4 2 6 1 3 5

Left Right

Pivot

Pivot = 5Left =6

Is Pivot > Left( 5 > 6)

NO

So we swap pivot and left

Page 41: Quick sort-Data Structure

0 1 2 3 4 5

4 2 5 1 3 6

Left Right

Pivot

Pivot = 5Left =6

Is Pivot > Left( 5 > 6)

NO

So we swap pivot and left

Page 42: Quick sort-Data Structure

0 1 2 3 4 5

4 2 5 1 3 6

Left Right

Pivot

Pivot = 5Left =6

Is Pivot > Left( 5 > 6)

NO

So we swap pivot and left

And move the pivot to left

Page 43: Quick sort-Data Structure

0 1 2 3 4 5

4 2 5 1 3 6

Left Right

Pivot

Page 44: Quick sort-Data Structure

0 1 2 3 4 5

4 2 5 1 3 6

Left Right

Pivot

Now the pivot is pointing at left

Page 45: Quick sort-Data Structure

0 1 2 3 4 5

4 2 5 1 3 6

Left Right

Pivot

Now the pivot is pointing at left So we will start

from right

Page 46: Quick sort-Data Structure

0 1 2 3 4 5

4 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 47: Quick sort-Data Structure

0 1 2 3 4 5

4 2 5 1 3 6

Left Right

Pivot

Pivot = 5Right =6

Is Pivot < Right( 5 < 6)

Page 48: Quick sort-Data Structure

0 1 2 3 4 5

4 2 5 1 3 6

Left Right

Pivot

Pivot = 5Right =6

Is Pivot < Right( 5 < 6)

YES

Page 49: Quick sort-Data Structure

0 1 2 3 4 5

4 2 5 1 3 6

Left Right

Pivot

Pivot = 5Right =6

Is Pivot < Right( 5 < 6)

YES

So we move right one position towards left

Page 50: Quick sort-Data Structure

0 1 2 3 4 5

4 2 5 1 3 6

Left Right

Pivot

Pivot = 5Right =3

Is Pivot < Right( 5 < 3)

Page 51: Quick sort-Data Structure

0 1 2 3 4 5

4 2 5 1 3 6

Left Right

Pivot

Pivot = 5Right =3

Is Pivot < Right( 5 < 3)

NO

Page 52: Quick sort-Data Structure

0 1 2 3 4 5

4 2 5 1 3 6

Left Right

Pivot

Pivot = 5Right =3

Is Pivot < Right( 5 < 3)

NOSo we swap pivot and right

Page 53: Quick sort-Data Structure

0 1 2 3 4 5

4 2 3 1 5 6

Left Right

Pivot

Pivot = 5Right =3

Is Pivot < Right( 5 < 3)

NOSo we swap pivot and right

Page 54: Quick sort-Data Structure

0 1 2 3 4 5

4 2 3 1 5 6

Left Right

Pivot

Pivot = 5Right =3

Is Pivot > Right( 5 > 3)

NOSo we swap pivot and right

And move the pivot to right

Page 55: Quick sort-Data Structure

0 1 2 3 4 5

4 2 3 1 5 6

Left Right

Pivot

Pivot = 5Left =3

Is Pivot > Left( 5 > 3)

Page 56: Quick sort-Data Structure

0 1 2 3 4 5

4 2 3 1 5 6

Left Right

Pivot

Pivot = 5Left =3YES

Is Pivot > Left( 5 > 3)

Page 57: Quick sort-Data Structure

0 1 2 3 4 5

4 2 3 1 5 6

Left Right

Pivot

Pivot = 5Left =3

Is Pivot > Left( 5 > 3)

So we move left one position towards right

YES

Page 58: Quick sort-Data Structure

0 1 2 3 4 5

4 2 3 1 5 6

Left Right

Pivot

Pivot = 5Left = 1

Is Pivot > Left( 5 > 1)

Page 59: Quick sort-Data Structure

0 1 2 3 4 5

4 2 3 1 5 6

Left Right

Pivot

Page 60: Quick sort-Data Structure

0 1 2 3 4 5

4 2 3 1 5 6

Left Right

Pivot

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

Page 61: Quick sort-Data Structure

0 1 2 3 4 5

4 2 3 1 5 6

Left Right

Pivot

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

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

45

Page 62: Quick sort-Data Structure

0 1 2 3 4 5

4 2 3 1 5 6

Left Right

Pivot

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

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

45

Elements left of pivot are smaller

Page 63: Quick sort-Data Structure

0 1 2 3 4 5

4 2 3 1 5 6

Left Right

Pivot

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

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

45

Elements left of pivot are smallerElements right of pivot are greater

Page 64: Quick sort-Data Structure

0 1 2 3 4 5

4 2 3 1 5 6

Left Right

Pivot

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

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

45

Elements left of pivot are smallerElements right of pivot are greater

So pivot has divided the array into two sub array

Page 65: Quick sort-Data Structure

0 1 2 3 4 5

4 2 3 1 5 6

Left Right

Pivot

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

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

45

Elements left of pivot are smallerElements right of pivot are greater

Left sub array Right sub array

So pivot has divided the array into two sub array

Page 66: Quick sort-Data Structure

0 1 2 3 4 5

4 2 3 1 5 6

Left Right

Pivot

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

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

45

Elements left of pivot are smallerElements right of pivot are greater

Left sub array Right sub array

So pivot has divided the array into two sub array We will now quick sort

the left sub array

Page 67: Quick sort-Data Structure

0 1 2 3 4 5

4 2 3 1 5 6

Left Right

Pivot

Pivot = 4Left =1

Is Pivot < Left( 4 < 1)

Page 68: Quick sort-Data Structure

0 1 2 3 4 5

4 2 3 1 5 6

Pivot

Pivot = 4Left =1

Is Pivot < Left( 4 < 1)

NO

Left Right

Page 69: Quick sort-Data Structure

0 1 2 3 4 5

4 2 3 1 5 6

Pivot

Pivot = 4Left =1

Is Pivot < Left( 4 < 1)

NOSo we swap pivot and right

Left Right

Page 70: Quick sort-Data Structure

0 1 2 3 4 5

1 2 3 4 5 6

Pivot

Pivot = 4Left =1

Is Pivot < Left( 4 < 1)

NOSo we swap pivot and right

Left Right

Page 71: Quick sort-Data Structure

0 1 2 3 4 5

1 2 3 4 5 6

Left Right

Pivot

Pivot = 4Left =1

Is Pivot < Left( 4 < 1)

NOSo we swap pivot and right

Now move the pivot to right

Page 72: Quick sort-Data Structure

0 1 2 3 4 5

1 2 3 4 5 6

Pivot

Pivot = 4Left =1

Is Pivot < Left( 4 < 1)

NOSo we swap pivot and right

Left Right

Page 73: Quick sort-Data Structure

0 1 2 3 4 5

1 2 3 4 5 6

The Array is Sorted

Page 74: Quick sort-Data Structure

Jeanie Lyn Arnoco