9
QUICKSORT

Quicksort Algorithm..simply defined through animations..!!

Embed Size (px)

DESCRIPTION

I've seen many ppts on Quicksort algorithm which are very good but fail to maintain simplicity and clarity. So, I've prepared a very simple ppt for students to understand the operation in a better way.

Citation preview

Page 1: Quicksort Algorithm..simply defined through animations..!!

QUICKSORT

Page 2: Quicksort Algorithm..simply defined through animations..!!

Quicksort, or partition-exchange sort, is a sorting algorithm developed by Tony Hoare that, on average, makes O(n log n) comparisons to sort n items. In the worst case, it makes O(n2) comparisons, though this behavior is rare. Quicksort is often faster in practice than other O(n log n) algorithms. Additionally, quicksort's sequential and localized memory references work well with a cache. Quicksort is a comparison sort and, in efficient implementations, is not a stable sort. Quicksort can be implemented with an in-place partitioning algorithm, so the entire sort can be done with only O(log n) additional space used by the stack during the recursion.

INTRODUCTION

TONY HAORE

Page 3: Quicksort Algorithm..simply defined through animations..!!

Quick-sort is a randomized sorting algorithm based on the divide-and-conquer paradigm: Divide: pick a random

element x (called pivot) and partition S into

L elements less than x

E elements equal x

G elements greater than x

Recur: sort L and G

Conquer: join L, E and G

x

x

L GE

x

ALGORITHM

Page 4: Quicksort Algorithm..simply defined through animations..!!

We partition an input sequence as follows: We remove, in turn, each

element y from S and

We insert y into L, E or G, depending on the result of the comparison with the pivot x

Each insertion and removal is at the beginning or at the end of a sequence, and hence takes O(1) time

Thus, the partition step of quick-sort takes O(n) time

Algorithm partition(S, p)Input sequence S, position p of

pivot Output subsequences L, E, G of

the elements of S less

than, equal to,or greater than the

pivot, resp.L, E, G empty sequences

x S.remove(p) while S.isEmpty()

y S.remove(S.first())if y < x

L.insertLast(y)else if y = x

E.insertLast(y)else { y > x }

G.insertLast(y)return L, E, G

PARTITION

Page 5: Quicksort Algorithm..simply defined through animations..!!

EXAMPLE

To start with let us take an unsorted array which we need to sort in ascending order

Now we need to choose a pivot element which can be done in various ways. Here

we choose the mid element that is 7

9 71 4 10

6 3852

Page 6: Quicksort Algorithm..simply defined through animations..!!

9 71 4 10

6 3852

Now elements smaller than the pivot are brought to its left and the greater elements

are brought to is right. To check we start from the 1st element of array and likewise move forward by placing the element as decribed

pivot

7 914 10

625 83

Page 7: Quicksort Algorithm..simply defined through animations..!!

7 914 10

625 83

sorted In this array the pivot element 7 is in sorted position. Now the element at the left of pivot which are smaller than 7 is one part of the array, the element at the right of pivot which are greater than 7 is another part of the array. After this we will perform the same operation as we did in the first pass recursively.

146253 9 10

8

pivot

2 3 5 6 41

pivot

10

98

sortedsorted3 5 6 41

pivot

534 6

sorted34 6

pivot3 4

sorted

Page 8: Quicksort Algorithm..simply defined through animations..!!

7 914 10

625 83

2 3 5 6 41

534 6

3 41 2 5 6 7

10

98

98 10

This is the sorted portioned array. Now

we need to merge them all to get the

sorted array

3 4 5 6

3 4 5 621 98 10

7

3 4 5 621 98 10

7 This is the final sorted array

Page 9: Quicksort Algorithm..simply defined through animations..!!

Presented by

Mahesh Tibrewal