KEMBAR78
DESIGN AND ANALYSIS OF ALGORITHM (DAA) | PPT
PREPERED BY
R.VENGATESH(13BCS2095)
S.SURYA(13BCS2087)
S.VIGNESH(13BCS2096)
V.VIGNESH
BALAKRISHNAN(13BCS2097)
M.VADIVEL(13BCS2089)
GUIDED BY
S.SARAVANAN
(Asst.professor)
Dept. of CSE
 Definition of Sorting
 Brute force
◦ Selection sort
◦ Bubble sort
◦ Radix sort
 Divide and Conquer
◦ Merge sort
◦ Quick sort
 Conclusion
17/07/2014 2
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 }
1. We have two group of items:
◦ sorted group, and
◦ unsorted group
1. 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
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
5 1 3 4 6 2
17/07/2014 6
Comparison
Data Movement
Sorted
5 1 3 4 6 2
17/07/2014 7
Comparison
Data Movement
Sorted
5 1 3 4 6 2
17/07/2014 8
Comparison
Data Movement
Sorted
5 1 3 4 6 2
17/07/2014 9
Comparison
Data Movement
Sorted
5 1 3 4 6 2
17/07/2014 10
Comparison
Data Movement
Sorted
5 1 3 4 6 2
17/07/2014 11
Comparison
Data Movement
Sorted
5 1 3 4 6 2
17/07/2014 12
Comparison
Data Movement
Sorted
5 1 3 4 6 2
17/07/2014 13
Comparison
Data Movement
Sorted

Largest
5 1 3 4 2 6
17/07/2014 14
Comparison
Data Movement
Sorted
5 1 3 4 2 6
17/07/2014 15
Comparison
Data Movement
Sorted
5 1 3 4 2 6
17/07/2014 16
Comparison
Data Movement
Sorted
5 1 3 4 2 6
17/07/2014 17
Comparison
Data Movement
Sorted
5 1 3 4 2 6
17/07/2014 18
Comparison
Data Movement
Sorted
5 1 3 4 2 6
17/07/2014 19
Comparison
Data Movement
Sorted
5 1 3 4 2 6
17/07/2014 20
Comparison
Data Movement
Sorted
5 1 3 4 2 6
17/07/2014 21
Comparison
Data Movement
Sorted

Largest
2 1 3 4 5 6
17/07/2014 22
Comparison
Data Movement
Sorted
2 1 3 4 5 6
17/07/2014 23
Comparison
Data Movement
Sorted
2 1 3 4 5 6
17/07/2014 24
Comparison
Data Movement
Sorted
2 1 3 4 5 6
17/07/2014 25
Comparison
Data Movement
Sorted
2 1 3 4 5 6
17/07/2014 26
Comparison
Data Movement
Sorted
2 1 3 4 5 6
17/07/2014 27
Comparison
Data Movement
Sorted
2 1 3 4 5 6
17/07/2014 28
Comparison
Data Movement
Sorted

Largest
2 1 3 4 5 6
17/07/2014 29
Comparison
Data Movement
Sorted
2 1 3 4 5 6
17/07/2014 30
Comparison
Data Movement
Sorted
2 1 3 4 5 6
17/07/2014 31
Comparison
Data Movement
Sorted
2 1 3 4 5 6
17/07/2014 32
Comparison
Data Movement
Sorted
2 1 3 4 5 6
17/07/2014 33
Comparison
Data Movement
Sorted
2 1 3 4 5 6
17/07/2014 34
Comparison
Data Movement
Sorted

Largest
2 1 3 4 5 6
17/07/2014 35
Comparison
Data Movement
Sorted
2 1 3 4 5 6
17/07/2014 36
Comparison
Data Movement
Sorted
2 1 3 4 5 6
17/07/2014 37
Comparison
Data Movement
Sorted
2 1 3 4 5 6
17/07/2014 38
Comparison
Data Movement
Sorted
2 1 3 4 5 6
17/07/2014 39
Comparison
Data Movement
Sorted

Largest
1 2 3 4 5 6
17/07/2014 40
Comparison
Data Movement
Sorted
1 2 3 4 5 6
17/07/2014 41
Comparison
Data Movement
Sorted
DONE!
 Running time:
◦ Worst case: O(N2
)
◦ Best case: O(N2
)
17/07/2014 42
Adjacent elements are compared and exchanged.
It is repeatedly done
At the end the largest element is bubbled to the last
position in the list
17/07/2014 43
17/07/2014 44
9, 6, 2, 12, 11, 9, 3, 7
6, 9, 2, 12, 11, 9, 3, 7
6, 2, 9, 12, 11, 9, 3, 7
6, 2, 9, 12, 11, 9, 3, 7
6, 2, 9, 11, 12, 9, 3, 7
6, 2, 9, 11, 9, 12, 3, 7
6, 2, 9, 11, 9, 3, 12, 7
6, 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.
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
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, 12
Second 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.
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, 12
Second 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
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, 12
Second 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
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, 12
Second Pass
First Pass
Third Pass
2, 6, 9, 3, 7, 9, 11, 12Fourth Pass
2, 6, 3, 7, 9, 9, 11, 12Fifth Pass
Sixth Pass
2, 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.
 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
 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
 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
 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
 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
17/07/2014 55
The unit digit is 0
The unit digit is 1
 The unit digit is 2
 The unit digit is 3
17/07/2014 56
The unit digits are already in order
Now start sorting the tens digit
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
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
17/07/2014 59
subproblem 2
of size n/2
subproblem 1
of size n/2
a solution to
subproblem 1
a solution to
the original problem
a solution to
subproblem 2
a problem of
size n
 Sorting: mergesort and quicksort
 Tree traversals
 Binary search
 Matrix multiplication-Strassen’s algorithm
 Convex hull-QuickHull algorithm
17/07/2014 60
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
Merge-Sort Example
17/07/2014 62
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
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
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
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
674523 14 6 3398 42
6 14 23 33 42 45 67 98
17/07/2014 67
 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
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
We are given array of n integers to sort:
17/07/2014 70
40 20 10 80 60 50 7 30 100
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
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
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
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
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
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
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
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
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_index
swap data[too_big_index] and data[too_small_index]
17/07/2014 79
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_index
swap data[too_big_index] and data[too_small_index]
17/07/2014 80
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_index
swap data[too_big_index] and data[too_small_index]
4. While too_small_index > too_big_index, go to 1.
17/07/2014 81
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_index
swap data[too_big_index] and data[too_small_index]
4. While too_small_index > too_big_index, go to 1.
17/07/2014 82
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_index
swap data[too_big_index] and data[too_small_index]
4. While too_small_index > too_big_index, go to 1.
17/07/2014 83
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_index
swap data[too_big_index] and data[too_small_index]
4. While too_small_index > too_big_index, go to 1.
17/07/2014 84
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_index
swap data[too_big_index] and data[too_small_index]
4. While too_small_index > too_big_index, go to 1.
17/07/2014 85
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_index
swap data[too_big_index] and data[too_small_index]
4. While too_small_index > too_big_index, go to 1.
17/07/2014 86
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_index
swap 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
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_index
swap 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
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_index
swap 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
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_index
swap 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
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_index
swap 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
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_index
swap 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
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_index
swap 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
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_index
swap 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
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_index
swap 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
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_index
swap 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
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_index
swap 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
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]
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]
 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
 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

DESIGN AND ANALYSIS OF ALGORITHM (DAA)

  • 1.
  • 2.
     Definition ofSorting  Brute force ◦ Selection sort ◦ Bubble sort ◦ Radix sort  Divide and Conquer ◦ Merge sort ◦ Quick sort  Conclusion 17/07/2014 2
  • 3.
    Sorting: an operationthat 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 }
  • 4.
    1. We havetwo group of items: ◦ sorted group, and ◦ unsorted group 1. 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
  • 5.
    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
  • 6.
    5 1 34 6 2 17/07/2014 6 Comparison Data Movement Sorted
  • 7.
    5 1 34 6 2 17/07/2014 7 Comparison Data Movement Sorted
  • 8.
    5 1 34 6 2 17/07/2014 8 Comparison Data Movement Sorted
  • 9.
    5 1 34 6 2 17/07/2014 9 Comparison Data Movement Sorted
  • 10.
    5 1 34 6 2 17/07/2014 10 Comparison Data Movement Sorted
  • 11.
    5 1 34 6 2 17/07/2014 11 Comparison Data Movement Sorted
  • 12.
    5 1 34 6 2 17/07/2014 12 Comparison Data Movement Sorted
  • 13.
    5 1 34 6 2 17/07/2014 13 Comparison Data Movement Sorted  Largest
  • 14.
    5 1 34 2 6 17/07/2014 14 Comparison Data Movement Sorted
  • 15.
    5 1 34 2 6 17/07/2014 15 Comparison Data Movement Sorted
  • 16.
    5 1 34 2 6 17/07/2014 16 Comparison Data Movement Sorted
  • 17.
    5 1 34 2 6 17/07/2014 17 Comparison Data Movement Sorted
  • 18.
    5 1 34 2 6 17/07/2014 18 Comparison Data Movement Sorted
  • 19.
    5 1 34 2 6 17/07/2014 19 Comparison Data Movement Sorted
  • 20.
    5 1 34 2 6 17/07/2014 20 Comparison Data Movement Sorted
  • 21.
    5 1 34 2 6 17/07/2014 21 Comparison Data Movement Sorted  Largest
  • 22.
    2 1 34 5 6 17/07/2014 22 Comparison Data Movement Sorted
  • 23.
    2 1 34 5 6 17/07/2014 23 Comparison Data Movement Sorted
  • 24.
    2 1 34 5 6 17/07/2014 24 Comparison Data Movement Sorted
  • 25.
    2 1 34 5 6 17/07/2014 25 Comparison Data Movement Sorted
  • 26.
    2 1 34 5 6 17/07/2014 26 Comparison Data Movement Sorted
  • 27.
    2 1 34 5 6 17/07/2014 27 Comparison Data Movement Sorted
  • 28.
    2 1 34 5 6 17/07/2014 28 Comparison Data Movement Sorted  Largest
  • 29.
    2 1 34 5 6 17/07/2014 29 Comparison Data Movement Sorted
  • 30.
    2 1 34 5 6 17/07/2014 30 Comparison Data Movement Sorted
  • 31.
    2 1 34 5 6 17/07/2014 31 Comparison Data Movement Sorted
  • 32.
    2 1 34 5 6 17/07/2014 32 Comparison Data Movement Sorted
  • 33.
    2 1 34 5 6 17/07/2014 33 Comparison Data Movement Sorted
  • 34.
    2 1 34 5 6 17/07/2014 34 Comparison Data Movement Sorted  Largest
  • 35.
    2 1 34 5 6 17/07/2014 35 Comparison Data Movement Sorted
  • 36.
    2 1 34 5 6 17/07/2014 36 Comparison Data Movement Sorted
  • 37.
    2 1 34 5 6 17/07/2014 37 Comparison Data Movement Sorted
  • 38.
    2 1 34 5 6 17/07/2014 38 Comparison Data Movement Sorted
  • 39.
    2 1 34 5 6 17/07/2014 39 Comparison Data Movement Sorted  Largest
  • 40.
    1 2 34 5 6 17/07/2014 40 Comparison Data Movement Sorted
  • 41.
    1 2 34 5 6 17/07/2014 41 Comparison Data Movement Sorted DONE!
  • 42.
     Running time: ◦Worst case: O(N2 ) ◦ Best case: O(N2 ) 17/07/2014 42
  • 43.
    Adjacent elements arecompared and exchanged. It is repeatedly done At the end the largest element is bubbled to the last position in the list 17/07/2014 43
  • 44.
    17/07/2014 44 9, 6,2, 12, 11, 9, 3, 7 6, 9, 2, 12, 11, 9, 3, 7 6, 2, 9, 12, 11, 9, 3, 7 6, 2, 9, 12, 11, 9, 3, 7 6, 2, 9, 11, 12, 9, 3, 7 6, 2, 9, 11, 9, 12, 3, 7 6, 2, 9, 11, 9, 3, 12, 7 6, 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.
  • 45.
    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
  • 46.
    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, 12 Second 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.
  • 47.
    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, 12 Second 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
  • 48.
    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, 12 Second 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
  • 49.
    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, 12 Second Pass First Pass Third Pass 2, 6, 9, 3, 7, 9, 11, 12Fourth Pass 2, 6, 3, 7, 9, 9, 11, 12Fifth Pass Sixth Pass 2, 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.
  • 50.
     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
  • 51.
     This sortis 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
  • 52.
     Assuming decimalelements 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
  • 53.
     On thesecond 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
  • 54.
     Each elementis 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
  • 55.
    17/07/2014 55 The unitdigit is 0 The unit digit is 1  The unit digit is 2  The unit digit is 3
  • 56.
    17/07/2014 56 The unitdigits are already in order Now start sorting the tens digit
  • 57.
    Values in thebuckets are now in order 17/07/2014 57 The unit and tens digits are already in order Now start sorting the hundreds digit
  • 58.
    The most wellknown 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
  • 59.
    17/07/2014 59 subproblem 2 ofsize n/2 subproblem 1 of size n/2 a solution to subproblem 1 a solution to the original problem a solution to subproblem 2 a problem of size n
  • 60.
     Sorting: mergesortand quicksort  Tree traversals  Binary search  Matrix multiplication-Strassen’s algorithm  Convex hull-QuickHull algorithm 17/07/2014 60
  • 61.
    Merge sort  Itstores 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
  • 62.
  • 63.
    674523 14 63398 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
  • 64.
    Merge 98 451423 674523 146 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
  • 65.
    98 451423 674523 146 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
  • 66.
    98 451423 674523 146 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
  • 67.
    674523 14 63398 42 6 14 23 33 42 45 67 98 17/07/2014 67
  • 68.
     Quicksort ismore 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
  • 69.
    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
  • 70.
    We are givenarray of n integers to sort: 17/07/2014 70 40 20 10 80 60 50 7 30 100
  • 71.
    There are anumber 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
  • 72.
    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
  • 73.
    40 20 1080 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
  • 74.
    40 20 1080 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
  • 75.
    40 20 1080 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
  • 76.
    40 20 1080 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
  • 77.
    40 20 1080 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
  • 78.
    40 20 1080 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
  • 79.
    40 20 1080 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_index swap data[too_big_index] and data[too_small_index] 17/07/2014 79
  • 80.
    40 20 1030 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_index swap data[too_big_index] and data[too_small_index] 17/07/2014 80
  • 81.
    40 20 1030 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_index swap data[too_big_index] and data[too_small_index] 4. While too_small_index > too_big_index, go to 1. 17/07/2014 81
  • 82.
    40 20 1030 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_index swap data[too_big_index] and data[too_small_index] 4. While too_small_index > too_big_index, go to 1. 17/07/2014 82
  • 83.
    40 20 1030 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_index swap data[too_big_index] and data[too_small_index] 4. While too_small_index > too_big_index, go to 1. 17/07/2014 83
  • 84.
    40 20 1030 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_index swap data[too_big_index] and data[too_small_index] 4. While too_small_index > too_big_index, go to 1. 17/07/2014 84
  • 85.
    40 20 1030 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_index swap data[too_big_index] and data[too_small_index] 4. While too_small_index > too_big_index, go to 1. 17/07/2014 85
  • 86.
    40 20 1030 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_index swap data[too_big_index] and data[too_small_index] 4. While too_small_index > too_big_index, go to 1. 17/07/2014 86
  • 87.
    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_index swap 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
  • 88.
    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_index swap 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
  • 89.
    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_index swap 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
  • 90.
    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_index swap 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
  • 91.
    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_index swap 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
  • 92.
    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_index swap 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
  • 93.
    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_index swap 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
  • 94.
    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_index swap 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
  • 95.
    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_index swap 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
  • 96.
    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_index swap 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
  • 97.
    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_index swap 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
  • 98.
    17/07/2014 98 7 2010 30 40 50 60 80 100 [0] [1] [2] [3] [4] [5] [6] [7] [8] <= data[pivot] > data[pivot]
  • 99.
    17/07/2014 99 7 2010 30 40 50 60 80 100 [0] [1] [2] [3] [4] [5] [6] [7] [8] <= data[pivot] > data[pivot]
  • 100.
     Quicksort illustratesthe 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
  • 101.
     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