quick sort algorithm

Post on 14-Apr-2017

207 Views

Category:

Engineering

1 Downloads

Preview:

Click to see full reader

TRANSCRIPT

Quick sort Algorithm

By: HALIMA KHAN NIAZI

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

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 .

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

Example

• Consider an array having 6 elements

5 2 6 1 3 4Arrange the elements in ascending order using 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

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

pivot

left right

As the pivot is pointing at the left

So we will start from right

And move towards left

pivot

left right

In previousPivot =5Right=4

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

4 5

pivot

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

0 1 2 3 4 5

1 2 3 4 5 6

Pivot

left Right

Pivot =2Right= 3

Is pivot < right ? YES

So we move right one position towards left

0 1 2 3 4 5

1 2 3 4 5 6

left Right

Pivot

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

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

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

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

0 1 2 3 4 5

1 2 3 4 5 6

THIS IS ARRAY SORTED!

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

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

Order of quick sort

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

top related