DESIGN AND ANALYSIS OF ALGORITHM (DAA)

101
PREPERED BY R.VENGATESH(13BCS2095) S.SURYA(13BCS2087) S.VIGNESH(13BCS2096) V.VIGNESH BALAKRISHNAN(13BCS2097) M.VADIVEL(13BCS2089) GUIDED BY S.SARAVANAN (Asst.profess or) Dept. of

description

THIS IS THE PRESENTATION ABOUT TYPES OF SORTING ALGORITHMS IN DAA& DS.

Transcript of DESIGN AND ANALYSIS OF ALGORITHM (DAA)

Page 1: DESIGN AND ANALYSIS OF ALGORITHM (DAA)

PREPERED BYR.VENGATESH(13BCS2095)

S.SURYA(13BCS2087)S.VIGNESH(13BCS2096)

V.VIGNESH BALAKRISHNAN(13BCS2097)

M.VADIVEL(13BCS2089)

GUIDED BY S.SARAVANAN(Asst.professor) Dept. of CSE

Page 2: DESIGN AND ANALYSIS OF ALGORITHM (DAA)

Definition of Sorting Brute force

◦ Selection sort◦ Bubble sort◦ Radix sort

Divide and Conquer◦ Merge sort◦ Quick sort

Conclusion

17/07/2014 2

Page 3: DESIGN AND ANALYSIS OF ALGORITHM (DAA)

Sorting: an operation that segregates items into groups according to specified criterion.

A = { 3 1 6 2 1 3 4 5 9 0 }

A = { 0 1 1 2 3 3 4 5 6 9 }

Page 4: DESIGN AND ANALYSIS OF ALGORITHM (DAA)

1. We have two group of items:◦ sorted group, and◦ unsorted group

2. Initially, all items are in the unsorted group. The sorted group is empty. ◦ We assume that items in the unsorted group

unsorted. ◦ We have to keep items in the sorted group

sorted.

17/07/2014 4

Page 5: DESIGN AND ANALYSIS OF ALGORITHM (DAA)

1. Select the “best” (eg. smallest) item from the unsorted group, then put the “best” item at the end of the sorted group.

2. Repeat the process until the unsorted group becomes empty.

17/07/2014 5

Page 6: DESIGN AND ANALYSIS OF ALGORITHM (DAA)

5 1 3 4 6 2

17/07/2014 6

Comparison

Data Movement

Sorted

Page 7: DESIGN AND ANALYSIS OF ALGORITHM (DAA)

5 1 3 4 6 2

17/07/2014 7

Comparison

Data Movement

Sorted

Page 8: DESIGN AND ANALYSIS OF ALGORITHM (DAA)

5 1 3 4 6 2

17/07/2014 8

Comparison

Data Movement

Sorted

Page 9: DESIGN AND ANALYSIS OF ALGORITHM (DAA)

5 1 3 4 6 2

17/07/2014 9

Comparison

Data Movement

Sorted

Page 10: DESIGN AND ANALYSIS OF ALGORITHM (DAA)

5 1 3 4 6 2

17/07/2014 10

Comparison

Data Movement

Sorted

Page 11: DESIGN AND ANALYSIS OF ALGORITHM (DAA)

5 1 3 4 6 2

17/07/2014 11

Comparison

Data Movement

Sorted

Page 12: DESIGN AND ANALYSIS OF ALGORITHM (DAA)

5 1 3 4 6 2

17/07/2014 12

Comparison

Data Movement

Sorted

Page 13: DESIGN AND ANALYSIS OF ALGORITHM (DAA)

5 1 3 4 6 2

17/07/2014 13

Comparison

Data Movement

Sorted

Largest

Page 14: DESIGN AND ANALYSIS OF ALGORITHM (DAA)

5 1 3 4 2 6

17/07/2014 14

Comparison

Data Movement

Sorted

Page 15: DESIGN AND ANALYSIS OF ALGORITHM (DAA)

5 1 3 4 2 6

17/07/2014 15

Comparison

Data Movement

Sorted

Page 16: DESIGN AND ANALYSIS OF ALGORITHM (DAA)

5 1 3 4 2 6

17/07/2014 16

Comparison

Data Movement

Sorted

Page 17: DESIGN AND ANALYSIS OF ALGORITHM (DAA)

5 1 3 4 2 6

17/07/2014 17

Comparison

Data Movement

Sorted

Page 18: DESIGN AND ANALYSIS OF ALGORITHM (DAA)

5 1 3 4 2 6

17/07/2014 18

Comparison

Data Movement

Sorted

Page 19: DESIGN AND ANALYSIS OF ALGORITHM (DAA)

5 1 3 4 2 6

17/07/2014 19

Comparison

Data Movement

Sorted

Page 20: DESIGN AND ANALYSIS OF ALGORITHM (DAA)

5 1 3 4 2 6

17/07/2014 20

Comparison

Data Movement

Sorted

Page 21: DESIGN AND ANALYSIS OF ALGORITHM (DAA)

5 1 3 4 2 6

17/07/2014 21

Comparison

Data Movement

Sorted

Largest

Page 22: DESIGN AND ANALYSIS OF ALGORITHM (DAA)

2 1 3 4 5 6

17/07/2014 22

Comparison

Data Movement

Sorted

Page 23: DESIGN AND ANALYSIS OF ALGORITHM (DAA)

2 1 3 4 5 6

17/07/2014 23

Comparison

Data Movement

Sorted

Page 24: DESIGN AND ANALYSIS OF ALGORITHM (DAA)

2 1 3 4 5 6

17/07/2014 24

Comparison

Data Movement

Sorted

Page 25: DESIGN AND ANALYSIS OF ALGORITHM (DAA)

2 1 3 4 5 6

17/07/2014 25

Comparison

Data Movement

Sorted

Page 26: DESIGN AND ANALYSIS OF ALGORITHM (DAA)

2 1 3 4 5 6

17/07/2014 26

Comparison

Data Movement

Sorted

Page 27: DESIGN AND ANALYSIS OF ALGORITHM (DAA)

2 1 3 4 5 6

17/07/2014 27

Comparison

Data Movement

Sorted

Page 28: DESIGN AND ANALYSIS OF ALGORITHM (DAA)

2 1 3 4 5 6

17/07/2014 28

Comparison

Data Movement

Sorted

Largest

Page 29: DESIGN AND ANALYSIS OF ALGORITHM (DAA)

2 1 3 4 5 6

17/07/2014 29

Comparison

Data Movement

Sorted

Page 30: DESIGN AND ANALYSIS OF ALGORITHM (DAA)

2 1 3 4 5 6

17/07/2014 30

Comparison

Data Movement

Sorted

Page 31: DESIGN AND ANALYSIS OF ALGORITHM (DAA)

2 1 3 4 5 6

17/07/2014 31

Comparison

Data Movement

Sorted

Page 32: DESIGN AND ANALYSIS OF ALGORITHM (DAA)

2 1 3 4 5 6

17/07/2014 32

Comparison

Data Movement

Sorted

Page 33: DESIGN AND ANALYSIS OF ALGORITHM (DAA)

2 1 3 4 5 6

17/07/2014 33

Comparison

Data Movement

Sorted

Page 34: DESIGN AND ANALYSIS OF ALGORITHM (DAA)

2 1 3 4 5 6

17/07/2014 34

Comparison

Data Movement

Sorted

Largest

Page 35: DESIGN AND ANALYSIS OF ALGORITHM (DAA)

2 1 3 4 5 6

17/07/2014 35

Comparison

Data Movement

Sorted

Page 36: DESIGN AND ANALYSIS OF ALGORITHM (DAA)

2 1 3 4 5 6

17/07/2014 36

Comparison

Data Movement

Sorted

Page 37: DESIGN AND ANALYSIS OF ALGORITHM (DAA)

2 1 3 4 5 6

17/07/2014 37

Comparison

Data Movement

Sorted

Page 38: DESIGN AND ANALYSIS OF ALGORITHM (DAA)

2 1 3 4 5 6

17/07/2014 38

Comparison

Data Movement

Sorted

Page 39: DESIGN AND ANALYSIS OF ALGORITHM (DAA)

2 1 3 4 5 6

17/07/2014 39

Comparison

Data Movement

Sorted

Largest

Page 40: DESIGN AND ANALYSIS OF ALGORITHM (DAA)

1 2 3 4 5 6

17/07/2014 40

Comparison

Data Movement

Sorted

Page 41: DESIGN AND ANALYSIS OF ALGORITHM (DAA)

1 2 3 4 5 6

17/07/2014 41

Comparison

Data Movement

Sorted

DONE!

Page 42: DESIGN AND ANALYSIS OF ALGORITHM (DAA)

Running time:◦ Worst case: O(N2)◦ Best case: O(N2)

17/07/2014 42

Page 43: DESIGN AND ANALYSIS OF ALGORITHM (DAA)

Adjacent elements are compared and exchanged.It is repeatedly doneAt the end the largest element is bubbled to the last position in the list

17/07/2014 43

Page 44: DESIGN AND ANALYSIS OF ALGORITHM (DAA)

17/07/2014 44

9, 6, 2, 12, 11, 9, 3, 76, 9, 2, 12, 11, 9, 3, 76, 2, 9, 12, 11, 9, 3, 76, 2, 9, 12, 11, 9, 3, 76, 2, 9, 11, 12, 9, 3, 76, 2, 9, 11, 9, 12, 3, 76, 2, 9, 11, 9, 3, 12, 76, 2, 9, 11, 9, 3, 7, 12The 12 is greater than the 7 so they are exchanged.The 12 is greater than the 7 so they are exchanged.

The 12 is greater than the 3 so they are exchanged.The 12 is greater than the 3 so they are exchanged.

The twelve is greater than the 9 so they are exchangedThe twelve is greater than the 9 so they are exchanged

The 12 is larger than the 11 so they are exchanged.The 12 is larger than the 11 so they are exchanged.

In the third comparison, the 9 is not larger than the 12 so no exchange is made. We move on to compare the next pair without any change to the list.

In the third comparison, the 9 is not larger than the 12 so no exchange is made. We move on to compare the next pair without any change to the list.

Now the next pair of numbers are compared. Again the 9 is the larger and so this pair is also exchanged.

Now the next pair of numbers are compared. Again the 9 is the larger and so this pair is also exchanged.

Bubblesort compares the numbers in pairs from left to right exchanging when necessary. Here the first number is compared to the second and as it is larger they are exchanged.

Bubblesort compares the numbers in pairs from left to right exchanging when necessary. Here the first number is compared to the second and as it is larger they are exchanged.

The end of the list has been reached so this is the end of the first pass. The twelve at the end of the list must be largest number in the list and so is now in the correct position. We now start a new pass from left to right.

The end of the list has been reached so this is the end of the first pass. The twelve at the end of the list must be largest number in the list and so is now in the correct position. We now start a new pass from left to right.

Page 45: DESIGN AND ANALYSIS OF ALGORITHM (DAA)

17/07/2014 45

6, 2, 9, 11, 9, 3, 7, 122, 6, 9, 11, 9, 3, 7, 122, 6, 9, 9, 11, 3, 7, 122, 6, 9, 9, 3, 11, 7, 122, 6, 9, 9, 3, 7, 11, 12

6, 2, 9, 11, 9, 3, 7, 12

Notice that this time we do not have to compare the last two numbers as we know the 12 is in position. This pass therefore only requires 6 comparisons.

Notice that this time we do not have to compare the last two numbers as we know the 12 is in position. This pass therefore only requires 6 comparisons.

First Pass

Second Pass

Page 46: DESIGN AND ANALYSIS OF ALGORITHM (DAA)

17/07/2014 46

2, 6, 9, 9, 3, 7, 11, 122, 6, 9, 3, 9, 7, 11, 122, 6, 9, 3, 7, 9, 11, 12

6, 2, 9, 11, 9, 3, 7, 12

2, 6, 9, 9, 3, 7, 11, 12Second Pass

First Pass

Third Pass

This time the 11 and 12 are in position. This pass therefore only requires 5 comparisons.

This time the 11 and 12 are in position. This pass therefore only requires 5 comparisons.

Page 47: DESIGN AND ANALYSIS OF ALGORITHM (DAA)

17/07/2014 47

2, 6, 9, 3, 7, 9, 11, 122, 6, 3, 9, 7, 9, 11, 122, 6, 3, 7, 9, 9, 11, 12

6, 2, 9, 11, 9, 3, 7, 12

2, 6, 9, 9, 3, 7, 11, 12Second Pass

First Pass

Third Pass

Each pass requires fewer comparisons. This time only 4 are needed.Each pass requires fewer comparisons. This time only 4 are needed.

2, 6, 9, 3, 7, 9, 11, 12Fourth Pass

Page 48: DESIGN AND ANALYSIS OF ALGORITHM (DAA)

17/07/2014 48

2, 6, 3, 7, 9, 9, 11, 122, 3, 6, 7, 9, 9, 11, 12

6, 2, 9, 11, 9, 3, 7, 12

2, 6, 9, 9, 3, 7, 11, 12Second Pass

First Pass

Third Pass

The list is now sorted but the algorithm does not know this until it completes a pass with no exchanges.

The list is now sorted but the algorithm does not know this until it completes a pass with no exchanges.

2, 6, 9, 3, 7, 9, 11, 12Fourth Pass

2, 6, 3, 7, 9, 9, 11, 12Fifth Pass

Page 49: DESIGN AND ANALYSIS OF ALGORITHM (DAA)

17/07/2014 49

2, 3, 6, 7, 9, 9, 11, 12

6, 2, 9, 11, 9, 3, 7, 12

2, 6, 9, 9, 3, 7, 11, 12Second Pass

First Pass

Third Pass

2, 6, 9, 3, 7, 9, 11, 12Fourth Pass

2, 6, 3, 7, 9, 9, 11, 12Fifth Pass

Sixth Pass2, 3, 6, 7, 9, 9, 11, 12

This pass no exchanges are made so the algorithm knows the list is sorted. It can therefore save time by not doing the final pass. With other lists this check could save much more work.

This pass no exchanges are made so the algorithm knows the list is sorted. It can therefore save time by not doing the final pass. With other lists this check could save much more work.

Page 50: DESIGN AND ANALYSIS OF ALGORITHM (DAA)

Running time:◦ Worst case: O(N2)◦ Best case: O(N)

Variant:◦ bi-directional bubble sort

original bubble sort: only works to one direction bi-directional bubble sort: works back and forth.

17/07/2014 50

Page 51: DESIGN AND ANALYSIS OF ALGORITHM (DAA)

This sort is unusual because it does not directly compare any of the elements

We instead create a set of buckets and repeatedly separate the elements into the buckets

On each pass, we look at a different part of the elements

17/07/2014 51

Page 52: DESIGN AND ANALYSIS OF ALGORITHM (DAA)

Assuming decimal elements and 10 buckets, we would put the elements into the bucket associated with its units digit

The buckets are actually queues so the elements are added at the end of the bucket

At the end of the pass, the buckets are combined in increasing order

17/07/2014 52

Page 53: DESIGN AND ANALYSIS OF ALGORITHM (DAA)

On the second pass, we separate the elements based on the “tens” digit, and on the third pass we separate them based on the “hundreds” digit

Each pass must make sure to process the elements in order and to put the buckets back together in the correct order

17/07/2014 53

Page 54: DESIGN AND ANALYSIS OF ALGORITHM (DAA)

Each element is examined once for each of the digits it contains, so if the elements have at most M digits and there are N elements this algorithm has order O(M*N)

This means that sorting is linear based on the number of elements

Why then isn’t this the only sorting algorithm used?

17/07/2014 54

Page 55: DESIGN AND ANALYSIS OF ALGORITHM (DAA)

17/07/2014 55

The unit digit is 0

The unit digit is 1

The unit digit is 2

The unit digit is 3

Page 56: DESIGN AND ANALYSIS OF ALGORITHM (DAA)

17/07/2014 56

The unit digits are already in order

Now start sorting the tens digit

Page 57: DESIGN AND ANALYSIS OF ALGORITHM (DAA)

Values in the buckets are now in order

17/07/2014 57

The unit and tens digits are already in order

Now start sorting the hundreds digit

Page 58: DESIGN AND ANALYSIS OF ALGORITHM (DAA)

The most well known algorithm design strategy:

1. Divide instance of problem into two or more smaller instances

2. Solve smaller instances recursively

3. Obtain solution to original (larger) instance by combining these solutions

17/07/2014 58

Page 59: DESIGN AND ANALYSIS OF ALGORITHM (DAA)

17/07/2014 59

subproblem 2 of size n/2

subproblem 1 of size n/2

a solution to subproblem 1

a solution tothe original problem

a solution to subproblem 2

a problem of size n

Page 60: DESIGN AND ANALYSIS OF ALGORITHM (DAA)

Sorting: mergesort and quicksort

Tree traversals

Binary search

Matrix multiplication-Strassen’s algorithm

Convex hull-QuickHull algorithm

17/07/2014 60

Page 61: DESIGN AND ANALYSIS OF ALGORITHM (DAA)

Merge sort

It stores by dividing array into two halves

Sort each them and merge them into single sorted one

17/07/2014 61

8 3 2 9 7 1 5 4

8 3 2 9 7 1 5 4

8 3 2 9 7 1 5 4

8 3 2 9 7 1 5 4

3 8 2 9 1 7 4 5

2 3 8 9 1 4 5 7

1 2 3 4 5 7 8 9

8 3 2 9 7 1 5 4

8 3 2 9 7 1 5 4

8 3 2 9 7 1 5 4

8 3 2 9 7 1 5 4

3 8 2 9 1 7 4 5

2 3 8 9 1 4 5 7

1 2 3 4 5 7 8 9

Page 62: DESIGN AND ANALYSIS OF ALGORITHM (DAA)

Merge-Sort Example

17/07/2014 62

Page 63: DESIGN AND ANALYSIS OF ALGORITHM (DAA)

674523 14 6 3398 42

674523 14 6 3398 42

2398

2398 2398 2398 2398

Merge

23

23

98

98

45 14

45 1445 14

Merge

14

14

45

45

17/07/2014 63

Page 64: DESIGN AND ANALYSIS OF ALGORITHM (DAA)

Merge

98 451423

674523 14 6 3398 42

674523 14 6 3398 42

2398

2398 2398 2398 2398 2398

45 14

45 1445 141445

14

14

4523

23

98 45

45

98

98

676 33 42

676 676

Merge

6

6

67

67

33 4233 42

Merge

33

33

42

42

17/07/2014 64

Page 65: DESIGN AND ANALYSIS OF ALGORITHM (DAA)

98 451423

674523 14 6 3398 42

674523 14 6 3398 42

2398

2398 2398 2398 2398 2398

45 14

45 1445 141445

14

14

4523

23

98 45

45

98

98

676 33 42

676 6766

6

67

67

33 4233 4233

33

42

42

Merge

676 42336

6

67 33

33

4242

42

67

67

17/07/2014 65

Page 66: DESIGN AND ANALYSIS OF ALGORITHM (DAA)

98 451423

674523 14 6 3398 42

674523 14 6 3398 42

2398

2398 2398 2398 2398 2398

45 14

45 1445 141445

14

14

4523

23

98 45

45

98

98

676 33 42

676 6766

6

67

67

33 4233 4233

33

42

42676 42336

6

67 33

33

4242

42

67

67

Merge

23 45 33 42 6714 6

Merge

23 6 42 67

6

14 3314 45 6 42 67

6 14

23 3323

23

45

33

42

42

67

45

67

67 98

17/07/2014 66

Page 67: DESIGN AND ANALYSIS OF ALGORITHM (DAA)

674523 14 6 3398 42

6 14 23 33 42 45 67 98

17/07/2014 67

Page 68: DESIGN AND ANALYSIS OF ALGORITHM (DAA)

Quicksort is more widely used than any other sort. Quicksort is well-studied, not difficult to implement,

works well on a variety of data, and consumes fewer resources that other sorts in nearly all situations.

Quicksort is O(n*log n) time, and O(log n) additional space due to recursion.

17/07/2014 68

Page 69: DESIGN AND ANALYSIS OF ALGORITHM (DAA)

Given a pivot, partition the elements of the array such that the resulting array consists of:

1. One sub-array that contains elements >= pivot 2. Another sub-array that contains elements < pivot

The sub-arrays are stored in the original data array.

Partitioning loops through, swapping elements below/above pivot.

17/07/2014 69

Page 70: DESIGN AND ANALYSIS OF ALGORITHM (DAA)

We are given array of n integers to sort:

17/07/2014 70

40 20 10 80 60 50 7 30 100

Page 71: DESIGN AND ANALYSIS OF ALGORITHM (DAA)

There are a number of ways to pick the pivot element. In this example, we will use the first element in the array:

17/07/2014 71

40 20 10 80 60 50 7 30 100

Page 72: DESIGN AND ANALYSIS OF ALGORITHM (DAA)

Given a pivot, partition the elements of the array such that the resulting array consists of:

1. One sub-array that contains elements >= pivot 2. Another sub-array that contains elements <

pivot

The sub-arrays are stored in the original data array.

Partitioning loops through, swapping elements below/above pivot.

17/07/2014 72

Page 73: DESIGN AND ANALYSIS OF ALGORITHM (DAA)

40 20 10 80 60 50 7 30 100pivot_index = 0

[0] [1] [2] [3] [4] [5] [6] [7] [8]

too_big_index too_small_index

17/07/2014 73

Page 74: DESIGN AND ANALYSIS OF ALGORITHM (DAA)

40 20 10 80 60 50 7 30 100pivot_index = 0

[0] [1] [2] [3] [4] [5] [6] [7] [8]

too_big_index too_small_index

1. While data[too_big_index] <= data[pivot]++too_big_index

17/07/2014 74

Page 75: DESIGN AND ANALYSIS OF ALGORITHM (DAA)

40 20 10 80 60 50 7 30 100pivot_index = 0

[0] [1] [2] [3] [4] [5] [6] [7] [8]

too_big_index too_small_index

1. While data[too_big_index] <= data[pivot]++too_big_index

17/07/2014 75

Page 76: DESIGN AND ANALYSIS OF ALGORITHM (DAA)

40 20 10 80 60 50 7 30 100pivot_index = 0

[0] [1] [2] [3] [4] [5] [6] [7] [8]

too_big_index too_small_index

1. While data[too_big_index] <= data[pivot]++too_big_index

17/07/2014 76

Page 77: DESIGN AND ANALYSIS OF ALGORITHM (DAA)

40 20 10 80 60 50 7 30 100pivot_index = 0

[0] [1] [2] [3] [4] [5] [6] [7] [8]

too_big_index too_small_index

1. While data[too_big_index] <= data[pivot]++too_big_index

2. While data[too_small_index] > data[pivot]--too_small_index

17/07/2014 77

Page 78: DESIGN AND ANALYSIS OF ALGORITHM (DAA)

40 20 10 80 60 50 7 30 100pivot_index = 0

[0] [1] [2] [3] [4] [5] [6] [7] [8]

too_big_index too_small_index

1. While data[too_big_index] <= data[pivot]++too_big_index

2. While data[too_small_index] > data[pivot]--too_small_index

17/07/2014 78

Page 79: DESIGN AND ANALYSIS OF ALGORITHM (DAA)

40 20 10 80 60 50 7 30 100pivot_index = 0

[0] [1] [2] [3] [4] [5] [6] [7] [8]

too_big_index too_small_index

1. While data[too_big_index] <= data[pivot]++too_big_index

2. While data[too_small_index] > data[pivot]--too_small_index

3. If too_big_index < too_small_indexswap data[too_big_index] and data[too_small_index]

17/07/2014 79

Page 80: DESIGN AND ANALYSIS OF ALGORITHM (DAA)

40 20 10 30 60 50 7 80 100pivot_index = 0

[0] [1] [2] [3] [4] [5] [6] [7] [8]

too_big_index too_small_index

1. While data[too_big_index] <= data[pivot]++too_big_index

2. While data[too_small_index] > data[pivot]--too_small_index

3. If too_big_index < too_small_indexswap data[too_big_index] and data[too_small_index]

17/07/2014 80

Page 81: DESIGN AND ANALYSIS OF ALGORITHM (DAA)

40 20 10 30 60 50 7 80 100pivot_index = 0

[0] [1] [2] [3] [4] [5] [6] [7] [8]

too_big_index too_small_index

1. While data[too_big_index] <= data[pivot]++too_big_index

2. While data[too_small_index] > data[pivot]--too_small_index

3. If too_big_index < too_small_indexswap data[too_big_index] and data[too_small_index]

4. While too_small_index > too_big_index, go to 1.

17/07/2014 81

Page 82: DESIGN AND ANALYSIS OF ALGORITHM (DAA)

40 20 10 30 60 50 7 80 100pivot_index = 0

[0] [1] [2] [3] [4] [5] [6] [7] [8]

too_big_index too_small_index

1. While data[too_big_index] <= data[pivot]++too_big_index

2. While data[too_small_index] > data[pivot]--too_small_index

3. If too_big_index < too_small_indexswap data[too_big_index] and data[too_small_index]

4. While too_small_index > too_big_index, go to 1.

17/07/2014 82

Page 83: DESIGN AND ANALYSIS OF ALGORITHM (DAA)

40 20 10 30 60 50 7 80 100pivot_index = 0

[0] [1] [2] [3] [4] [5] [6] [7] [8]

too_big_index too_small_index

1. While data[too_big_index] <= data[pivot]++too_big_index

2. While data[too_small_index] > data[pivot]--too_small_index

3. If too_big_index < too_small_indexswap data[too_big_index] and data[too_small_index]

4. While too_small_index > too_big_index, go to 1.

17/07/2014 83

Page 84: DESIGN AND ANALYSIS OF ALGORITHM (DAA)

40 20 10 30 60 50 7 80 100pivot_index = 0

[0] [1] [2] [3] [4] [5] [6] [7] [8]

too_big_index too_small_index

1. While data[too_big_index] <= data[pivot]++too_big_index

2. While data[too_small_index] > data[pivot]--too_small_index

3. If too_big_index < too_small_indexswap data[too_big_index] and data[too_small_index]

4. While too_small_index > too_big_index, go to 1.

17/07/2014 84

Page 85: DESIGN AND ANALYSIS OF ALGORITHM (DAA)

40 20 10 30 60 50 7 80 100pivot_index = 0

[0] [1] [2] [3] [4] [5] [6] [7] [8]

too_big_index too_small_index

1. While data[too_big_index] <= data[pivot]++too_big_index

2. While data[too_small_index] > data[pivot]--too_small_index

3. If too_big_index < too_small_indexswap data[too_big_index] and data[too_small_index]

4. While too_small_index > too_big_index, go to 1.

17/07/2014 85

Page 86: DESIGN AND ANALYSIS OF ALGORITHM (DAA)

40 20 10 30 60 50 7 80 100pivot_index = 0

[0] [1] [2] [3] [4] [5] [6] [7] [8]

too_big_index too_small_index

1. While data[too_big_index] <= data[pivot]++too_big_index

2. While data[too_small_index] > data[pivot]--too_small_index

3. If too_big_index < too_small_indexswap data[too_big_index] and data[too_small_index]

4. While too_small_index > too_big_index, go to 1.

17/07/2014 86

Page 87: DESIGN AND ANALYSIS OF ALGORITHM (DAA)

1. While data[too_big_index] <= data[pivot]++too_big_index

2. While data[too_small_index] > data[pivot]--too_small_index

3. If too_big_index < too_small_indexswap data[too_big_index] and data[too_small_index]

4. While too_small_index > too_big_index, go to 1.

40 20 10 30 7 50 60 80 100pivot_index = 0

[0] [1] [2] [3] [4] [5] [6] [7] [8]

too_big_index too_small_index

17/07/2014 87

Page 88: DESIGN AND ANALYSIS OF ALGORITHM (DAA)

1. While data[too_big_index] <= data[pivot]++too_big_index

2. While data[too_small_index] > data[pivot]--too_small_index

3. If too_big_index < too_small_indexswap data[too_big_index] and data[too_small_index]

4. While too_small_index > too_big_index, go to 1.

40 20 10 30 7 50 60 80 100pivot_index = 0

[0] [1] [2] [3] [4] [5] [6] [7] [8]

too_big_index too_small_index

17/07/2014 88

Page 89: DESIGN AND ANALYSIS OF ALGORITHM (DAA)

1. While data[too_big_index] <= data[pivot]++too_big_index

2. While data[too_small_index] > data[pivot]--too_small_index

3. If too_big_index < too_small_indexswap data[too_big_index] and data[too_small_index]

4. While too_small_index > too_big_index, go to 1.

40 20 10 30 7 50 60 80 100pivot_index = 0

[0] [1] [2] [3] [4] [5] [6] [7] [8]

too_big_index too_small_index

17/07/2014 89

Page 90: DESIGN AND ANALYSIS OF ALGORITHM (DAA)

1. While data[too_big_index] <= data[pivot]++too_big_index

2. While data[too_small_index] > data[pivot]--too_small_index

3. If too_big_index < too_small_indexswap data[too_big_index] and data[too_small_index]

4. While too_small_index > too_big_index, go to 1.

40 20 10 30 7 50 60 80 100pivot_index = 0

[0] [1] [2] [3] [4] [5] [6] [7] [8]

too_big_index too_small_index

17/07/2014 90

Page 91: DESIGN AND ANALYSIS OF ALGORITHM (DAA)

1. While data[too_big_index] <= data[pivot]++too_big_index

2. While data[too_small_index] > data[pivot]--too_small_index

3. If too_big_index < too_small_indexswap data[too_big_index] and data[too_small_index]

4. While too_small_index > too_big_index, go to 1.

40 20 10 30 7 50 60 80 100pivot_index = 0

[0] [1] [2] [3] [4] [5] [6] [7] [8]

too_big_index too_small_index

17/07/2014 91

Page 92: DESIGN AND ANALYSIS OF ALGORITHM (DAA)

1. While data[too_big_index] <= data[pivot]++too_big_index

2. While data[too_small_index] > data[pivot]--too_small_index

3. If too_big_index < too_small_indexswap data[too_big_index] and data[too_small_index]

4. While too_small_index > too_big_index, go to 1.

40 20 10 30 7 50 60 80 100pivot_index = 0

[0] [1] [2] [3] [4] [5] [6] [7] [8]

too_big_index too_small_index

17/07/2014 92

Page 93: DESIGN AND ANALYSIS OF ALGORITHM (DAA)

1. While data[too_big_index] <= data[pivot]++too_big_index

2. While data[too_small_index] > data[pivot]--too_small_index

3. If too_big_index < too_small_indexswap data[too_big_index] and data[too_small_index]

4. While too_small_index > too_big_index, go to 1.

40 20 10 30 7 50 60 80 100pivot_index = 0

[0] [1] [2] [3] [4] [5] [6] [7] [8]

too_big_index too_small_index

17/07/2014 93

Page 94: DESIGN AND ANALYSIS OF ALGORITHM (DAA)

1. While data[too_big_index] <= data[pivot]++too_big_index

2. While data[too_small_index] > data[pivot]--too_small_index

3. If too_big_index < too_small_indexswap data[too_big_index] and data[too_small_index]

4. While too_small_index > too_big_index, go to 1.

40 20 10 30 7 50 60 80 100pivot_index = 0

[0] [1] [2] [3] [4] [5] [6] [7] [8]

too_big_index too_small_index

17/07/2014 94

Page 95: DESIGN AND ANALYSIS OF ALGORITHM (DAA)

1. While data[too_big_index] <= data[pivot]++too_big_index

2. While data[too_small_index] > data[pivot]--too_small_index

3. If too_big_index < too_small_indexswap data[too_big_index] and data[too_small_index]

4. While too_small_index > too_big_index, go to 1.

40 20 10 30 7 50 60 80 100pivot_index = 0

[0] [1] [2] [3] [4] [5] [6] [7] [8]

too_big_index too_small_index

17/07/2014 95

Page 96: DESIGN AND ANALYSIS OF ALGORITHM (DAA)

1. While data[too_big_index] <= data[pivot]++too_big_index

2. While data[too_small_index] > data[pivot]--too_small_index

3. If too_big_index < too_small_indexswap data[too_big_index] and data[too_small_index]

4. While too_small_index > too_big_index, go to 1.5. Swap data[too_small_index] and data[pivot_index]

40 20 10 30 7 50 60 80 100pivot_index = 0

[0] [1] [2] [3] [4] [5] [6] [7] [8]

too_big_index too_small_index

17/07/2014 96

Page 97: DESIGN AND ANALYSIS OF ALGORITHM (DAA)

1. While data[too_big_index] <= data[pivot]++too_big_index

2. While data[too_small_index] > data[pivot]--too_small_index

3. If too_big_index < too_small_indexswap data[too_big_index] and data[too_small_index]

4. While too_small_index > too_big_index, go to 1.5. Swap data[too_small_index] and data[pivot_index]

7 20 10 30 40 50 60 80 100pivot_index = 4

[0] [1] [2] [3] [4] [5] [6] [7] [8]

too_big_index too_small_index

17/07/2014 97

Page 98: DESIGN AND ANALYSIS OF ALGORITHM (DAA)

17/07/2014 98

7 20 10 30 40 50 60 80 100

[0] [1] [2] [3] [4] [5] [6] [7] [8]

<= data[pivot] > data[pivot]

Page 99: DESIGN AND ANALYSIS OF ALGORITHM (DAA)

17/07/2014 99

7 20 10 30 40 50 60 80 100

[0] [1] [2] [3] [4] [5] [6] [7] [8]

<= data[pivot] > data[pivot]

Page 100: DESIGN AND ANALYSIS OF ALGORITHM (DAA)

Quicksort illustrates the operation of the basic algorithm. When the array is partitioned, one element is in place on the diagonal, the left subarray has its upper corner at that element, and the right subarray has its lower corner at that element. The original file is divided into two smaller parts that are sorted independently. The left subarray is always sorted first, so the sorted result emerges as a line of black dots moving right and up the diagonal.

17/07/2014 100

Page 101: DESIGN AND ANALYSIS OF ALGORITHM (DAA)

In this ,we can understand that every sorting will depends upon one another.

So, we should understand sorting is the best starting for every representation.

From this, Quick sort will easily perform sorting.

17/07/2014 101