SlideShare ist ein Scribd-Unternehmen logo
1 von 124
Downloaden Sie, um offline zu lesen
Data Structures & Algorithm
CS-102
Lecture 9
Elementary Sorting & NlogN
Algorithms
Lecturer: Syeda Nazia Ashraf 1
Elementary Sorting Algorithms
• Selection Sort
• Insertion Sort
• Bubble Sort
2
Selection Sort
• Main idea:
–find the smallest element
–put it in the first position
–find the next smallest element
–put it in the second position
–…
• And so on, until you get to the end of the list
3
Selection Sort: Idea
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.
Selection Sort: Cont’d
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.
1 2 3
0
7 12 19
5
a: 19
1 2 3
0
7 19 12
5
a: 12
1 2 3
0
19 7 12
5
a: 7
Selection Sort -- Example
1 2 3
0
7 12 19
5
a:
1 2 3
0
5 7 12
19
a:
a:
1 2 3
0
5
Swap Action (SelectionSorting)
5 8 20 10 7
5 7 20 10 8
5 7 8 10 20
20 8 5 10 7
5 7 8 10 20
Selection Sort
5 1 3 4 6 2
Comparison
Data Movement
Sorted
Selection Sort
5 1 3 4 6 2
Comparison
Data Movement
Sorted
Selection Sort
5 1 3 4 6 2
Comparison
Data Movement
Sorted
Selection Sort
5 1 3 4 6 2
Comparison
Data Movement
Sorted
Selection Sort
5 1 3 4 6 2
Comparison
Data Movement
Sorted
Selection Sort
5 1 3 4 6 2
Comparison
Data Movement
Sorted
Selection Sort
5 1 3 4 6 2
Comparison
Data Movement
Sorted
Selection Sort
5 1 3 4 6 2
Comparison
Data Movement
Sorted

Largest
Selection Sort
5 1 3 4 2 6
Comparison
Data Movement
Sorted
Selection Sort
5 1 3 4 2 6
Comparison
Data Movement
Sorted
Selection Sort
5 1 3 4 2 6
Comparison
Data Movement
Sorted
Selection Sort
5 1 3 4 2 6
Comparison
Data Movement
Sorted
Selection Sort
5 1 3 4 2 6
Comparison
Data Movement
Sorted
Selection Sort
5 1 3 4 2 6
Comparison
Data Movement
Sorted
Selection Sort
5 1 3 4 2 6
Comparison
Data Movement
Sorted
Selection Sort
5 1 3 4 2 6
Comparison
Data Movement
Sorted

Largest
Selection Sort
2 1 3 4 5 6
Comparison
Data Movement
Sorted
Selection Sort
2 1 3 4 5 6
Comparison
Data Movement
Sorted
Selection Sort
2 1 3 4 5 6
Comparison
Data Movement
Sorted
Selection Sort
2 1 3 4 5 6
Comparison
Data Movement
Sorted
Selection Sort
2 1 3 4 5 6
Comparison
Data Movement
Sorted
Selection Sort
2 1 3 4 5 6
Comparison
Data Movement
Sorted
Selection Sort
2 1 3 4 5 6
Comparison
Data Movement
Sorted

Largest
Selection Sort
2 1 3 4 5 6
Comparison
Data Movement
Sorted
Selection Sort
2 1 3 4 5 6
Comparison
Data Movement
Sorted
Selection Sort
2 1 3 4 5 6
Comparison
Data Movement
Sorted
Selection Sort
2 1 3 4 5 6
Comparison
Data Movement
Sorted
Selection Sort
2 1 3 4 5 6
Comparison
Data Movement
Sorted
Selection Sort
2 1 3 4 5 6
Comparison
Data Movement
Sorted

Largest
Selection Sort
2 1 3 4 5 6
Comparison
Data Movement
Sorted
Selection Sort
2 1 3 4 5 6
Comparison
Data Movement
Sorted
Selection Sort
2 1 3 4 5 6
Comparison
Data Movement
Sorted
Selection Sort
2 1 3 4 5 6
Comparison
Data Movement
Sorted
Selection Sort
2 1 3 4 5 6
Comparison
Data Movement
Sorted

Largest
Selection Sort
1 2 3 4 5 6
Comparison
Data Movement
Sorted
Selection Sort
1 2 3 4 5 6
Comparison
Data Movement
Sorted
DONE!
42
40 2 1 3 3 4 0 -1 65
58
43
40 2 1 43 3 4 0 -1 42 65
58
3
40 2 1 43 3 4 0 -1 58 3 65
42
40 2 1 43 3 65 0 -1 58 3 42 4
Selection Sort: Example
42
40 2 1 3 3 4 0 65
58
43
-1
42
-1 2 1 3 3 4 0 65
58
43
40
42
-1 2 1 3 3 4 65
58
43
40
0
42
-1 2 1 0 3 4 65
58
43
40
3
Selection Sort: Example
1
42
-1 2 1 3 4 65
58
43
40
3
0
42
-1 0 3 4 65
58
43
40
3
2
1 42
-1 0 3 4 65
58
43
40
3
2
1 42
0 3 4 65
58
43
40
3
2
-1
1 42
0 3 4 65
58
43
40
3
2
-1
Selection Sort: Example
Insertion Sort
• Basic idea (sorting cards):
– Starts by considering the first two elements of
the array data, if out of order, swap them
– Consider the third element, insert it into the
proper position among the first three elements.
– Consider the forth element, insert it into the
proper position among the first four elements.
– … …
Insertion Sort -- Example
1 2 3
0
12 5 7
19
a:
1 2 3
0
19 5 7
12
a:
1 2 3
0
12 19 7
5
a:
1 2 3
0
7 12 19
5
a:
Insertion Sort -- animation
1 2 3
0
12 5 7
19
a:
1 2 3
0
19 5 7
19
a:
count val pos
1 12 0
1 12 -1
1 2 3
0
5 7
19
a: 12
19
1 2 3
0
19 5 7
12
a:
12
Insertion Sort -- animation (cont)
1 2 3
0
12 19 7
5
a:
count val pos
2 5 1
2 5 -1
1 2 3
0
19 5 7
12
a:
1 2 3
0
5 7
12
a: 19 19
1 2 3
0
19 7
12
a: 19
12
2 5 0
1 2 3
0
12 19 7
12
a: 5
Insertion Sort -- animation (cont)
1 2 3
0
12 19 7
5
a:
count val pos
3 7 2
3 7 0
3 7 1
1 2 3
0
12 19 7
5
a: 19
1 2 3
0
12 19 19
5
a: 12
1 2 3
0
12 12 19
5
a: 7
1 2 3
0
7 12 19
5
a:
Bubble Sort
• Basic idea (lighter bubbles rise to the top):
– Exchange neighbouring items until the largest item
reaches the end of the array
– Repeat for the rest of the array
• Also called Sink Sort.
1 2 3
0
12 7 19
5
a:
Bubble Sort -- Example
1 2 3
0
12 7 19
5
a:
19 12 7
5
1 2 3
0
a:
12 19 7
5
a:
1 2 3
0
5 12 7
19
1 2 3
0
a: a:
1 2 3
0
12 7 19
5
a:
1 2 3
0
7 12 19
5
a:
1 2 3
0
7 12 19
5
a:
1 2 3
0
7 12 19
5
a:
1 2 3
0
7 12 19
5
a:
Bubble Sort Example
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, 12
The 12 is greater than the 7 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 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.
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.
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.
Bubble Sort Example
6, 2, 9, 11, 9, 3, 7, 12
2, 6, 9, 11, 9, 3, 7, 12
2, 6, 9, 9, 11, 3, 7, 12
2, 6, 9, 9, 3, 11, 7, 12
2, 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.
First Pass
Second Pass
Bubble Sort Example
2, 6, 9, 9, 3, 7, 11, 12
2, 6, 9, 3, 9, 7, 11, 12
2, 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.
Bubble Sort Example
2, 6, 9, 3, 7, 9, 11, 12
2, 6, 3, 9, 7, 9, 11, 12
2, 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.
2, 6, 9, 3, 7, 9, 11, 12
Fourth Pass
Bubble Sort Example
2, 6, 3, 7, 9, 9, 11, 12
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
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, 12
Fourth Pass
2, 6, 3, 7, 9, 9, 11, 12
Fifth Pass
Bubble Sort Example
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, 12
Fourth Pass
2, 6, 3, 7, 9, 9, 11, 12
Fifth 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.
Bubble Sort: Example
40 2 1 43 3 65 0 -1 58 3 42 4
65
2 1 40 3 43 0 -1 58 3 42 4
65
58
1 2 3 40 0 -1 43 3 42 4
1 2 3 40
0 65
-1 43 58
3 42 4
1
2
3
4
• Notice that at least one element will be in the
correct position each iteration.
1 0 -1 3
2 65
3 43 58
42
40
4
Bubble Sort: Example
0 -1 1 2 65
3 43 58
42
40
4
3
-1 0 1 2 65
3 43 58
42
40
4
3
6
7
8
1 2 0 3
-1 3 40 65
43 58
42
4
5
Summary
• Insertion, Selection and Bubble sort:
– Worst case time complexity is proportional to N2.
Bestsorting routines are N log(N)
NLogN Algorithms
• Divide and Conquer
• Merge Sort
• Quick Sort
• Heap Sort
Divide and Conquer
What if we split the list into two parts?
10 12 8 4 2 11 7 5
10 12 8 4 2 11 7 5
64
Divide and Conquer
Sort the two parts:
10 12 8 4 2 11 7 5
4 8 10 12 2 5 7 11
65
Divide and Conquer
Then merge the two parts together:
4 8 10 12 2 5 7 11
2 4 5 7 8 10 11 12
66
Divide and Conquer
• Why not divide the halves in half?
• The quarters in half?
• And so on . . .
• When should we stop?
At n = 1
67
Search
Divide and Conquer
Search
Search
Recall: Binary Search
68
Sort
Divide and Conquer
Sort Sort
Sort Sort Sort Sort
69
Divide and Conquer
Combine
Combine Combine
70
Mergesort
• Mergesort is a divide and conquer algorithm
that does exactly that.
• It splits the list in half
• Mergesorts the two halves
• Then merges the two sorted halves together
• Mergesort can be implemented recursively
71
Mergesort
• The mergesort algorithm involves three
steps:
– If the number of items to sort is 0 or 1, return
– Recursively sort the first and second halves
separately
– Merge the two sorted halves into a sorted group
72
Merging: animation
4 8 10 12 2 5 7 11
2
73
Merging: animation
4 8 10 12 2 5 7 11
2 4
74
Merging: animation
4 8 10 12 2 5 7 11
2 4 5
75
Merging
4 8 10 12 2 5 7 11
2 4 5 7
76
Mergesort
8 12 11 2 7 5
4
10
Split the list in half.
8 12
4
10
Mergesort the left half.
Split the list in half. Mergesort the left half.
4
10
Split the list in half. Mergesort the left half.
10
Mergesort the right.
4
77
Mergesort
8 12 11 2 7 5
4
10
8 12
4
10
4
10
Mergesort the right half.
Merge the two halves.
10
4 8 12
12
8
Merge the two halves.
8
8 12
78
Mergesort
8 12 11 2 7 5
4
10
8 12
4
10
Merge the two halves.
4
10
Mergesort the right half. Merge the two halves.
10
4 8 12
10 12
8
4
10
4 8 12
79
Mergesort
10 12 11 2 7 5
8
4
Mergesort the right half.
11 2 7 5
11 2
11 2
80
Mergesort
10 12 11 2 7 5
8
4
Mergesort the right half.
11 2 7 5
11 2
2 11
2 11
81
Mergesort
10 12 11 2 7 5
8
4
Mergesort the right half.
11 2 7 5
2 11
7
5
7 5
82
Mergesort
10 12 11 2 7 5
8
4
Mergesort the right half.
11 2 5
7
2 11
83
Mergesort
10 12 2 5 7 11
8
4
Mergesort the right half.
84
Mergesort
5 7 8 10 11 12
4
2
Merge the two halves.
85
3 5 15 28 30 6 10 14 22 43 50
a: b:
aSize: 5 bSize: 6
mergeArrays
tmp:
mergeArrays
5 15 28 30 10 14 22 43 50
a: b:
tmp:
i=0
k=0
j=0
3 6
mergeArrays
5 15 28 30 10 14 22 43 50
a: b:
tmp:
i=0
k=0
3
j=0
3 6
mergeArrays
3 15 28 30 10 14 22 43 50
a: b:
tmp:
i=1 j=0
k=1
3 5
5 6
mergeArrays
3 5 28 30 10 14 22 43 50
a: b:
3 5
tmp:
i=2 j=0
k=2
6
15 6
mergeArrays
3 5 28 30 6 14 22 43 50
a: b:
3 5 6
tmp:
i=2 j=1
k=3
15 10
10
10
mergeArrays
3 5 28 30 6 22 43 50
a: b:
3 5 6
tmp:
i=2 j=2
k=4
15 10 14
14
14
10
mergeArrays
3 5 28 30 6 14 43 50
a: b:
3 5 6
tmp:
i=2 j=3
k=5
15 10 22
15
14
10
mergeArrays
3 5 30 6 14 43 50
a: b:
3 5 6
tmp:
i=3 j=3
k=6
15 10 22
22
15
28
14
10
mergeArrays
3 5 30 6 14 50
a: b:
3 5 6
tmp:
i=3 j=4
k=7
15 10 22
28
15
28 43
22
14
10
mergeArrays
3 5 6 14 50
a: b:
3 5 6
tmp:
i=4 j=4
k=8
15 10 22
30
15
28 43
22
30
28
14
10
mergeArrays
3 5 6 14 50
a: b:
3 5 6 30
tmp:
i=5 j=4
k=9
15 10 22
15
28 43
22
30
28 43 50
Done.
Merge Sort Algorithm (Cont.)
Merge Sort(A) [splitting]
{
n= length(A) [store no. of elements in A]
if(n < 2) [BASE or EXIT CONDITION:
if only one element in array, then return and stop
partitioning /recursion]
return
mid = n/2
left= array of size(mid)
right= array of size(n-mid)
for(i = 0 to mid-1)
left[i] = A[i]
for(i = mid to n-1)
right[i-mid] = A[i]
98
Merge Sort(left)
Merge Sort(right)
Merge(left, right, A)
}
[recursive call]
[Merge Left and Right
Sub lists into A]
Merge Sort Algorithm
Merge(L, R, A) [merge sorted halves]
{
nL=Length(L) [no. of elements in left]
nR=Length(R) [no. of elements in left]
i=0
j=0
k=0
while(i < nL && j < nR)
{
if(L[i]) <= R[j]
{
A[k] = L[i]
i = i+1
}
else
99
{
A[k] = R[j]
j = j+1
}
k = k+1
}
while (i < nL)
{
A[k]=L[i]
i=i+1
k=k+1
}
while (j < nR)
{
A[k]=R[j]
j=j+1
k=k+1
}
}
[For leftover L and R
elements for fill
remaining positions of k]
Mergesort Analysis
Merging the two lists of size n/2:
O(n)
Merging the four lists of size n/4:
O(n)
.
.
.
Merging the n lists of size 1:
O(n)
O (log n)
times
100
Mergesort Analysis
• Mergesort is O(n log n)
• Space?
• The other sorts we have looked at
(insertion, selection) are in-place (only
require a constant amount of extra space)
• Mergesort requires O(n) extra space for
merging
101
Quicksort
• Quicksort is another divide and conquer
algorithm
• Quicksort is based on the idea of partitioning
(splitting) the list around a pivot or split value
102
Quicksort
• Quicksort is a divide and conquer algorithm.
Quicksort first divides a large array into two smaller
sub-arrays: the low elements and the high elements.
Quicksort can then recursively sort the sub-arrays.
• The steps are:
• Pick an element, called a pivot, from the array.
• Reorder the array so that all elements with values
less than the pivot come before the pivot, while all
elements with values greater than the pivot come
after it (equal values can go either way). After this
partitioning, the pivot is in its final position. This is
called the partition operation.
• Recursively apply the above steps to the sub-array of
elements with smaller values and separately to the
sub-array of elements with greater values.
103
Quicksort
104
Initial Step - First Partition
Sort Left Partition in the same way
Quicksort
First the list is partitioned around a pivot value.
Pivot can be chosen from the beginning, end or
middle of list):
8 3
2 11 7
5
4 10
12
4 5
5
pivot value
105
Quicksort
The pivot is swapped to the last position and the
remaining elements are compared starting at the
ends.
8 3 2 11 7 5
4 10
12
4 5
low high
5
pivot value
106
4 < 5
Move
right
7>5
Move
left
Quicksort
Then the low index moves right until it is at an element
that is larger than the pivot value (i.e., it is on the
wrong side)
8 6 2 11 7 5
10
12
4 6
low high
5
pivot value
3
12
107
7>5 move
left
12!<5 stop
Quicksort
Then the high index moves left until it is at an
element that is smaller than the pivot value (i.e., it
is on the wrong side)
8 6 2 11 7 5
4 10
12
4 6
low high
5
pivot value
3 2
108
11>5 move
left
2!>5 stop
Quicksort
Then the two values are swapped and the index
values are updated:
8 6 2 11 7 5
4 10
12
4 6
low high
5
pivot value
3
2 12
109
10!<5
stop
3!>5
stop
Interchange 2 and 12
Quicksort
This continues until the two index values pass
each other:
8 6 12 11 7 5
4
2
4 6
low high
5
pivot value
3
10
3 10
110
3<5
Move
10>5
Move
Interchange 10 and 3
111
Quicksort
This continues until the two index values pass
each other:
8 6 12 11 7 5
4
2
4 6
low high
5
pivot value
3
10
3 10
111
8!<5
Stop
10>5
Move
Interchange 10 and 3
112
112
Quicksort
This continues until the two index values pass
each other:
8 6 12 11 7 5
4
2
4 6
lowhigh
5
pivot value
3
10
3 10
112
8>5
Move
Interchange 10 and 3
Quicksort
The two index values pass each other:
8 6 12 11 7 5
4
2
4 6
low
high
5
pivot value
10
3
113
3<5 8>5
Quicksort
Then the pivot value is swapped into position:
8 6 12 11 7 5
4
2
4 6
low
high
10
3 8
5
114
Interchange 8 with
pivot value 5
Quicksort
Recursively quicksort the two parts:
5 6 12 11 7 8
4
2
4 6
10
3
Quicksort the left part Quicksort the right part
5
115
Quicksort
116
5 6 12 11 7 8
4
2
4 6
10
3 5
3
pivot value
8
pivot value
Quicksort the left part Quicksort the right part
10!<8
stop
7!>8
stop
high
low low high
4!<3 2!>3
Quicksort
117
5 6 12 11 10 8
4
4
2 6
7
3 5
3
pivot value
8
pivot value
Quicksort the left part Quicksort the right part
12!<8
11>8
move
high
low low high
Interchange 4 and 2
Interchange 10 and 7
118
Quicksort
118
5 6 12 11 10 8
4
4
2 6
7
3 5
3
pivot value
8
pivot value
Quicksort the left part Quicksort the right part
12>8
Move
high
low low
high
Interchange 4 and 2
119
119
Quicksort
119
5 6 12 11 10 8
4
4
2 6
7
3 5
3
pivot value
8
pivot value
Quicksort the left part Quicksort the right part
12!<8
high
low low
high
Interchange 4 and 2
Interchange 10 and 7
Quicksort
120
5 6 8 11 10 12
4
3
2 6
7
4 5
sorted
Interchange 12 with Pivot value
Interchange 4 with Pivot value
sorted
QuickSort the
right part
Quicksort
121
5 6 8 11 10 12
4
3
2 6
7
4 5
sorted sorted QuickSort the
right part
12
pivot value
low high
11<12 10!>12
Quicksort
122
5 6 8 10 11 12
4
3
2 6
7
4 5
sorted
Quick Sort Algorithm
Quick Sort(A, start, end) [A is array, end is Pivot]
{
If (start < end) then [or if(start >=end) return]
{
Pindex = Partition(A, start, end)
Quick Sort(A, start, Pindex-1 ) [Left first half- values before Pivot]
Quick Sort(A, Pindex+1 , end ) [Right second half values after Pivot]
}
}
123
Quick Sort Algorithm (Cont.)
Partition(A, start, end)
{
Pivot = A[end]
Pindex = start
for(i=start to end-1) [i is current position, end-1 because end is Pivot]
{
if(A[i] <= Pivot) then [if current <= Pivot]
{
swap(A[i] , A[Pindex])
Pindex = Pindex+1 [index only change when swapping occurs]
}
}
124
swap(A[Pindex], A[end]) [for last swap
of Index and Pivot]
return(Pindex)
}

Weitere ähnliche Inhalte

Ähnlich wie LECT 9-DS ALGO(sorting).pdf

Ähnlich wie LECT 9-DS ALGO(sorting).pdf (20)

Sorting algorithms
Sorting algorithmsSorting algorithms
Sorting algorithms
 
Sorting
SortingSorting
Sorting
 
Sorting algorithms
Sorting algorithmsSorting algorithms
Sorting algorithms
 
Sorting algorithms v01
Sorting algorithms v01Sorting algorithms v01
Sorting algorithms v01
 
Data Structures - Lecture 8 [Sorting Algorithms]
Data Structures - Lecture 8 [Sorting Algorithms]Data Structures - Lecture 8 [Sorting Algorithms]
Data Structures - Lecture 8 [Sorting Algorithms]
 
3.1 bubble sort
3.1 bubble sort3.1 bubble sort
3.1 bubble sort
 
Algo PPT.pdf
Algo PPT.pdfAlgo PPT.pdf
Algo PPT.pdf
 
Sorting
SortingSorting
Sorting
 
DESIGN AND ANALYSIS OF ALGORITHM (DAA)
DESIGN AND ANALYSIS OF ALGORITHM (DAA)DESIGN AND ANALYSIS OF ALGORITHM (DAA)
DESIGN AND ANALYSIS OF ALGORITHM (DAA)
 
Chapter 8 advanced sorting and hashing for print
Chapter 8 advanced sorting and hashing for printChapter 8 advanced sorting and hashing for print
Chapter 8 advanced sorting and hashing for print
 
Bs,qs,divide and conquer 1
Bs,qs,divide and conquer 1Bs,qs,divide and conquer 1
Bs,qs,divide and conquer 1
 
Bubble sort
Bubble sortBubble sort
Bubble sort
 
Everyday Math And Algorithms Ppt July 06
Everyday Math And Algorithms Ppt July 06Everyday Math And Algorithms Ppt July 06
Everyday Math And Algorithms Ppt July 06
 
Merge sort analysis and its real time applications
Merge sort analysis and its real time applicationsMerge sort analysis and its real time applications
Merge sort analysis and its real time applications
 
13_median.pdf
13_median.pdf13_median.pdf
13_median.pdf
 
Unit ii divide and conquer -1
Unit ii divide and conquer -1Unit ii divide and conquer -1
Unit ii divide and conquer -1
 
W1-L1 Negative-numbers-ppt..pptx
W1-L1 Negative-numbers-ppt..pptxW1-L1 Negative-numbers-ppt..pptx
W1-L1 Negative-numbers-ppt..pptx
 
Unit 7 sorting
Unit   7 sortingUnit   7 sorting
Unit 7 sorting
 
Ppt bubble sort
Ppt bubble sortPpt bubble sort
Ppt bubble sort
 
Sorting in data structures and algorithms , it has all the necessary points t...
Sorting in data structures and algorithms , it has all the necessary points t...Sorting in data structures and algorithms , it has all the necessary points t...
Sorting in data structures and algorithms , it has all the necessary points t...
 

Mehr von MuhammadUmerIhtisham

Lect 13, 14 (final)AVL Tree and Rotations.pdf
Lect 13, 14 (final)AVL Tree and Rotations.pdfLect 13, 14 (final)AVL Tree and Rotations.pdf
Lect 13, 14 (final)AVL Tree and Rotations.pdfMuhammadUmerIhtisham
 
LEC 7-DS ALGO(expression and huffman).pdf
LEC 7-DS  ALGO(expression and huffman).pdfLEC 7-DS  ALGO(expression and huffman).pdf
LEC 7-DS ALGO(expression and huffman).pdfMuhammadUmerIhtisham
 
SMIU Discrete Structure Lecture 3 Section 3E.pdf
SMIU Discrete Structure Lecture 3 Section 3E.pdfSMIU Discrete Structure Lecture 3 Section 3E.pdf
SMIU Discrete Structure Lecture 3 Section 3E.pdfMuhammadUmerIhtisham
 
Discrete Structure Lecture #5 & 6.pdf
Discrete Structure Lecture #5 & 6.pdfDiscrete Structure Lecture #5 & 6.pdf
Discrete Structure Lecture #5 & 6.pdfMuhammadUmerIhtisham
 
Discrete Structure Lecture #7 & 8.pdf
Discrete Structure Lecture #7 & 8.pdfDiscrete Structure Lecture #7 & 8.pdf
Discrete Structure Lecture #7 & 8.pdfMuhammadUmerIhtisham
 
SMIU Lecture #1 & 2 Introduction to Discrete Structure and Truth Table.pdf
SMIU Lecture #1 & 2 Introduction to Discrete Structure and Truth Table.pdfSMIU Lecture #1 & 2 Introduction to Discrete Structure and Truth Table.pdf
SMIU Lecture #1 & 2 Introduction to Discrete Structure and Truth Table.pdfMuhammadUmerIhtisham
 

Mehr von MuhammadUmerIhtisham (15)

LECT 10, 11-DSALGO(Hashing).pdf
LECT 10, 11-DSALGO(Hashing).pdfLECT 10, 11-DSALGO(Hashing).pdf
LECT 10, 11-DSALGO(Hashing).pdf
 
LEC 12-DSALGO-GRAPHS(final12).pdf
LEC 12-DSALGO-GRAPHS(final12).pdfLEC 12-DSALGO-GRAPHS(final12).pdf
LEC 12-DSALGO-GRAPHS(final12).pdf
 
Lect 13, 14 (final)AVL Tree and Rotations.pdf
Lect 13, 14 (final)AVL Tree and Rotations.pdfLect 13, 14 (final)AVL Tree and Rotations.pdf
Lect 13, 14 (final)AVL Tree and Rotations.pdf
 
LEC4-DS ALGO.pdf
LEC4-DS  ALGO.pdfLEC4-DS  ALGO.pdf
LEC4-DS ALGO.pdf
 
LEC3-DS ALGO(updated).pdf
LEC3-DS  ALGO(updated).pdfLEC3-DS  ALGO(updated).pdf
LEC3-DS ALGO(updated).pdf
 
LEC 7-DS ALGO(expression and huffman).pdf
LEC 7-DS  ALGO(expression and huffman).pdfLEC 7-DS  ALGO(expression and huffman).pdf
LEC 7-DS ALGO(expression and huffman).pdf
 
LEC 8-DS ALGO(heaps).pdf
LEC 8-DS  ALGO(heaps).pdfLEC 8-DS  ALGO(heaps).pdf
LEC 8-DS ALGO(heaps).pdf
 
LEC 5-DS ALGO(updated).pdf
LEC 5-DS  ALGO(updated).pdfLEC 5-DS  ALGO(updated).pdf
LEC 5-DS ALGO(updated).pdf
 
LEC 6-DS ALGO(updated).pdf
LEC 6-DS  ALGO(updated).pdfLEC 6-DS  ALGO(updated).pdf
LEC 6-DS ALGO(updated).pdf
 
lect 1-ds algo(final)_2.pdf
lect 1-ds  algo(final)_2.pdflect 1-ds  algo(final)_2.pdf
lect 1-ds algo(final)_2.pdf
 
lect 2-DS ALGO(online).pdf
lect 2-DS  ALGO(online).pdflect 2-DS  ALGO(online).pdf
lect 2-DS ALGO(online).pdf
 
SMIU Discrete Structure Lecture 3 Section 3E.pdf
SMIU Discrete Structure Lecture 3 Section 3E.pdfSMIU Discrete Structure Lecture 3 Section 3E.pdf
SMIU Discrete Structure Lecture 3 Section 3E.pdf
 
Discrete Structure Lecture #5 & 6.pdf
Discrete Structure Lecture #5 & 6.pdfDiscrete Structure Lecture #5 & 6.pdf
Discrete Structure Lecture #5 & 6.pdf
 
Discrete Structure Lecture #7 & 8.pdf
Discrete Structure Lecture #7 & 8.pdfDiscrete Structure Lecture #7 & 8.pdf
Discrete Structure Lecture #7 & 8.pdf
 
SMIU Lecture #1 & 2 Introduction to Discrete Structure and Truth Table.pdf
SMIU Lecture #1 & 2 Introduction to Discrete Structure and Truth Table.pdfSMIU Lecture #1 & 2 Introduction to Discrete Structure and Truth Table.pdf
SMIU Lecture #1 & 2 Introduction to Discrete Structure and Truth Table.pdf
 

Kürzlich hochgeladen

POINT- BIOCHEMISTRY SEM 2 ENZYMES UNIT 5.pptx
POINT- BIOCHEMISTRY SEM 2 ENZYMES UNIT 5.pptxPOINT- BIOCHEMISTRY SEM 2 ENZYMES UNIT 5.pptx
POINT- BIOCHEMISTRY SEM 2 ENZYMES UNIT 5.pptxSayali Powar
 
1029-Danh muc Sach Giao Khoa khoi 6.pdf
1029-Danh muc Sach Giao Khoa khoi  6.pdf1029-Danh muc Sach Giao Khoa khoi  6.pdf
1029-Danh muc Sach Giao Khoa khoi 6.pdfQucHHunhnh
 
Disha NEET Physics Guide for classes 11 and 12.pdf
Disha NEET Physics Guide for classes 11 and 12.pdfDisha NEET Physics Guide for classes 11 and 12.pdf
Disha NEET Physics Guide for classes 11 and 12.pdfchloefrazer622
 
Interactive Powerpoint_How to Master effective communication
Interactive Powerpoint_How to Master effective communicationInteractive Powerpoint_How to Master effective communication
Interactive Powerpoint_How to Master effective communicationnomboosow
 
CARE OF CHILD IN INCUBATOR..........pptx
CARE OF CHILD IN INCUBATOR..........pptxCARE OF CHILD IN INCUBATOR..........pptx
CARE OF CHILD IN INCUBATOR..........pptxGaneshChakor2
 
Student login on Anyboli platform.helpin
Student login on Anyboli platform.helpinStudent login on Anyboli platform.helpin
Student login on Anyboli platform.helpinRaunakKeshri1
 
Separation of Lanthanides/ Lanthanides and Actinides
Separation of Lanthanides/ Lanthanides and ActinidesSeparation of Lanthanides/ Lanthanides and Actinides
Separation of Lanthanides/ Lanthanides and ActinidesFatimaKhan178732
 
Advanced Views - Calendar View in Odoo 17
Advanced Views - Calendar View in Odoo 17Advanced Views - Calendar View in Odoo 17
Advanced Views - Calendar View in Odoo 17Celine George
 
1029 - Danh muc Sach Giao Khoa 10 . pdf
1029 -  Danh muc Sach Giao Khoa 10 . pdf1029 -  Danh muc Sach Giao Khoa 10 . pdf
1029 - Danh muc Sach Giao Khoa 10 . pdfQucHHunhnh
 
Grant Readiness 101 TechSoup and Remy Consulting
Grant Readiness 101 TechSoup and Remy ConsultingGrant Readiness 101 TechSoup and Remy Consulting
Grant Readiness 101 TechSoup and Remy ConsultingTechSoup
 
Accessible design: Minimum effort, maximum impact
Accessible design: Minimum effort, maximum impactAccessible design: Minimum effort, maximum impact
Accessible design: Minimum effort, maximum impactdawncurless
 
A Critique of the Proposed National Education Policy Reform
A Critique of the Proposed National Education Policy ReformA Critique of the Proposed National Education Policy Reform
A Critique of the Proposed National Education Policy ReformChameera Dedduwage
 
Z Score,T Score, Percential Rank and Box Plot Graph
Z Score,T Score, Percential Rank and Box Plot GraphZ Score,T Score, Percential Rank and Box Plot Graph
Z Score,T Score, Percential Rank and Box Plot GraphThiyagu K
 
Kisan Call Centre - To harness potential of ICT in Agriculture by answer farm...
Kisan Call Centre - To harness potential of ICT in Agriculture by answer farm...Kisan Call Centre - To harness potential of ICT in Agriculture by answer farm...
Kisan Call Centre - To harness potential of ICT in Agriculture by answer farm...Krashi Coaching
 
Web & Social Media Analytics Previous Year Question Paper.pdf
Web & Social Media Analytics Previous Year Question Paper.pdfWeb & Social Media Analytics Previous Year Question Paper.pdf
Web & Social Media Analytics Previous Year Question Paper.pdfJayanti Pande
 
Ecosystem Interactions Class Discussion Presentation in Blue Green Lined Styl...
Ecosystem Interactions Class Discussion Presentation in Blue Green Lined Styl...Ecosystem Interactions Class Discussion Presentation in Blue Green Lined Styl...
Ecosystem Interactions Class Discussion Presentation in Blue Green Lined Styl...fonyou31
 
APM Welcome, APM North West Network Conference, Synergies Across Sectors
APM Welcome, APM North West Network Conference, Synergies Across SectorsAPM Welcome, APM North West Network Conference, Synergies Across Sectors
APM Welcome, APM North West Network Conference, Synergies Across SectorsAssociation for Project Management
 
The basics of sentences session 2pptx copy.pptx
The basics of sentences session 2pptx copy.pptxThe basics of sentences session 2pptx copy.pptx
The basics of sentences session 2pptx copy.pptxheathfieldcps1
 

Kürzlich hochgeladen (20)

POINT- BIOCHEMISTRY SEM 2 ENZYMES UNIT 5.pptx
POINT- BIOCHEMISTRY SEM 2 ENZYMES UNIT 5.pptxPOINT- BIOCHEMISTRY SEM 2 ENZYMES UNIT 5.pptx
POINT- BIOCHEMISTRY SEM 2 ENZYMES UNIT 5.pptx
 
1029-Danh muc Sach Giao Khoa khoi 6.pdf
1029-Danh muc Sach Giao Khoa khoi  6.pdf1029-Danh muc Sach Giao Khoa khoi  6.pdf
1029-Danh muc Sach Giao Khoa khoi 6.pdf
 
Disha NEET Physics Guide for classes 11 and 12.pdf
Disha NEET Physics Guide for classes 11 and 12.pdfDisha NEET Physics Guide for classes 11 and 12.pdf
Disha NEET Physics Guide for classes 11 and 12.pdf
 
Interactive Powerpoint_How to Master effective communication
Interactive Powerpoint_How to Master effective communicationInteractive Powerpoint_How to Master effective communication
Interactive Powerpoint_How to Master effective communication
 
CARE OF CHILD IN INCUBATOR..........pptx
CARE OF CHILD IN INCUBATOR..........pptxCARE OF CHILD IN INCUBATOR..........pptx
CARE OF CHILD IN INCUBATOR..........pptx
 
Student login on Anyboli platform.helpin
Student login on Anyboli platform.helpinStudent login on Anyboli platform.helpin
Student login on Anyboli platform.helpin
 
Separation of Lanthanides/ Lanthanides and Actinides
Separation of Lanthanides/ Lanthanides and ActinidesSeparation of Lanthanides/ Lanthanides and Actinides
Separation of Lanthanides/ Lanthanides and Actinides
 
Mattingly "AI & Prompt Design: Structured Data, Assistants, & RAG"
Mattingly "AI & Prompt Design: Structured Data, Assistants, & RAG"Mattingly "AI & Prompt Design: Structured Data, Assistants, & RAG"
Mattingly "AI & Prompt Design: Structured Data, Assistants, & RAG"
 
Advanced Views - Calendar View in Odoo 17
Advanced Views - Calendar View in Odoo 17Advanced Views - Calendar View in Odoo 17
Advanced Views - Calendar View in Odoo 17
 
1029 - Danh muc Sach Giao Khoa 10 . pdf
1029 -  Danh muc Sach Giao Khoa 10 . pdf1029 -  Danh muc Sach Giao Khoa 10 . pdf
1029 - Danh muc Sach Giao Khoa 10 . pdf
 
Grant Readiness 101 TechSoup and Remy Consulting
Grant Readiness 101 TechSoup and Remy ConsultingGrant Readiness 101 TechSoup and Remy Consulting
Grant Readiness 101 TechSoup and Remy Consulting
 
Accessible design: Minimum effort, maximum impact
Accessible design: Minimum effort, maximum impactAccessible design: Minimum effort, maximum impact
Accessible design: Minimum effort, maximum impact
 
A Critique of the Proposed National Education Policy Reform
A Critique of the Proposed National Education Policy ReformA Critique of the Proposed National Education Policy Reform
A Critique of the Proposed National Education Policy Reform
 
Z Score,T Score, Percential Rank and Box Plot Graph
Z Score,T Score, Percential Rank and Box Plot GraphZ Score,T Score, Percential Rank and Box Plot Graph
Z Score,T Score, Percential Rank and Box Plot Graph
 
Kisan Call Centre - To harness potential of ICT in Agriculture by answer farm...
Kisan Call Centre - To harness potential of ICT in Agriculture by answer farm...Kisan Call Centre - To harness potential of ICT in Agriculture by answer farm...
Kisan Call Centre - To harness potential of ICT in Agriculture by answer farm...
 
Web & Social Media Analytics Previous Year Question Paper.pdf
Web & Social Media Analytics Previous Year Question Paper.pdfWeb & Social Media Analytics Previous Year Question Paper.pdf
Web & Social Media Analytics Previous Year Question Paper.pdf
 
Ecosystem Interactions Class Discussion Presentation in Blue Green Lined Styl...
Ecosystem Interactions Class Discussion Presentation in Blue Green Lined Styl...Ecosystem Interactions Class Discussion Presentation in Blue Green Lined Styl...
Ecosystem Interactions Class Discussion Presentation in Blue Green Lined Styl...
 
APM Welcome, APM North West Network Conference, Synergies Across Sectors
APM Welcome, APM North West Network Conference, Synergies Across SectorsAPM Welcome, APM North West Network Conference, Synergies Across Sectors
APM Welcome, APM North West Network Conference, Synergies Across Sectors
 
The basics of sentences session 2pptx copy.pptx
The basics of sentences session 2pptx copy.pptxThe basics of sentences session 2pptx copy.pptx
The basics of sentences session 2pptx copy.pptx
 
Mattingly "AI & Prompt Design: The Basics of Prompt Design"
Mattingly "AI & Prompt Design: The Basics of Prompt Design"Mattingly "AI & Prompt Design: The Basics of Prompt Design"
Mattingly "AI & Prompt Design: The Basics of Prompt Design"
 

LECT 9-DS ALGO(sorting).pdf

  • 1. Data Structures & Algorithm CS-102 Lecture 9 Elementary Sorting & NlogN Algorithms Lecturer: Syeda Nazia Ashraf 1
  • 2. Elementary Sorting Algorithms • Selection Sort • Insertion Sort • Bubble Sort 2
  • 3. Selection Sort • Main idea: –find the smallest element –put it in the first position –find the next smallest element –put it in the second position –… • And so on, until you get to the end of the list 3
  • 4. Selection Sort: Idea 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.
  • 5. Selection Sort: Cont’d 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.
  • 6. 1 2 3 0 7 12 19 5 a: 19 1 2 3 0 7 19 12 5 a: 12 1 2 3 0 19 7 12 5 a: 7 Selection Sort -- Example 1 2 3 0 7 12 19 5 a: 1 2 3 0 5 7 12 19 a: a: 1 2 3 0 5
  • 7. Swap Action (SelectionSorting) 5 8 20 10 7 5 7 20 10 8 5 7 8 10 20 20 8 5 10 7 5 7 8 10 20
  • 8. Selection Sort 5 1 3 4 6 2 Comparison Data Movement Sorted
  • 9. Selection Sort 5 1 3 4 6 2 Comparison Data Movement Sorted
  • 10. Selection Sort 5 1 3 4 6 2 Comparison Data Movement Sorted
  • 11. Selection Sort 5 1 3 4 6 2 Comparison Data Movement Sorted
  • 12. Selection Sort 5 1 3 4 6 2 Comparison Data Movement Sorted
  • 13. Selection Sort 5 1 3 4 6 2 Comparison Data Movement Sorted
  • 14. Selection Sort 5 1 3 4 6 2 Comparison Data Movement Sorted
  • 15. Selection Sort 5 1 3 4 6 2 Comparison Data Movement Sorted  Largest
  • 16. Selection Sort 5 1 3 4 2 6 Comparison Data Movement Sorted
  • 17. Selection Sort 5 1 3 4 2 6 Comparison Data Movement Sorted
  • 18. Selection Sort 5 1 3 4 2 6 Comparison Data Movement Sorted
  • 19. Selection Sort 5 1 3 4 2 6 Comparison Data Movement Sorted
  • 20. Selection Sort 5 1 3 4 2 6 Comparison Data Movement Sorted
  • 21. Selection Sort 5 1 3 4 2 6 Comparison Data Movement Sorted
  • 22. Selection Sort 5 1 3 4 2 6 Comparison Data Movement Sorted
  • 23. Selection Sort 5 1 3 4 2 6 Comparison Data Movement Sorted  Largest
  • 24. Selection Sort 2 1 3 4 5 6 Comparison Data Movement Sorted
  • 25. Selection Sort 2 1 3 4 5 6 Comparison Data Movement Sorted
  • 26. Selection Sort 2 1 3 4 5 6 Comparison Data Movement Sorted
  • 27. Selection Sort 2 1 3 4 5 6 Comparison Data Movement Sorted
  • 28. Selection Sort 2 1 3 4 5 6 Comparison Data Movement Sorted
  • 29. Selection Sort 2 1 3 4 5 6 Comparison Data Movement Sorted
  • 30. Selection Sort 2 1 3 4 5 6 Comparison Data Movement Sorted  Largest
  • 31. Selection Sort 2 1 3 4 5 6 Comparison Data Movement Sorted
  • 32. Selection Sort 2 1 3 4 5 6 Comparison Data Movement Sorted
  • 33. Selection Sort 2 1 3 4 5 6 Comparison Data Movement Sorted
  • 34. Selection Sort 2 1 3 4 5 6 Comparison Data Movement Sorted
  • 35. Selection Sort 2 1 3 4 5 6 Comparison Data Movement Sorted
  • 36. Selection Sort 2 1 3 4 5 6 Comparison Data Movement Sorted  Largest
  • 37. Selection Sort 2 1 3 4 5 6 Comparison Data Movement Sorted
  • 38. Selection Sort 2 1 3 4 5 6 Comparison Data Movement Sorted
  • 39. Selection Sort 2 1 3 4 5 6 Comparison Data Movement Sorted
  • 40. Selection Sort 2 1 3 4 5 6 Comparison Data Movement Sorted
  • 41. Selection Sort 2 1 3 4 5 6 Comparison Data Movement Sorted  Largest
  • 42. Selection Sort 1 2 3 4 5 6 Comparison Data Movement Sorted
  • 43. Selection Sort 1 2 3 4 5 6 Comparison Data Movement Sorted DONE!
  • 44. 42 40 2 1 3 3 4 0 -1 65 58 43 40 2 1 43 3 4 0 -1 42 65 58 3 40 2 1 43 3 4 0 -1 58 3 65 42 40 2 1 43 3 65 0 -1 58 3 42 4 Selection Sort: Example
  • 45. 42 40 2 1 3 3 4 0 65 58 43 -1 42 -1 2 1 3 3 4 0 65 58 43 40 42 -1 2 1 3 3 4 65 58 43 40 0 42 -1 2 1 0 3 4 65 58 43 40 3 Selection Sort: Example
  • 46. 1 42 -1 2 1 3 4 65 58 43 40 3 0 42 -1 0 3 4 65 58 43 40 3 2 1 42 -1 0 3 4 65 58 43 40 3 2 1 42 0 3 4 65 58 43 40 3 2 -1 1 42 0 3 4 65 58 43 40 3 2 -1 Selection Sort: Example
  • 47. Insertion Sort • Basic idea (sorting cards): – Starts by considering the first two elements of the array data, if out of order, swap them – Consider the third element, insert it into the proper position among the first three elements. – Consider the forth element, insert it into the proper position among the first four elements. – … …
  • 48. Insertion Sort -- Example 1 2 3 0 12 5 7 19 a: 1 2 3 0 19 5 7 12 a: 1 2 3 0 12 19 7 5 a: 1 2 3 0 7 12 19 5 a:
  • 49. Insertion Sort -- animation 1 2 3 0 12 5 7 19 a: 1 2 3 0 19 5 7 19 a: count val pos 1 12 0 1 12 -1 1 2 3 0 5 7 19 a: 12 19 1 2 3 0 19 5 7 12 a: 12
  • 50. Insertion Sort -- animation (cont) 1 2 3 0 12 19 7 5 a: count val pos 2 5 1 2 5 -1 1 2 3 0 19 5 7 12 a: 1 2 3 0 5 7 12 a: 19 19 1 2 3 0 19 7 12 a: 19 12 2 5 0 1 2 3 0 12 19 7 12 a: 5
  • 51. Insertion Sort -- animation (cont) 1 2 3 0 12 19 7 5 a: count val pos 3 7 2 3 7 0 3 7 1 1 2 3 0 12 19 7 5 a: 19 1 2 3 0 12 19 19 5 a: 12 1 2 3 0 12 12 19 5 a: 7 1 2 3 0 7 12 19 5 a:
  • 52. Bubble Sort • Basic idea (lighter bubbles rise to the top): – Exchange neighbouring items until the largest item reaches the end of the array – Repeat for the rest of the array • Also called Sink Sort.
  • 53. 1 2 3 0 12 7 19 5 a: Bubble Sort -- Example 1 2 3 0 12 7 19 5 a: 19 12 7 5 1 2 3 0 a: 12 19 7 5 a: 1 2 3 0 5 12 7 19 1 2 3 0 a: a: 1 2 3 0 12 7 19 5 a: 1 2 3 0 7 12 19 5 a: 1 2 3 0 7 12 19 5 a: 1 2 3 0 7 12 19 5 a: 1 2 3 0 7 12 19 5 a:
  • 54. Bubble Sort Example 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, 12 The 12 is greater than the 7 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 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. 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. 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.
  • 55. Bubble Sort Example 6, 2, 9, 11, 9, 3, 7, 12 2, 6, 9, 11, 9, 3, 7, 12 2, 6, 9, 9, 11, 3, 7, 12 2, 6, 9, 9, 3, 11, 7, 12 2, 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. First Pass Second Pass
  • 56. Bubble Sort Example 2, 6, 9, 9, 3, 7, 11, 12 2, 6, 9, 3, 9, 7, 11, 12 2, 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.
  • 57. Bubble Sort Example 2, 6, 9, 3, 7, 9, 11, 12 2, 6, 3, 9, 7, 9, 11, 12 2, 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. 2, 6, 9, 3, 7, 9, 11, 12 Fourth Pass
  • 58. Bubble Sort Example 2, 6, 3, 7, 9, 9, 11, 12 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 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, 12 Fourth Pass 2, 6, 3, 7, 9, 9, 11, 12 Fifth Pass
  • 59. Bubble Sort Example 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, 12 Fourth Pass 2, 6, 3, 7, 9, 9, 11, 12 Fifth 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.
  • 60. Bubble Sort: Example 40 2 1 43 3 65 0 -1 58 3 42 4 65 2 1 40 3 43 0 -1 58 3 42 4 65 58 1 2 3 40 0 -1 43 3 42 4 1 2 3 40 0 65 -1 43 58 3 42 4 1 2 3 4 • Notice that at least one element will be in the correct position each iteration.
  • 61. 1 0 -1 3 2 65 3 43 58 42 40 4 Bubble Sort: Example 0 -1 1 2 65 3 43 58 42 40 4 3 -1 0 1 2 65 3 43 58 42 40 4 3 6 7 8 1 2 0 3 -1 3 40 65 43 58 42 4 5
  • 62. Summary • Insertion, Selection and Bubble sort: – Worst case time complexity is proportional to N2. Bestsorting routines are N log(N)
  • 63. NLogN Algorithms • Divide and Conquer • Merge Sort • Quick Sort • Heap Sort
  • 64. Divide and Conquer What if we split the list into two parts? 10 12 8 4 2 11 7 5 10 12 8 4 2 11 7 5 64
  • 65. Divide and Conquer Sort the two parts: 10 12 8 4 2 11 7 5 4 8 10 12 2 5 7 11 65
  • 66. Divide and Conquer Then merge the two parts together: 4 8 10 12 2 5 7 11 2 4 5 7 8 10 11 12 66
  • 67. Divide and Conquer • Why not divide the halves in half? • The quarters in half? • And so on . . . • When should we stop? At n = 1 67
  • 69. Sort Divide and Conquer Sort Sort Sort Sort Sort Sort 69
  • 71. Mergesort • Mergesort is a divide and conquer algorithm that does exactly that. • It splits the list in half • Mergesorts the two halves • Then merges the two sorted halves together • Mergesort can be implemented recursively 71
  • 72. Mergesort • The mergesort algorithm involves three steps: – If the number of items to sort is 0 or 1, return – Recursively sort the first and second halves separately – Merge the two sorted halves into a sorted group 72
  • 73. Merging: animation 4 8 10 12 2 5 7 11 2 73
  • 74. Merging: animation 4 8 10 12 2 5 7 11 2 4 74
  • 75. Merging: animation 4 8 10 12 2 5 7 11 2 4 5 75
  • 76. Merging 4 8 10 12 2 5 7 11 2 4 5 7 76
  • 77. Mergesort 8 12 11 2 7 5 4 10 Split the list in half. 8 12 4 10 Mergesort the left half. Split the list in half. Mergesort the left half. 4 10 Split the list in half. Mergesort the left half. 10 Mergesort the right. 4 77
  • 78. Mergesort 8 12 11 2 7 5 4 10 8 12 4 10 4 10 Mergesort the right half. Merge the two halves. 10 4 8 12 12 8 Merge the two halves. 8 8 12 78
  • 79. Mergesort 8 12 11 2 7 5 4 10 8 12 4 10 Merge the two halves. 4 10 Mergesort the right half. Merge the two halves. 10 4 8 12 10 12 8 4 10 4 8 12 79
  • 80. Mergesort 10 12 11 2 7 5 8 4 Mergesort the right half. 11 2 7 5 11 2 11 2 80
  • 81. Mergesort 10 12 11 2 7 5 8 4 Mergesort the right half. 11 2 7 5 11 2 2 11 2 11 81
  • 82. Mergesort 10 12 11 2 7 5 8 4 Mergesort the right half. 11 2 7 5 2 11 7 5 7 5 82
  • 83. Mergesort 10 12 11 2 7 5 8 4 Mergesort the right half. 11 2 5 7 2 11 83
  • 84. Mergesort 10 12 2 5 7 11 8 4 Mergesort the right half. 84
  • 85. Mergesort 5 7 8 10 11 12 4 2 Merge the two halves. 85
  • 86. 3 5 15 28 30 6 10 14 22 43 50 a: b: aSize: 5 bSize: 6 mergeArrays tmp:
  • 87. mergeArrays 5 15 28 30 10 14 22 43 50 a: b: tmp: i=0 k=0 j=0 3 6
  • 88. mergeArrays 5 15 28 30 10 14 22 43 50 a: b: tmp: i=0 k=0 3 j=0 3 6
  • 89. mergeArrays 3 15 28 30 10 14 22 43 50 a: b: tmp: i=1 j=0 k=1 3 5 5 6
  • 90. mergeArrays 3 5 28 30 10 14 22 43 50 a: b: 3 5 tmp: i=2 j=0 k=2 6 15 6
  • 91. mergeArrays 3 5 28 30 6 14 22 43 50 a: b: 3 5 6 tmp: i=2 j=1 k=3 15 10 10
  • 92. 10 mergeArrays 3 5 28 30 6 22 43 50 a: b: 3 5 6 tmp: i=2 j=2 k=4 15 10 14 14
  • 93. 14 10 mergeArrays 3 5 28 30 6 14 43 50 a: b: 3 5 6 tmp: i=2 j=3 k=5 15 10 22 15
  • 94. 14 10 mergeArrays 3 5 30 6 14 43 50 a: b: 3 5 6 tmp: i=3 j=3 k=6 15 10 22 22 15 28
  • 95. 14 10 mergeArrays 3 5 30 6 14 50 a: b: 3 5 6 tmp: i=3 j=4 k=7 15 10 22 28 15 28 43 22
  • 96. 14 10 mergeArrays 3 5 6 14 50 a: b: 3 5 6 tmp: i=4 j=4 k=8 15 10 22 30 15 28 43 22 30 28
  • 97. 14 10 mergeArrays 3 5 6 14 50 a: b: 3 5 6 30 tmp: i=5 j=4 k=9 15 10 22 15 28 43 22 30 28 43 50 Done.
  • 98. Merge Sort Algorithm (Cont.) Merge Sort(A) [splitting] { n= length(A) [store no. of elements in A] if(n < 2) [BASE or EXIT CONDITION: if only one element in array, then return and stop partitioning /recursion] return mid = n/2 left= array of size(mid) right= array of size(n-mid) for(i = 0 to mid-1) left[i] = A[i] for(i = mid to n-1) right[i-mid] = A[i] 98 Merge Sort(left) Merge Sort(right) Merge(left, right, A) } [recursive call] [Merge Left and Right Sub lists into A]
  • 99. Merge Sort Algorithm Merge(L, R, A) [merge sorted halves] { nL=Length(L) [no. of elements in left] nR=Length(R) [no. of elements in left] i=0 j=0 k=0 while(i < nL && j < nR) { if(L[i]) <= R[j] { A[k] = L[i] i = i+1 } else 99 { A[k] = R[j] j = j+1 } k = k+1 } while (i < nL) { A[k]=L[i] i=i+1 k=k+1 } while (j < nR) { A[k]=R[j] j=j+1 k=k+1 } } [For leftover L and R elements for fill remaining positions of k]
  • 100. Mergesort Analysis Merging the two lists of size n/2: O(n) Merging the four lists of size n/4: O(n) . . . Merging the n lists of size 1: O(n) O (log n) times 100
  • 101. Mergesort Analysis • Mergesort is O(n log n) • Space? • The other sorts we have looked at (insertion, selection) are in-place (only require a constant amount of extra space) • Mergesort requires O(n) extra space for merging 101
  • 102. Quicksort • Quicksort is another divide and conquer algorithm • Quicksort is based on the idea of partitioning (splitting) the list around a pivot or split value 102
  • 103. Quicksort • Quicksort is a divide and conquer algorithm. Quicksort first divides a large array into two smaller sub-arrays: the low elements and the high elements. Quicksort can then recursively sort the sub-arrays. • The steps are: • Pick an element, called a pivot, from the array. • Reorder the array so that all elements with values less than the pivot come before the pivot, while all elements with values greater than the pivot come after it (equal values can go either way). After this partitioning, the pivot is in its final position. This is called the partition operation. • Recursively apply the above steps to the sub-array of elements with smaller values and separately to the sub-array of elements with greater values. 103
  • 104. Quicksort 104 Initial Step - First Partition Sort Left Partition in the same way
  • 105. Quicksort First the list is partitioned around a pivot value. Pivot can be chosen from the beginning, end or middle of list): 8 3 2 11 7 5 4 10 12 4 5 5 pivot value 105
  • 106. Quicksort The pivot is swapped to the last position and the remaining elements are compared starting at the ends. 8 3 2 11 7 5 4 10 12 4 5 low high 5 pivot value 106 4 < 5 Move right 7>5 Move left
  • 107. Quicksort Then the low index moves right until it is at an element that is larger than the pivot value (i.e., it is on the wrong side) 8 6 2 11 7 5 10 12 4 6 low high 5 pivot value 3 12 107 7>5 move left 12!<5 stop
  • 108. Quicksort Then the high index moves left until it is at an element that is smaller than the pivot value (i.e., it is on the wrong side) 8 6 2 11 7 5 4 10 12 4 6 low high 5 pivot value 3 2 108 11>5 move left 2!>5 stop
  • 109. Quicksort Then the two values are swapped and the index values are updated: 8 6 2 11 7 5 4 10 12 4 6 low high 5 pivot value 3 2 12 109 10!<5 stop 3!>5 stop Interchange 2 and 12
  • 110. Quicksort This continues until the two index values pass each other: 8 6 12 11 7 5 4 2 4 6 low high 5 pivot value 3 10 3 10 110 3<5 Move 10>5 Move Interchange 10 and 3
  • 111. 111 Quicksort This continues until the two index values pass each other: 8 6 12 11 7 5 4 2 4 6 low high 5 pivot value 3 10 3 10 111 8!<5 Stop 10>5 Move Interchange 10 and 3
  • 112. 112 112 Quicksort This continues until the two index values pass each other: 8 6 12 11 7 5 4 2 4 6 lowhigh 5 pivot value 3 10 3 10 112 8>5 Move Interchange 10 and 3
  • 113. Quicksort The two index values pass each other: 8 6 12 11 7 5 4 2 4 6 low high 5 pivot value 10 3 113 3<5 8>5
  • 114. Quicksort Then the pivot value is swapped into position: 8 6 12 11 7 5 4 2 4 6 low high 10 3 8 5 114 Interchange 8 with pivot value 5
  • 115. Quicksort Recursively quicksort the two parts: 5 6 12 11 7 8 4 2 4 6 10 3 Quicksort the left part Quicksort the right part 5 115
  • 116. Quicksort 116 5 6 12 11 7 8 4 2 4 6 10 3 5 3 pivot value 8 pivot value Quicksort the left part Quicksort the right part 10!<8 stop 7!>8 stop high low low high 4!<3 2!>3
  • 117. Quicksort 117 5 6 12 11 10 8 4 4 2 6 7 3 5 3 pivot value 8 pivot value Quicksort the left part Quicksort the right part 12!<8 11>8 move high low low high Interchange 4 and 2 Interchange 10 and 7
  • 118. 118 Quicksort 118 5 6 12 11 10 8 4 4 2 6 7 3 5 3 pivot value 8 pivot value Quicksort the left part Quicksort the right part 12>8 Move high low low high Interchange 4 and 2
  • 119. 119 119 Quicksort 119 5 6 12 11 10 8 4 4 2 6 7 3 5 3 pivot value 8 pivot value Quicksort the left part Quicksort the right part 12!<8 high low low high Interchange 4 and 2 Interchange 10 and 7
  • 120. Quicksort 120 5 6 8 11 10 12 4 3 2 6 7 4 5 sorted Interchange 12 with Pivot value Interchange 4 with Pivot value sorted QuickSort the right part
  • 121. Quicksort 121 5 6 8 11 10 12 4 3 2 6 7 4 5 sorted sorted QuickSort the right part 12 pivot value low high 11<12 10!>12
  • 122. Quicksort 122 5 6 8 10 11 12 4 3 2 6 7 4 5 sorted
  • 123. Quick Sort Algorithm Quick Sort(A, start, end) [A is array, end is Pivot] { If (start < end) then [or if(start >=end) return] { Pindex = Partition(A, start, end) Quick Sort(A, start, Pindex-1 ) [Left first half- values before Pivot] Quick Sort(A, Pindex+1 , end ) [Right second half values after Pivot] } } 123
  • 124. Quick Sort Algorithm (Cont.) Partition(A, start, end) { Pivot = A[end] Pindex = start for(i=start to end-1) [i is current position, end-1 because end is Pivot] { if(A[i] <= Pivot) then [if current <= Pivot] { swap(A[i] , A[Pindex]) Pindex = Pindex+1 [index only change when swapping occurs] } } 124 swap(A[Pindex], A[end]) [for last swap of Index and Pivot] return(Pindex) }