0% found this document useful (0 votes)
2 views

Unit 7_Searching, Sorting, Hashing

Uploaded by

XYZ XYZ
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
2 views

Unit 7_Searching, Sorting, Hashing

Uploaded by

XYZ XYZ
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 128

Unit 6: Searching and Sorting

UNIT 7
SEARCHING, SORTING AND HASHING: 8 HOURS

DR. NEEPA SHAH 1

SYLLABUS CONTENT

 Searching: Linear Search, Binary Search and Fibonacci search.


 Sorting: Bubble Sort, Selection Sort, Heap Sort, Insertion Sort, Radix Sort, Merge Sort, Quick Sort.
 Analysis of Searching and Sorting Techniques.
 Hashing: Hash Functions, Overflow handling, Collision & Collision Resolution Techniques, Linear hashing, Hashing
with chaining, Separate Chaining, Open Addressing, Rehashing and Extendible hashing.

DR. NEEPA SHAH 2

Dr. Neepa Shah 1


Unit 6: Searching and Sorting

LINEAR SEARCH

 Unordered List
 One occurrence
 All occurrences

DR. NEEPA SHAH 3

ALGORITHM

 Linear Search ( Array Arr, Value a )


Step 1: Set i to 0
Step 2: if i > n then go to step 7
Step 3: if Arr[i] = a then go to step 6
Step 4: Set i to i + 1
Step 5: Goto step 2
Step 6: Print element a found at index i and go to step 8
Step 7: Print element not found
Step 8: Exit

DR. NEEPA SHAH 4

Dr. Neepa Shah 2


Unit 6: Searching and Sorting

LINEAR SEARCH
 Unordered List  All occurrences
 One occurrence (with function and without int search(int arr[], int n, int x)
function)
{
int search(int arr[], int n, int x)
int i, count = 0;
{
for (i = 0; i < n; i++)
int i;
{
for (i = 0; i < n; i++) if (arr[i] == x)
if (arr[i] == x) {
return i; cout <<x<<“ is present at location ”<<i+1<<endl;
return -1; count++;

} }
}
return count;
DR. NEEPA SHAH 5
}

BINARY SEARCH

 Given an integer X and integers A0 , A1 , …, AN-1, which are presorted, find i such that Ai = X, or
return i = -1 if X is not in the input.

6
DR. NEEPA SHAH 6

Dr. Neepa Shah 3


Unit 6: Searching and Sorting

BINARY SEARCH: EXAMPLE

7
DR. NEEPA SHAH 7

BINARY SEARCH: EXAMPLE CONTD.

8
DR. NEEPA SHAH 8

Dr. Neepa Shah 4


Unit 6: Searching and Sorting

ALGORITHM: ITERATIVE

 do until the pointers low and high meet each other.


mid = (low + high)/2
if (x = arr[mid])
return mid
else if (x > arr[mid]) // x is on the right side
low = mid + 1
else // x is on the left side
high = mid - 1

DR. NEEPA SHAH 9

BINARY SEARCH: ITERATIVE


int binarySearch(int []a, int x, int N)
{
int low = 0, high = N-1;
while (low <= high)
{
int mid = (low + high) / 2; // middle subscript
if (a[mid] < x)
low = mid + 1;
else if (x < a[mid])
high = mid – 1;
else
return mid; // found
}
return –1; // not found
}
Homework: Recursive function for binary search
10
DR. NEEPA SHAH 10

Dr. Neepa Shah 5


Unit 6: Searching and Sorting

BINARY SEARCH: RECURSIVE

binarySearch (arr, x, low, high)


if low > high
return False
else
mid = (low + high) / 2
if x == arr[mid]
return mid
else if x > arr[mid]
return binarySearch(arr, x, mid + 1, high)
else
return binarySearch(arr, x, low, mid - 1)
DR. NEEPA SHAH 11

BINARY SEARCH: ANALYSIS

 The work done inside the while loop clearly takes O (1) time, so the total running
time depends on the number of iterations of the while loop.
 Each iteration reduces the number of elements remaining to be considered by half,
so clearly the number of iterations required is k.Thus T (N ) = O (log N ).

DR. NEEPA SHAH 12 12

Dr. Neepa Shah 6


Unit 6: Searching and Sorting

BINARY SEARCH: ANALYSIS CONTD

DR. NEEPA SHAH 13 13

FIBONACCI SEARCH

 Divides given array into unequal parts


 Binary Search uses a division operator to
 Works for sorted arrays divide range. Fibonacci Search uses + and -.
 A Divide and Conquer Algorithm  Fibonacci Search examines relatively closer
elements in subsequent steps. So when the
 O(log n) time complexity
input array is big that cannot fit in CPU cache
or even in RAM, Fibonacci Search can be
useful.

DR. NEEPA SHAH 14

Dr. Neepa Shah 7


Unit 6: Searching and Sorting

FIBONACCI SEARCH PROCESS

 Find the immediate Fibonacci number that is greater than or equal to the size of the input array. Then, also hold the two
preceding numbers of the selected Fibonacci number.
 Initialize the offset value as -1
 Until Fm2 is greater than 0
 Compare the key element with the element at index [min(offset+Fm-2,n-1)]. If a match is found, return the index.
 If the key element is lesser value than this element, reduce the range of the input from 0 to the index of this element. The Fibonacci
numbers are also updated with Fm = Fm2.
 If the key is greater than the element at this index, remove the elements before this element from the search range. The Fibonacci
numbers are updated as Fm = Fm1.The offset value is set to the index of this element.
 As there are two 1s in the Fibonacci series, there arises a case where two preceding numbers will become 1. So if Fm1
becomes 1, there is only one element left in the array to be searched. Compare the key element with that element and
return the 1st index. Otherwise, the algorithm returns an unsuccessful search.

DR. NEEPA SHAH 15

FIBONACCI SEARCH ALGORITHM


n <- size of the input array
offset = -1 else if (A[i] > x) then:
Fm2 = 0
Fm = Fm2
Fm1= 1
Fm = Fm2 + Fm1 Fm1 = Fm1 - Fm2
while Fm < n do: Fm2 = Fm - Fm1
Fm2 = Fm1 end
Fm1 = Fm
else
Fm = Fm2 + Fm1
done return i;
while fm > 1 do: end
i = minimum of (offset + fm2, n – 1) done
if (A[i] < x) then:
if (Fm1 and Array[offset + 1] == x) then:
Fm = Fm1
Fm1 = Fm2 return offset + 1
Fm2 = Fm - Fm1 end
offset = i
return invalid location;
end

DR. NEEPA SHAH 16

Dr. Neepa Shah 8


Unit 6: Searching and Sorting

EXAMPLE

 Step 3:
 update the offset value and the Fibonacci numbers
 Offset = 4
 Fm = Fm1 = 8; Fm1 = 5; Fm2 = 3
 Step 1:
 Compare it with the element at index = minimum (offset + Fm2, n – 1) = 7.
 n = 10
 The smallest Fibonacci number greater than 10 is 13.
 Fm = 13, Fm1 = 8, Fm2 = 5
 offset = -1

 Step 2:
 index = minimum (offset + Fm2, n – 1) = minimum (-1 +
5, 9) = 4

DR. NEEPA SHAH 17

EXAMPLE CONTD.

 Step 4:
 discard the elements after the 7th index, so n = 7
 offset value remains 4
 Fibonacci numbers are pushed two steps backward, i.e. Fm = Fm2 = 3.
 Fm1 = 2, Fm2 = 1
 Compare it with the element at index = minimum (offset + Fm-2, n – 1) = 5.

DR. NEEPA SHAH 18

Dr. Neepa Shah 9


Unit 6: Searching and Sorting

EXAMPLE

DR. NEEPA SHAH 19

WHAT IS SORTING?

 Examples:
 Sorting Books in Library
 Sorting Individuals by Height (Feet and Inches)
 Sorting Movies in Blockbuster (Alphabetical)
 Sorting Numbers (Sequential)

DR. NEEPA SHAH 20

Dr. Neepa Shah 10


Unit 6: Searching and Sorting

CLASSIFICATION

 Sorting algorithms are often classified by:


 Computational Complexity
 Computational Complexity of Swaps
 Memory usage
 Recursion
 Stability

DR. NEEPA SHAH 21

SORTING ALGORITHMS
 Bubble Sort
 Selection Sort
 Insertion Sort
 Shell Sort
 Merge Sort
 Quick Sort
 Heap Sort
 Bucket Sort
 Radix Sort
DR. NEEPA SHAH 22

Dr. Neepa Shah 11


Unit 6: Searching and Sorting

BUBBLE SORT

 One of the simplest sorting algorithm

 Also known as Exchange sort

DR. NEEPA SHAH 23

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,
BUBBLE SORT EXAMPLE
9, 11, 9, 12, 3, 7
6, 2,
DR. NEEPA SHAH
9, 11, 9, 3, 12, 7 24

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

Dr. Neepa Shah 12


Unit 6: Searching and Sorting

BUBBLE SORT EXAMPLE CONTD.


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

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

DR. NEEPA SHAH 25

BUBBLE SORT EXAMPLE CONTD.


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

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

DR. NEEPA SHAH 26

Dr. Neepa Shah 13


Unit 6: Searching and Sorting

BUBBLE SORT EXAMPLE CONTD.


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

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

DR. NEEPA SHAH 27

BUBBLE SORT EXAMPLE CONTD.


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

Third Pass
2, 6, 9, 9, 3, 7, 11, 12
Fourth Pass
2, 6, 9, 3, 7, 9, 11, 12
Fifth Pass
2, 6, 3, 7, 9, 9, 11, 12
2, 6,
3, 3,
6, 7, 9, 9, 11, 12
DR. NEEPA SHAH 28

Dr. Neepa Shah 14


Unit 6: Searching and Sorting

BUBBLE SORT EXAMPLE CONTD.


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

Third Pass
2, 6, 9, 9, 3, 7, 11, 12
Fourth Pass
2, 6, 9, 3, 7, 9, 11, 12
Fifth Pass
2, 6, 3, 7, 9, 9, 11, 12
Sixth Pass
2, 3, 6, 7, 9, 9, 11, 12
DR. NEEPA SHAH
2, 3, 6, 7, 9, 9, 11, 12 29

BUBBLE SORT PROCEDURE

1. Do steps 2-3 for i= n-1 down to 1


2. Do step 3 for j=0 upto i-1
3. If the 2 consecutive elements sj and sj+1, are out of order swap them

DR. NEEPA SHAH 30

Dr. Neepa Shah 15


Unit 6: Searching and Sorting

BUBBLE SORT ALGORITHM

for(i = SIZE-1; i > 0; i--) {


for(i = 0; i < n; i++) {
for(j = 0; j < i; j++) {
for(j = 1; j < (n-i); j++) {
if(a[j] > a[j+1]) { if(a[j-1] > a[j]) {
t = a[j]; t = a[j-1];
a[j]=a[j+1]; a[j-1]=a[j];
a[j+1]=t; a[j]=t;
} }
}
}
}
}

DR. NEEPA SHAH 31

BUBBLE SORT

 Pros: Simplicity and ease of implementation

 Cons: Horribly Inefficient

DR. NEEPA SHAH 32

Dr. Neepa Shah 16


Unit 6: Searching and Sorting

USING A BOOLEAN “FLAG”

 We can use a Boolean variable to determine if any swapping occurred during the “bubble up.”

 If no swapping occurred, then we know that the collection is already sorted!

 This Boolean “flag” needs to be reset after each “bubble up.”

DR. NEEPA SHAH 33

AN EXAMPLE
N 8 did_swap true

to_do 7

index

98 23 45 14 6 67 33 42

1 2 3 4 5 6 7 8

DR. NEEPA SHAH 34

Dr. Neepa Shah 17


Unit 6: Searching and Sorting

AN EXAMPLE
N 8 did_swap false

to_do 7

index 1

98 23 45 14 6 67 33 42

1 2 3 4 5 6 7 8

DR. NEEPA SHAH 35

AN EXAMPLE
N 8 did_swap false

to_do 7

index 1

Swap

98 23 45 14 6 67 33 42

1 2 3 4 5 6 7 8

DR. NEEPA SHAH 36

Dr. Neepa Shah 18


Unit 6: Searching and Sorting

AN EXAMPLE
N 8 did_swap true

to_do 7

index 1

Swap

23 98 45 14 6 67 33 42

1 2 3 4 5 6 7 8

DR. NEEPA SHAH 37

AN EXAMPLE
N 8 did_swap true

to_do 7

index 2

23 98 45 14 6 67 33 42

1 2 3 4 5 6 7 8

DR. NEEPA SHAH 38

Dr. Neepa Shah 19


Unit 6: Searching and Sorting

AN EXAMPLE
N 8 did_swap true

to_do 7

index 2

Swap

23 98 45 14 6 67 33 42

1 2 3 4 5 6 7 8

DR. NEEPA SHAH 39

AN EXAMPLE
N 8 did_swap true

to_do 7

index 2

Swap

23 45 98 14 6 67 33 42

1 2 3 4 5 6 7 8

DR. NEEPA SHAH 40

Dr. Neepa Shah 20


Unit 6: Searching and Sorting

AN EXAMPLE
N 8 did_swap true

to_do 7

index 3

23 45 98 14 6 67 33 42

1 2 3 4 5 6 7 8

DR. NEEPA SHAH 41

AN EXAMPLE
N 8 did_swap true

to_do 7

index 3

Swap

23 45 98 14 6 67 33 42

1 2 3 4 5 6 7 8

DR. NEEPA SHAH 42

Dr. Neepa Shah 21


Unit 6: Searching and Sorting

AN EXAMPLE
N 8 did_swap true

to_do 7

index 3

Swap

23 45 14 98 6 67 33 42

1 2 3 4 5 6 7 8

DR. NEEPA SHAH 43

AN EXAMPLE
N 8 did_swap true

to_do 7

index 4

23 45 14 98 6 67 33 42

1 2 3 4 5 6 7 8

DR. NEEPA SHAH 44

Dr. Neepa Shah 22


Unit 6: Searching and Sorting

AN EXAMPLE
N 8 did_swap true

to_do 7

index 4

Swap

23 45 14 98 6 67 33 42

1 2 3 4 5 6 7 8

DR. NEEPA SHAH 45

AN EXAMPLE
N 8 did_swap true

to_do 7

index 4

Swap

23 45 14 6 98 67 33 42

1 2 3 4 5 6 7 8

DR. NEEPA SHAH 46

Dr. Neepa Shah 23


Unit 6: Searching and Sorting

AN EXAMPLE
N 8 did_swap true

to_do 7

index 5

23 45 14 6 98 67 33 42

1 2 3 4 5 6 7 8

DR. NEEPA SHAH 47

AN EXAMPLE
N 8 did_swap true

to_do 7

index 5

Swap

23 45 14 6 98 67 33 42

1 2 3 4 5 6 7 8

DR. NEEPA SHAH 48

Dr. Neepa Shah 24


Unit 6: Searching and Sorting

AN EXAMPLE
N 8 did_swap true

to_do 7

index 5

Swap

23 45 14 6 67 98 33 42

1 2 3 4 5 6 7 8

DR. NEEPA SHAH 49

AN EXAMPLE
N 8 did_swap true

to_do 7

index 6

23 45 14 6 67 98 33 42

1 2 3 4 5 6 7 8

DR. NEEPA SHAH 50

Dr. Neepa Shah 25


Unit 6: Searching and Sorting

AN EXAMPLE
N 8 did_swap true

to_do 7

index 6

Swap

23 45 14 6 67 98 33 42

1 2 3 4 5 6 7 8

DR. NEEPA SHAH 51

AN EXAMPLE
N 8 did_swap true

to_do 7

index 6

Swap

23 45 14 6 67 33 98 42

1 2 3 4 5 6 7 8

DR. NEEPA SHAH 52

Dr. Neepa Shah 26


Unit 6: Searching and Sorting

AN EXAMPLE
N 8 did_swap true

to_do 7

index 7

23 45 14 6 67 33 98 42

1 2 3 4 5 6 7 8

DR. NEEPA SHAH 53

AN EXAMPLE
N 8 did_swap true

to_do 7

index 7

Swap

23 45 14 6 67 33 98 42

1 2 3 4 5 6 7 8

DR. NEEPA SHAH 54

Dr. Neepa Shah 27


Unit 6: Searching and Sorting

AN EXAMPLE
N 8 did_swap true

to_do 7

index 7

Swap

23 45 14 6 67 33 42 98

1 2 3 4 5 6 7 8

DR. NEEPA SHAH 55

AFTER FIRST PASS OF OUTER LOOP


N 8 did_swap true

to_do 7

index 8 Finished first “Bubble Up”

23 45 14 6 67 33 42 98

1 2 3 4 5 6 7 8

DR. NEEPA SHAH 56

Dr. Neepa Shah 28


Unit 6: Searching and Sorting

THE SECOND “BUBBLE UP”


N 8 did_swap false

to_do 6

index 1

23 45 14 6 67 33 42 98

1 2 3 4 5 6 7 8

DR. NEEPA SHAH 57

THE SECOND “BUBBLE UP”


N 8 did_swap false

to_do 6

index 1

No Swap

23 45 14 6 67 33 42 98

1 2 3 4 5 6 7 8

DR. NEEPA SHAH 58

Dr. Neepa Shah 29


Unit 6: Searching and Sorting

THE SECOND “BUBBLE UP”


N 8 did_swap false

to_do 6

index 2

23 45 14 6 67 33 42 98

1 2 3 4 5 6 7 8

DR. NEEPA SHAH 59

THE SECOND “BUBBLE UP”


N 8 did_swap false

to_do 6

index 2

Swap

23 45 14 6 67 33 42 98

1 2 3 4 5 6 7 8

DR. NEEPA SHAH 60

Dr. Neepa Shah 30


Unit 6: Searching and Sorting

THE SECOND “BUBBLE UP”


N 8 did_swap true

to_do 6

index 2

Swap

23 14 45 6 67 33 42 98

1 2 3 4 5 6 7 8

DR. NEEPA SHAH 61

THE SECOND “BUBBLE UP”


N 8 did_swap true

to_do 6

index 3

23 14 45 6 67 33 42 98

1 2 3 4 5 6 7 8

DR. NEEPA SHAH 62

Dr. Neepa Shah 31


Unit 6: Searching and Sorting

THE SECOND “BUBBLE UP”


N 8 did_swap true

to_do 6

index 3

Swap

23 14 45 6 67 33 42 98

1 2 3 4 5 6 7 8

DR. NEEPA SHAH 63

THE SECOND “BUBBLE UP”


N 8 did_swap true

to_do 6

index 3

Swap

23 14 6 45 67 33 42 98

1 2 3 4 5 6 7 8

DR. NEEPA SHAH 64

Dr. Neepa Shah 32


Unit 6: Searching and Sorting

THE SECOND “BUBBLE UP”


N 8 did_swap true

to_do 6

index 4

23 14 6 45 67 33 42 98

1 2 3 4 5 6 7 8

DR. NEEPA SHAH 65

THE SECOND “BUBBLE UP”


N 8 did_swap true

to_do 6

index 4

No Swap

23 14 6 45 67 33 42 98

1 2 3 4 5 6 7 8

DR. NEEPA SHAH 66

Dr. Neepa Shah 33


Unit 6: Searching and Sorting

THE SECOND “BUBBLE UP”


N 8 did_swap true

to_do 6

index 5

23 14 6 45 67 33 42 98

1 2 3 4 5 6 7 8

DR. NEEPA SHAH 67

THE SECOND “BUBBLE UP”


N 8 did_swap true

to_do 6

index 5

Swap

23 14 6 45 67 33 42 98

1 2 3 4 5 6 7 8

DR. NEEPA SHAH 68

Dr. Neepa Shah 34


Unit 6: Searching and Sorting

THE SECOND “BUBBLE UP”


N 8 did_swap true

to_do 6

index 5

Swap

23 14 6 45 33 67 42 98

1 2 3 4 5 6 7 8

DR. NEEPA SHAH 69

THE SECOND “BUBBLE UP”


N 8 did_swap true

to_do 6

index 6

23 14 6 45 33 67 42 98

1 2 3 4 5 6 7 8

DR. NEEPA SHAH 70

Dr. Neepa Shah 35


Unit 6: Searching and Sorting

THE SECOND “BUBBLE UP”


N 8 did_swap true

to_do 6

index 6

Swap

23 14 6 45 33 67 42 98

1 2 3 4 5 6 7 8

DR. NEEPA SHAH 71

THE SECOND “BUBBLE UP”


N 8 did_swap true

to_do 6

index 6

Swap

23 14 6 45 33 42 67 98

1 2 3 4 5 6 7 8

DR. NEEPA SHAH 72

Dr. Neepa Shah 36


Unit 6: Searching and Sorting

AFTER SECOND PASS OF OUTER LOOP


N 8 did_swap true

to_do 6

index 7 Finished second “Bubble Up”

23 14 6 45 33 42 67 98

1 2 3 4 5 6 7 8

DR. NEEPA SHAH 73

THE THIRD “BUBBLE UP”


N 8 did_swap false

to_do 5

index 1

23 14 6 45 33 42 67 98

1 2 3 4 5 6 7 8

DR. NEEPA SHAH 74

Dr. Neepa Shah 37


Unit 6: Searching and Sorting

THE THIRD “BUBBLE UP”


N 8 did_swap false

to_do 5

index 1

Swap

23 14 6 45 33 42 67 98

1 2 3 4 5 6 7 8

DR. NEEPA SHAH 75

THE THIRD “BUBBLE UP”


N 8 did_swap true

to_do 5

index 1

Swap

14 23 6 45 33 42 67 98

1 2 3 4 5 6 7 8

DR. NEEPA SHAH 76

Dr. Neepa Shah 38


Unit 6: Searching and Sorting

THE THIRD “BUBBLE UP”


N 8 did_swap true

to_do 5

index 2

14 23 6 45 33 42 67 98

1 2 3 4 5 6 7 8

DR. NEEPA SHAH 77

THE THIRD “BUBBLE UP”


N 8 did_swap true

to_do 5

index 2

Swap

14 23 6 45 33 42 67 98

1 2 3 4 5 6 7 8

DR. NEEPA SHAH 78

Dr. Neepa Shah 39


Unit 6: Searching and Sorting

THE THIRD “BUBBLE UP”


N 8 did_swap true

to_do 5

index 2

Swap

14 6 23 45 33 42 67 98

1 2 3 4 5 6 7 8

DR. NEEPA SHAH 79

THE THIRD “BUBBLE UP”


N 8 did_swap true

to_do 5

index 3

14 6 23 45 33 42 67 98

1 2 3 4 5 6 7 8

DR. NEEPA SHAH 80

Dr. Neepa Shah 40


Unit 6: Searching and Sorting

THE THIRD “BUBBLE UP”


N 8 did_swap true

to_do 5

index 3

No Swap

14 6 23 45 33 42 67 98

1 2 3 4 5 6 7 8

DR. NEEPA SHAH 81

THE THIRD “BUBBLE UP”


N 8 did_swap true

to_do 5

index 4

14 6 23 45 33 42 67 98

1 2 3 4 5 6 7 8

DR. NEEPA SHAH 82

Dr. Neepa Shah 41


Unit 6: Searching and Sorting

THE THIRD “BUBBLE UP”


N 8 did_swap true

to_do 5

index 4

Swap

14 6 23 45 33 42 67 98

1 2 3 4 5 6 7 8

DR. NEEPA SHAH 83

THE THIRD “BUBBLE UP”


N 8 did_swap true

to_do 5

index 4

Swap

14 6 23 33 45 42 67 98

1 2 3 4 5 6 7 8

DR. NEEPA SHAH 84

Dr. Neepa Shah 42


Unit 6: Searching and Sorting

THE THIRD “BUBBLE UP”


N 8 did_swap true

to_do 5

index 5

14 6 23 33 45 42 67 98

1 2 3 4 5 6 7 8

DR. NEEPA SHAH 85

THE THIRD “BUBBLE UP”


N 8 did_swap true

to_do 5

index 5

Swap

14 6 23 33 45 42 67 98

1 2 3 4 5 6 7 8

DR. NEEPA SHAH 86

Dr. Neepa Shah 43


Unit 6: Searching and Sorting

THE THIRD “BUBBLE UP”


N 8 did_swap true

to_do 5

index 5

Swap

14 6 23 33 42 45 67 98

1 2 3 4 5 6 7 8

DR. NEEPA SHAH 87

AFTER THIRD PASS OF OUTER LOOP


N 8 did_swap true

to_do 5

index 6 Finished third “Bubble Up”

14 6 23 33 42 45 67 98

1 2 3 4 5 6 7 8

DR. NEEPA SHAH 88

Dr. Neepa Shah 44


Unit 6: Searching and Sorting

THE FOURTH “BUBBLE UP”


N 8 did_swap false

to_do 4

index 1

14 6 23 33 42 45 67 98

1 2 3 4 5 6 7 8

DR. NEEPA SHAH 89

THE FOURTH “BUBBLE UP”


N 8 did_swap false

to_do 4

index 1

Swap

14 6 23 33 42 45 67 98

1 2 3 4 5 6 7 8

DR. NEEPA SHAH 90

Dr. Neepa Shah 45


Unit 6: Searching and Sorting

THE FOURTH “BUBBLE UP”


N 8 did_swap true

to_do 4

index 1

Swap

6 14 23 33 42 45 67 98

1 2 3 4 5 6 7 8

DR. NEEPA SHAH 91

THE FOURTH “BUBBLE UP”


N 8 did_swap true

to_do 4

index 2

6 14 23 33 42 45 67 98

1 2 3 4 5 6 7 8

DR. NEEPA SHAH 92

Dr. Neepa Shah 46


Unit 6: Searching and Sorting

THE FOURTH “BUBBLE UP”


N 8 did_swap true

to_do 4

index 2

No Swap

6 14 23 33 42 45 67 98

1 2 3 4 5 6 7 8

DR. NEEPA SHAH 93

THE FOURTH “BUBBLE UP”


N 8 did_swap true

to_do 4

index 3

6 14 23 33 42 45 67 98

1 2 3 4 5 6 7 8

DR. NEEPA SHAH 94

Dr. Neepa Shah 47


Unit 6: Searching and Sorting

THE FOURTH “BUBBLE UP”


N 8 did_swap true

to_do 4

index 3

No Swap

6 14 23 33 42 45 67 98

1 2 3 4 5 6 7 8

DR. NEEPA SHAH 95

THE FOURTH “BUBBLE UP”


N 8 did_swap true

to_do 4

index 4

6 14 23 33 42 45 67 98

1 2 3 4 5 6 7 8

DR. NEEPA SHAH 96

Dr. Neepa Shah 48


Unit 6: Searching and Sorting

THE FOURTH “BUBBLE UP”


N 8 did_swap true

to_do 4

index 4

No Swap

6 14 23 33 42 45 67 98

1 2 3 4 5 6 7 8

DR. NEEPA SHAH 97

AFTER FOURTH PASS OF OUTER LOOP


N 8 did_swap true

to_do 4

index 5 Finished fourth “Bubble Up”

6 14 23 33 42 45 67 98

1 2 3 4 5 6 7 8

DR. NEEPA SHAH 98

Dr. Neepa Shah 49


Unit 6: Searching and Sorting

THE FIFTH “BUBBLE UP”


N 8 did_swap false

to_do 3

index 1

6 14 23 33 42 45 67 98

1 2 3 4 5 6 7 8

DR. NEEPA SHAH 99

THE FIFTH “BUBBLE UP”


N 8 did_swap false

to_do 3

index 1

No Swap

6 14 23 33 42 45 67 98

1 2 3 4 5 6 7 8

DR. NEEPA SHAH 100

Dr. Neepa Shah 50


Unit 6: Searching and Sorting

THE FIFTH “BUBBLE UP”


N 8 did_swap false

to_do 3

index 2

6 14 23 33 42 45 67 98

1 2 3 4 5 6 7 8

DR. NEEPA SHAH 101

THE FIFTH “BUBBLE UP”


N 8 did_swap false

to_do 3

index 2

No Swap

6 14 23 33 42 45 67 98

1 2 3 4 5 6 7 8

DR. NEEPA SHAH 102

Dr. Neepa Shah 51


Unit 6: Searching and Sorting

THE FIFTH “BUBBLE UP”


N 8 did_swap false

to_do 3

index 3

6 14 23 33 42 45 67 98

1 2 3 4 5 6 7 8

DR. NEEPA SHAH 103

THE FIFTH “BUBBLE UP”


N 8 did_swap false

to_do 3

index 3

No Swap

6 14 23 33 42 45 67 98

1 2 3 4 5 6 7 8

DR. NEEPA SHAH 104

Dr. Neepa Shah 52


Unit 6: Searching and Sorting

AFTER FIFTH PASS OF OUTER LOOP


N 8 did_swap false

to_do 3

index 4 Finished fifth “Bubble Up”

6 14 23 33 42 45 67 98

1 2 3 4 5 6 7 8

DR. NEEPA SHAH 105

FINISHED “EARLY”
N 8 did_swap false

to_do 3
We didn’t do any swapping,
index 4 so all of the other elements
must be correctly placed.

We can “skip” the last two


passes of the outer loop.

6 14 23 33 42 45 67 98

1 2 3 4 5 6 7 8
DR. NEEPA SHAH 106

Dr. Neepa Shah 53


Unit 6: Searching and Sorting

EFFICIENT BUBBLE SORT

for(i = 0; i < n; i++) {


flag = 0;
for(j = 1; j < (n-i); j++) {
if(a[j-1] > a[j]) {
t = a[j-1];
a[j-1]=a[j];
a[j]=t;
flag = 1;
}
}

if (flag==0) break;
}

DR. NEEPA SHAH 107

SUMMARY

 “Bubble Up” algorithm will move largest value to its correct location (to the right)
 Repeat “Bubble Up” until all elements are correctly placed:
 Maximum of N-1 times
 Can finish early if no swapping occurs
 We reduce the number of elements we compare each time one is correctly placed

DR. NEEPA SHAH 108

Dr. Neepa Shah 54


Unit 6: Searching and Sorting

SELECTION SORT

 The idea of selection sort is rather simple.

 We repeatedly find the next largest (or smallest) element in the array and move it to its final position
in the sorted array.

 We begin by selecting the largest element and moving it to the highest index position. We can do this
by swapping the element at the highest index and the largest element. The process stops when the
effective size of the array becomes 1

DR. NEEPA SHAH 109

SELECTION SORT - EXAMPLE

32 91 12 55 74 73 18

DR. NEEPA SHAH 110

Dr. Neepa Shah 55


Unit 6: Searching and Sorting

SELECTION SORT - EXAMPLE

32 91 12 55 74 73 18

DR. NEEPA SHAH 111

SELECTION SORT - EXAMPLE


SWAP !

32 91 12 55 74 73 18

DR. NEEPA SHAH 112

Dr. Neepa Shah 56


Unit 6: Searching and Sorting

SELECTION SORT - EXAMPLE

12 91 32 55 74 73 18

DR. NEEPA SHAH 113

SELECTION SORT - EXAMPLE

12 91 32 55 74 73 18

DR. NEEPA SHAH 114

Dr. Neepa Shah 57


Unit 6: Searching and Sorting

SELECTION SORT - EXAMPLE


SWAP !

12 91 32 55 74 73 18

DR. NEEPA SHAH 115

SELECTION SORT - EXAMPLE

12 18 32 55 74 73 91

DR. NEEPA SHAH 116

Dr. Neepa Shah 58


Unit 6: Searching and Sorting

SELECTION SORT - EXAMPLE


NO SWAPPING !

12 18 32 55 74 73 91

DR. NEEPA SHAH 117

SELECTION SORT - EXAMPLE

12 18 32 55 74 73 91

DR. NEEPA SHAH 118

Dr. Neepa Shah 59


Unit 6: Searching and Sorting

SELECTION SORT - EXAMPLE SWAP !

12 18 32 55 74 73 91

DR. NEEPA SHAH 119

SELECTION SORT - EXAMPLE

12 18 32 55 73 74 91

DR. NEEPA SHAH 120

Dr. Neepa Shah 60


Unit 6: Searching and Sorting

SELECTION SORT - EXAMPLE

12 18 32 55 73 74 91

DR. NEEPA SHAH 121

SELECTION SORT - EXAMPLE NO SWAPPING !

12 18 32 55 73 74 91

DR. NEEPA SHAH 122

Dr. Neepa Shah 61


Unit 6: Searching and Sorting

SELECTION SORT - EXAMPLE

12 18 32 55 73 74 91

DR. NEEPA SHAH 123

SELECTION SORT PROCEDURE

1. Do steps 2-3 for i= n-1 down to 1


2. Locate the index m of the largest / smallest element among {s0…sj}
3. Swap si and sm

DR. NEEPA SHAH 124

Dr. Neepa Shah 62


Unit 6: Searching and Sorting

SELECTION SORT ALGORITHM

 selectionSort(array, size)
repeat (size - 1) times
set the first unsorted element as the minimum
for each of the unsorted elements
if element < currentMinimum
set element as new minimum
swap minimum with first unsorted position
end selectionSort

DR. NEEPA SHAH 125

SELECTION SORT ALGORITHM

void sort(int a[])


{
for(i = SIZE-1; i > 0 ; i--)
{
m = 0;
for(j = 1; j <= i; j++)
{
if(a[j] > a[m])
m = j;
}
swap(a, i, m);
}
}

DR. NEEPA SHAH 126

Dr. Neepa Shah 63


Unit 6: Searching and Sorting

INSERTION SORT

 Insertion sort keeps making the left side of the array sorted until the whole array is sorted.

It sorts the values seen so far and repeatedly inserts unseen values in the array into the left
sorted array.

 The insertion sort algorithm is the sort unknowingly used by most card players

DR. NEEPA SHAH 127

INSERTION SORT CONTD.

 while some elements unsorted:


 Using linear search, find the location in the sorted portion where the 1st element of
the unsorted portion should be inserted
 Move all the elements after the insertion location up one position to make space for
the new element

69

38 45
60 60
66 45
66 79 47 13 74 36 21 94 22 57 16 29 81

the fourth iteration of this loop is shown here

DR. NEEPA SHAH 128

Dr. Neepa Shah 64


Unit 6: Searching and Sorting

REVIEW: ONE STEP OF INSERTION SORT


sorted next to be inserted

3 4 7 12 14 14 20 21 33 38 10 55 9 23 28 16
temp
less than 10
10

3 4 7 10 12 14 14 20 21 33 38 55 9 23 28 16

sorted

DR. NEEPA SHAH 129

INSERTION SORT CONTD.

An insertion sort partitions the array into two regions

DR. NEEPA SHAH 130

Dr. Neepa Shah 65


Unit 6: Searching and Sorting

EXAMPLE OF INSERTION SORT


8 2 4 9 3 6

DR. NEEPA SHAH 131

EXAMPLE OF INSERTION SORT


8 2 4 9 3 6

DR. NEEPA SHAH 132

Dr. Neepa Shah 66


Unit 6: Searching and Sorting

EXAMPLE OF INSERTION SORT


8 2 4 9 3 6

2 8 4 9 3 6

DR. NEEPA SHAH 133

EXAMPLE OF INSERTION SORT


8 2 4 9 3 6

2 8 4 9 3 6

DR. NEEPA SHAH 134

Dr. Neepa Shah 67


Unit 6: Searching and Sorting

EXAMPLE OF INSERTION SORT


8 2 4 9 3 6

2 8 4 9 3 6

2 4 8 9 3 6

DR. NEEPA SHAH 135

EXAMPLE OF INSERTION SORT


8 2 4 9 3 6

2 8 4 9 3 6

2 4 8 9 3 6

DR. NEEPA SHAH 136

Dr. Neepa Shah 68


Unit 6: Searching and Sorting

EXAMPLE OF INSERTION SORT


8 2 4 9 3 6

2 8 4 9 3 6

2 4 8 9 3 6

2 4 8 9 3 6

DR. NEEPA SHAH 137

EXAMPLE OF INSERTION SORT


8 2 4 9 3 6

2 8 4 9 3 6

2 4 8 9 3 6

2 4 8 9 3 6

DR. NEEPA SHAH 138

Dr. Neepa Shah 69


Unit 6: Searching and Sorting

EXAMPLE OF INSERTION SORT


8 2 4 9 3 6

2 8 4 9 3 6

2 4 8 9 3 6

2 4 8 9 3 6

2 3 4 8 9 6

DR. NEEPA SHAH 139

EXAMPLE OF INSERTION SORT


8 2 4 9 3 6

2 8 4 9 3 6

2 4 8 9 3 6

2 4 8 9 3 6

2 3 4 8 9 6

DR. NEEPA SHAH 140

Dr. Neepa Shah 70


Unit 6: Searching and Sorting

EXAMPLE OF INSERTION SORT


8 2 4 9 3 6

2 8 4 9 3 6

2 4 8 9 3 6

2 4 8 9 3 6

2 3 4 8 9 6

2 3 4 6 8 9 done
DR. NEEPA SHAH 141

INSERTION SORT EXAMPLE

 5 7 0 3 4 2 6 1 (0)
 5 7 0 3 4 2 6 1 (0)
 0 5 7 3 4 2 6 1 (2)
 0 3 5 7 4 2 6 1 (2)
 0 3 4 5 7 2 6 1 (2)
 0 2 3 4 5 7 6 1 (4)
 0 2 3 4 5 6 7 1 (1)
 0 1 2 3 4 5 6 7 (6)

DR. NEEPA SHAH 142

Dr. Neepa Shah 71


Unit 6: Searching and Sorting

An insertion sort of an array of five integers

DR. NEEPA SHAH 143

INSERTION SORT PROCEDURE

1. Do steps 2-4 for i= 1 upto n-1


2. Hold the element s
3. Locate the least index j for which sj >= si
4. Shift the subsequence {sj … si-1} up one position to {sj+1 … si}
5. Copy the held value of si into sj

DR. NEEPA SHAH 144

Dr. Neepa Shah 72


Unit 6: Searching and Sorting

INSERTION SORT ALGORITHM

 insertionSort(array)
mark first element as sorted
for each unsorted element X
‘hold' the element X
for j <- lastSortedIndex down to 0
if current element j > X
move sorted element to the right by 1
break loop and insert X here
end insertionSort
DR. NEEPA SHAH 145

INSERTION SORT ALGORITHM

void insertionSort(int[] a) {
for (int i = 1; i < SIZE; i++) {
int ai = a[i], j;

for (j = i; j > 0 && a[j-1] > ai; j--) {


a[j] = a[j–1];
}
a[j] = ai;
}
}

DR. NEEPA SHAH 146

Dr. Neepa Shah 73


Unit 6: Searching and Sorting

INSERTION SORT: NUMBER OF COMPARISONS


# of Sorted Best case Worst case
Elements

0 0 0

1 1 1

2 1 2

… … …

n-1 1 n-1
n-1 n(n-1)/2

If the sequence is nearly sorted, then insertion sort will run nearly in O(n) time.

DR. NEEPA SHAH 147

DIVIDE AND CONQUER STRATEGY

Dr. Neepa Shah 74


Unit 6: Searching and Sorting

DIVIDE-AND-CONQUER

 Divide and Conquer is a method of algorithm design.

 This method has three distinct steps:


 Divide: If the input size is too large to deal with in a straightforward manner, divide the data into two or
more disjoint subsets.
 Recur: Use divide and conquer to solve the sub-problems associated with the data subsets.
 Conquer: Take the solutions to the sub-problems and “merge” these solutions into a solution for the original
problem.

DR. NEEPA SHAH 149

MERGE-SORT

 Algorithm:
 Divide
 Recur: Recursive sort sequences S1 and S2.
 Conquer: Put back the elements into S by merging the sorted sequences S1 and S2 into a unique
sorted sequence.

DR. NEEPA SHAH 150

Dr. Neepa Shah 75


Unit 6: Searching and Sorting

MERGE-SORT EXAMPLE

DR. NEEPA SHAH 151

98 23 45 14 6 67 33 42

98 23 45 14 6 67 33 42

DR. NEEPA SHAH 152

Dr. Neepa Shah 76


Unit 6: Searching and Sorting

98 23 45 14 6 67 33 42

98 23 45 14 6 67 33 42

98 23 45 14

DR. NEEPA SHAH 153

98 23 45 14 6 67 33 42

98 23 45 14 6 67 33 42

98 23 45 14

98 23

DR. NEEPA SHAH 154

Dr. Neepa Shah 77


Unit 6: Searching and Sorting

98 23 45 14 6 67 33 42

98 23 45 14 6 67 33 42

98 23 45 14

98 23

Merge

DR. NEEPA SHAH 155

98 23 45 14 6 67 33 42

98 23 45 14 6 67 33 42

98 23 45 14

98 23

23

Merge

DR. NEEPA SHAH 156

Dr. Neepa Shah 78


Unit 6: Searching and Sorting

98 23 45 14 6 67 33 42

98 23 45 14 6 67 33 42

98 23 45 14

98 23

23 98

Merge

DR. NEEPA SHAH 157

98 23 45 14 6 67 33 42

98 23 45 14 6 67 33 42

98 23 45 14

98 23 45 14

23 98

DR. NEEPA SHAH 158

Dr. Neepa Shah 79


Unit 6: Searching and Sorting

98 23 45 14 6 67 33 42

98 23 45 14 6 67 33 42

98 23 45 14

98 23 45 14

23 98

Merge

DR. NEEPA SHAH 159

98 23 45 14 6 67 33 42

98 23 45 14 6 67 33 42

98 23 45 14

98 23 45 14

23 98 14

Merge

DR. NEEPA SHAH 160

Dr. Neepa Shah 80


Unit 6: Searching and Sorting

98 23 45 14 6 67 33 42

98 23 45 14 6 67 33 42

98 23 45 14

98 23 45 14

23 98 14 45

Merge

DR. NEEPA SHAH 161

98 23 45 14 6 67 33 42

98 23 45 14 6 67 33 42

98 23 45 14

98 23 45 14

23 98 14 45

Merge

DR. NEEPA SHAH 162

Dr. Neepa Shah 81


Unit 6: Searching and Sorting

98 23 45 14 6 67 33 42

98 23 45 14 6 67 33 42

98 23 45 14

98 23 45 14

23 98 14 45

14

Merge

DR. NEEPA SHAH 163

98 23 45 14 6 67 33 42

98 23 45 14 6 67 33 42

98 23 45 14

98 23 45 14

23 98 14 45

14 23

Merge

DR. NEEPA SHAH 164

Dr. Neepa Shah 82


Unit 6: Searching and Sorting

98 23 45 14 6 67 33 42

98 23 45 14 6 67 33 42

98 23 45 14

98 23 45 14

23 98 14 45

14 23 45

Merge

DR. NEEPA SHAH 165

98 23 45 14 6 67 33 42

98 23 45 14 6 67 33 42

98 23 45 14

98 23 45 14

23 98 14 45

14 23 45 98

Merge

DR. NEEPA SHAH 166

Dr. Neepa Shah 83


Unit 6: Searching and Sorting

98 23 45 14 6 67 33 42

98 23 45 14 6 67 33 42

98 23 45 14 6 67 33 42

98 23 45 14

23 98 14 45

14 23 45 98

DR. NEEPA SHAH 167

98 23 45 14 6 67 33 42

98 23 45 14 6 67 33 42

98 23 45 14 6 67 33 42

98 23 45 14 6 67

23 98 14 45

14 23 45 98

DR. NEEPA SHAH 168

Dr. Neepa Shah 84


Unit 6: Searching and Sorting

98 23 45 14 6 67 33 42

98 23 45 14 6 67 33 42

98 23 45 14 6 67 33 42

98 23 45 14 6 67

23 98 14 45

14 23 45 98 Merge

DR. NEEPA SHAH 169

98 23 45 14 6 67 33 42

98 23 45 14 6 67 33 42

98 23 45 14 6 67 33 42

98 23 45 14 6 67

23 98 14 45 6

14 23 45 98 Merge

DR. NEEPA SHAH 170

Dr. Neepa Shah 85


Unit 6: Searching and Sorting

98 23 45 14 6 67 33 42

98 23 45 14 6 67 33 42

98 23 45 14 6 67 33 42

98 23 45 14 6 67

23 98 14 45 6 67

14 23 45 98 Merge

DR. NEEPA SHAH 171

98 23 45 14 6 67 33 42

98 23 45 14 6 67 33 42

98 23 45 14 6 67 33 42

98 23 45 14 6 67 33 42

23 98 14 45 6 67

14 23 45 98

DR. NEEPA SHAH 172

Dr. Neepa Shah 86


Unit 6: Searching and Sorting

98 23 45 14 6 67 33 42

98 23 45 14 6 67 33 42

98 23 45 14 6 67 33 42

98 23 45 14 6 67 33 42

23 98 14 45 6 67

14 23 45 98 Merge

DR. NEEPA SHAH 173

98 23 45 14 6 67 33 42

98 23 45 14 6 67 33 42

98 23 45 14 6 67 33 42

98 23 45 14 6 67 33 42

23 98 14 45 6 67 33

14 23 45 98 Merge

DR. NEEPA SHAH 174

Dr. Neepa Shah 87


Unit 6: Searching and Sorting

98 23 45 14 6 67 33 42

98 23 45 14 6 67 33 42

98 23 45 14 6 67 33 42

98 23 45 14 6 67 33 42

23 98 14 45 6 67 33 42

14 23 45 98 Merge

DR. NEEPA SHAH 175

98 23 45 14 6 67 33 42

98 23 45 14 6 67 33 42

98 23 45 14 6 67 33 42

98 23 45 14 6 67 33 42

23 98 14 45 6 67 33 42

14 23 45 98

Merge

DR. NEEPA SHAH 176

Dr. Neepa Shah 88


Unit 6: Searching and Sorting

98 23 45 14 6 67 33 42

98 23 45 14 6 67 33 42

98 23 45 14 6 67 33 42

98 23 45 14 6 67 33 42

23 98 14 45 6 67 33 42

14 23 45 98 6

Merge

DR. NEEPA SHAH 177

98 23 45 14 6 67 33 42

98 23 45 14 6 67 33 42

98 23 45 14 6 67 33 42

98 23 45 14 6 67 33 42

23 98 14 45 6 67 33 42

14 23 45 98 6 33

Merge

DR. NEEPA SHAH 178

Dr. Neepa Shah 89


Unit 6: Searching and Sorting

98 23 45 14 6 67 33 42

98 23 45 14 6 67 33 42

98 23 45 14 6 67 33 42

98 23 45 14 6 67 33 42

23 98 14 45 6 67 33 42

14 23 45 98 6 33 42

Merge

DR. NEEPA SHAH 179

98 23 45 14 6 67 33 42

98 23 45 14 6 67 33 42

98 23 45 14 6 67 33 42

98 23 45 14 6 67 33 42

23 98 14 45 6 67 33 42

14 23 45 98 6 33 42 67

Merge

DR. NEEPA SHAH 180

Dr. Neepa Shah 90


Unit 6: Searching and Sorting

98 23 45 14 6 67 33 42

98 23 45 14 6 67 33 42

98 23 45 14 6 67 33 42

98 23 45 14 6 67 33 42

23 98 14 45 6 67 33 42

14 23 45 98 6 33 42 67

DR. NEEPA SHAH Merge 181

98 23 45 14 6 67 33 42

98 23 45 14 6 67 33 42

98 23 45 14 6 67 33 42

98 23 45 14 6 67 33 42

23 98 14 45 6 67 33 42

14 23 45 98 6 33 42 67

DR. NEEPA SHAH Merge 182

Dr. Neepa Shah 91


Unit 6: Searching and Sorting

98 23 45 14 6 67 33 42

98 23 45 14 6 67 33 42

98 23 45 14 6 67 33 42

98 23 45 14 6 67 33 42

23 98 14 45 6 67 33 42

14 23 45 98 6 33 42 67

6 14

DR. NEEPA SHAH Merge 183

98 23 45 14 6 67 33 42

98 23 45 14 6 67 33 42

98 23 45 14 6 67 33 42

98 23 45 14 6 67 33 42

23 98 14 45 6 67 33 42

14 23 45 98 6 33 42 67

6 14 23

DR. NEEPA SHAH Merge 184

Dr. Neepa Shah 92


Unit 6: Searching and Sorting

98 23 45 14 6 67 33 42

98 23 45 14 6 67 33 42

98 23 45 14 6 67 33 42

98 23 45 14 6 67 33 42

23 98 14 45 6 67 33 42

14 23 45 98 6 33 42 67

6 14 23 33

DR. NEEPA SHAH Merge 185

98 23 45 14 6 67 33 42

98 23 45 14 6 67 33 42

98 23 45 14 6 67 33 42

98 23 45 14 6 67 33 42

23 98 14 45 6 67 33 42

14 23 45 98 6 33 42 67

6 14 23 33 42

DR. NEEPA SHAH Merge 186

Dr. Neepa Shah 93


Unit 6: Searching and Sorting

98 23 45 14 6 67 33 42

98 23 45 14 6 67 33 42

98 23 45 14 6 67 33 42

98 23 45 14 6 67 33 42

23 98 14 45 6 67 33 42

14 23 45 98 6 33 42 67

6 14 23 33 42 45

DR. NEEPA SHAH Merge 187

98 23 45 14 6 67 33 42

98 23 45 14 6 67 33 42

98 23 45 14 6 67 33 42

98 23 45 14 6 67 33 42

23 98 14 45 6 67 33 42

14 23 45 98 6 33 42 67

6 14 23 33 42 45 67

DR. NEEPA SHAH Merge 188

Dr. Neepa Shah 94


Unit 6: Searching and Sorting

98 23 45 14 6 67 33 42

98 23 45 14 6 67 33 42

98 23 45 14 6 67 33 42

98 23 45 14 6 67 33 42

23 98 14 45 6 67 33 42

14 23 45 98 6 33 42 67

6 14 23 33 42 45 67 98

DR. NEEPA SHAH Merge 189

98 23 45 14 6 67 33 42

98 23 45 14 6 67 33 42

98 23 45 14 6 67 33 42

98 23 45 14 6 67 33 42

23 98 14 45 6 67 33 42

14 23 45 98 6 33 42 67

6 14 23 33 42 45 67 98

DR. NEEPA SHAH 190

Dr. Neepa Shah 95


Unit 6: Searching and Sorting

98 23 45 14 6 67 33 42

6 14 23 33 42 45 67 98

DR. NEEPA SHAH 191

EXAMPLE

DR. NEEPA SHAH 192

Dr. Neepa Shah 96


Unit 6: Searching and Sorting

MERGE SORT ALGORITHM

 Overview:
 Split array into two halves
 Sort the left half (recursively)
 Sort the right half (recursively)
 Merge the two sorted halves

DR. NEEPA SHAH 193

MERGE SORT PROCEDURE

1. If q-p > 1 do steps 2-5


2. Split s into 2 subsequences, a = {sp … sm-1} and b = {sm … sq-1}, where m = (q-p)/2.
3. Sort a
4. Sort b
5. Merge a and b back into s, preserving order

DR. NEEPA SHAH 194

Dr. Neepa Shah 97


Unit 6: Searching and Sorting

MERGE SORT ALGORITHM

void sort(int a[],int p, int q)


{
if (p > r)
return;

int m = (p + q) / 2;
sort(a, p, m);
sort(a, m+1, q);
merge (a, p, m , q);
}

DR. NEEPA SHAH 195

MERGE FUNCTION
void merge(int arr[], int low, int mid, int high)
{
int i, m, k, l, temp[MAX];
l=low;
i=low;
m=mid+1;
while((l<=mid)&&(m<=high))
{
if(arr[l]<=arr[m])
{
temp[i]=arr[l];
l++;
}
else
{
temp[i]=arr[m];
m++;
}
i++;
}

DR. NEEPA SHAH 196

Dr. Neepa Shah 98


Unit 6: Searching and Sorting

MERGE FUNCTION CONTD.


if(l>mid)
{
for(k=m;k<=high;k++)
{
temp[i]=arr[k];
i++;
}
}
else
{
for(k=l;k<=mid;k++)
{
temp[i]=arr[k];
i++;
}
}

for(k=low;k<=high;k++)
{
arr[k]=temp[k];
}
}
DR. NEEPA SHAH 197

RUNNING TIME OF MERGE-SORT

 At each level in the binary tree created for Merge Sort, there are n elements, with O(1) time spent at each
element
 → O(n) running time for processing one level
 The height of the tree is O(log n)

 Therefore, the time complexity is O(nlog n)

DR. NEEPA SHAH 198

Dr. Neepa Shah 99


Unit 6: Searching and Sorting

QUICK-SORT

1) Divide : If the sequence S has 2 or more elements, select an element x from S to be your pivot. Any arbitrary
element, like the last, will do. Remove all the elements of S and divide them into 3 sequences:
L, holds S’s elements less than x
E, holds S’s elements equal to x
G, holds S’s elements greater than x
2) Recurse: Recursively sort L and G
3) Conquer: Finally, to put elements back into S in order, first inserts the elements of L, then those of E, and those of
G.

DR. NEEPA SHAH 199

IDEA OF QUICK SORT

1) Select: pick an element

2) Divide: rearrange elements so that x goes to its final


position E

3) Recurse and Conquer: recursively sort

DR. NEEPA SHAH 200

Dr. Neepa Shah 100


Unit 6: Searching and Sorting

QUICK-SORT TREE

DR. NEEPA SHAH 201

IN-PLACE QUICK-SORT

 Divide step: l scans the sequence


from the left, and r from the right.
 A swap is performed when l is at
an element larger than the pivot
and r is at one smaller than the
pivot.

DR. NEEPA SHAH 202

Dr. Neepa Shah 101


Unit 6: Searching and Sorting

IN PLACE QUICK SORT (CONT’D)

A final swap with the pivot completes the divide step

DR. NEEPA SHAH

https://www.youtube.com/watch?v=-2VqW516BcI 203

QUICK SORT PROCEDURE / ALGORITHM

1. If q-p > 1 do steps 2-5


2. Apply partition algorithm and
3. obtain pivot index m
4. Apply quick sort to {s0 … sm-1}
5. Apply quick sort to {sm+1 … sn-1}

DR. NEEPA SHAH 204

Dr. Neepa Shah 102


Unit 6: Searching and Sorting

QUICK SORT PROCEDURE / ALGORITHM

void sort(int a[],int p, int q){


if (q-p < 2)
{
return;
}

int m = partition(a, p, q) ;
sort(a, p, m);
sort(a, m+1, q);
}

DR. NEEPA SHAH 205

PARTITION ALGORITHM
int partition(int a[], int p, int q)
{

int pivot = a[p], i = p, j = q;


while (i < j)
{
while (i < j && a[--j] >= pivot) ;
if(i < j)
a[i] = a[j];

while (i < j && a[++i] <= pivot) ;


if(i < j)
a[j] = a[i];
}
a[j] = pivot;
DR. NEEPA SHAH 206
return j;
}

Dr. Neepa Shah 103


Unit 6: Searching and Sorting

WHY STUDY HEAPSORT?

 Heapsort is always O(n log n)


 Quicksort is usually O(n log n) but in the worst case slows to O(n2)

DR. NEEPA SHAH 207

WHAT IS A “HEAP”?

A balanced, left-justified binary tree in which no node has a value


greater than the value in its parent

DR. NEEPA SHAH 208

Dr. Neepa Shah 104


Unit 6: Searching and Sorting

BALANCED BINARY TREES

 The depth of a node is its distance from the root


 The depth of a tree is the depth of the deepest node

 A binary tree of depth n is balanced if all the nodes at depths 0 through n-2 have two children

n-2
n-1
n
Balanced Balanced Not balanced

DR. NEEPA SHAH 209

LEFT-JUSTIFIED BINARY TREES

 A balanced binary tree of depth n is left-justified if:


 it has 2n nodes at depth n (the tree is “full”)

or
 it has 2k nodes at depth k, for all k < n, and all the leaves at depth n are as far left as possible

Left-justified Not left-justified


DR. NEEPA SHAH 210

Dr. Neepa Shah 105


Unit 6: Searching and Sorting

APPROACH: HEAP SORT

 Top down
 Bottom up

 First, we will learn how to turn a binary tree into a heap


 Next, we will learn how to turn a binary tree back into a heap after it has been changed in a certain way
 Finally, we will see how to use these ideas to sort an array

DR. NEEPA SHAH 211

THE HEAP PROPERTY

12 12 12

8 3 8 12 8 14
Blue node has heap Blue node has heap Blue node does not have
property property heap property

 A node has the heap property if the value in the node is larger than the values in its children
 All leaf nodes automatically have the heap property
 A binary tree is a heap if all nodes in it have the heap property

DR. NEEPA SHAH 212

Dr. Neepa Shah 106


Unit 6: Searching and Sorting

SIFTUP

12 14

8 14 8 12
Blue node does not have Blue node has heap
heap property property

 Given a node that does not have the heap property, you can give it the heap property
by exchanging its value with the value of the larger child
 This is sometimes called sifting up

DR. NEEPA SHAH 213

CONSTRUCTING A HEAP I

 A tree consisting of a single node is automatically a heap


 We construct a heap by adding nodes one at a time:
 Add the node just to the right of the rightmost node in the deepest level
 If the deepest level is full, start a new level
 Examples:
Add a new Add a new
node here node here

DR. NEEPA SHAH 214

Dr. Neepa Shah 107


Unit 6: Searching and Sorting

CONSTRUCTING A HEAP II

 Each time we add a node, we may destroy the heap property of its parent node
 To fix this, we sift up
 But each time we sift up, the value of the topmost node in the sift may increase, and this may destroy the heap
property of its parent node
 We repeat the sifting up process, moving up in the tree, until either
 We reach nodes whose values don’t need to be swapped (because the parent is still larger than both children), or
 We reach the root

DR. NEEPA SHAH 215

CONSTRUCTING A HEAP III


8 8 10 10

10 8 8 5

1 2 3

10 10 12

8 5 12 5 10 5

12 8 8
4
DR. NEEPA SHAH 216

Dr. Neepa Shah 108


Unit 6: Searching and Sorting

OTHER CHILDREN ARE NOT AFFECTED


12 12 14

10 5 14 5 12 5

8 14 8 10 8 10

 The node containing 8 is not affected because its parent gets larger, not smaller
 The node containing 5 is not affected because its parent gets larger, not smaller
 The node containing 8 is still not affected because, although its parent got smaller, its parent is still
greater than it was originally

DR. NEEPA SHAH 217

A SAMPLE HEAP
 Here’s a sample binary tree 25
after it has been heapified
22 17
 Notice that heapified does not
mean sorted
 Heapifying does not change the 19 22 14 15
shape of the binary tree; this
18 14 21 3 9 11
binary tree is balanced and left-
justified

DR. NEEPA SHAH 218

Dr. Neepa Shah 109


Unit 6: Searching and Sorting

REMOVING THE ROOT

 Notice that the largest 11


number is now in the root
 Suppose we discard the 22 17
root:
 How can we fix the binary
tree so it is once again 19 22 14 15
balanced and left-justified?
 Solution: remove the 18 14 21 3 9 11
rightmost leaf at the
deepest level and use it for
the new root

DR. NEEPA SHAH 219

THE REHEAP METHOD

 Our tree is balanced and


11
left-justified, but no longer a
heap 22 17
 However, only the root lacks
the heap property
19 22 14 15
 We can siftUp() the root
 After doing this, one and 18 14 21 3 9
only one of its children may
have lost the heap property

DR. NEEPA SHAH 220

Dr. Neepa Shah 110


Unit 6: Searching and Sorting

THE REHEAP METHOD CONTD

22
 Now the left child of the
11 17
root (still the number 11)
lacks the heap property
 We can siftUp() this node 19 22 14 15

 After doing this, one and 18 14 21 3 9


only one of its children may
have lost the heap property

DR. NEEPA SHAH 221

THE REHEAP METHOD CONTD.

 Now the right child of the left 22


child of the root (still the
number 11) lacks the heap 22 17
property:
 We can siftUp() this node
19 11 14 15
 After doing this, one and only
one of its children may have 18 14 21 3 9
lost the heap property —but it
doesn’t, because it’s a leaf

DR. NEEPA SHAH 222

Dr. Neepa Shah 111


Unit 6: Searching and Sorting

THE REHEAP METHOD CONTD.

 Our tree is once again a heap, 22


because every node in it has the
heap property 22 17

 Once again, the largest value is in


the root 19 21 14 15
 We can repeat this process until
the tree becomes empty 18 14 11 3 9

 This produces a sequence of


values in order largest to smallest

DR. NEEPA SHAH 223

SORTING

 What do heaps have to do with sorting an array?


 Because the binary tree is balanced and left justified, it can be represented as an array
 All our operations on binary trees can be represented as operations on arrays
 To sort:
heapify the array;
while the array isn’t empty {
remove and replace the root;
reheap the new root node;
}

DR. NEEPA SHAH 224

Dr. Neepa Shah 112


Unit 6: Searching and Sorting

MAPPING INTO AN ARRAY


25

22 17

19 22 14 15

18 14 21 3 9 11

0 1 2 3 4 5 6 7 8 9 10 11 12
25 22 17 19 22 14 15 18 14 21 3 9 11
 The left child of index i is at index 2*i+1
DR. NEEPA SHAH  The right child of index i is at index 2*i+2 225

 Example: the children of node 3 (19) are 7 (18) and 8 (14)

REMOVING AND REPLACING THE ROOT


0 1 2 3 4 5 6 7 8 9 10 11 12
25 22 17 19 22 14 15 18 14 21 3 9 11

0 1 2 3 4 5 6 7 8 9 10 11 12
11 22 17 19 22 14 15 18 14 21 3 9 25

 The “root” is the first element in the array


 The “rightmost node at the deepest level” is the last element
 Swap them and pretend that the last element in the array no longer exists—that is,
the “last index” is 11 (containing the value 9)
DR. NEEPA SHAH 226

Dr. Neepa Shah 113


Unit 6: Searching and Sorting

REHEAP AND REPEAT


0 1 2 3 4 5 6 7 8 9 10 11 12
11 22 17 19 22 14 15 18 14 21 3 9 25

0 1 2 3 4 5 6 7 8 9 10 11 12
22 22 17 19 21 14 15 18 14 11 3 9 25

0 1 2 3 4 5 6 7 8 9 10 11 12
9 22 17 19 22 14 15 18 14 21 3 22 25

 Reheap the root node (index 0, containing 11) And again, remove and replace the root node
 Remember, though, that the “last” array index is changed
DR. NEEPA SHAH 227
 Repeat until the last becomes first, and the array is sorted!

ANALYSIS I

 Here’s how the algorithm starts:


heapify the array;

 Heapifying the array: we add each of n nodes


 Each node has to be sifted up, possibly as far as the root
 Since the binary tree is perfectly balanced, sifting up a single node takes O(log n) time
 Since we do this n times, heapifying takes n*O(log n) time, that is, O(n log n) time

DR. NEEPA SHAH 228

Dr. Neepa Shah 114


Unit 6: Searching and Sorting

ANALYSIS II

 Rest of the algorithm:


while the array isn’t empty {
remove and replace the root;
reheap the new root node;
}

 We do the while loop n times (actually, n-1 times), because we remove one of the n nodes each time
 Removing and replacing the root takes O(1) time

 Therefore, the total time is n times however long it takes the reheap method

DR. NEEPA SHAH 229

ANALYSIS III

 To reheap the root node, we have to follow one path from the root to a leaf node (and we might stop before we
reach a leaf)
 The binary tree is perfectly balanced
 Therefore, this path is O(log n) long
 And we only do O(1) operations at each node
 Therefore, reheaping takes O(log n) times
 Since we reheap inside a while loop that we do n times, the total time for the while loop is n*O(log n), or O(n
log n)

DR. NEEPA SHAH 230

Dr. Neepa Shah 115


Unit 6: Searching and Sorting

ANALYSIS IV

 Here’s the algorithm again:


heapify the array;
while the array isn’t empty {
remove and replace the root;
reheap the new root node;
}

 We have seen that heapifying takes O(n log n) time


 The while loop takes O(n log n) time
 The total time is therefore O(n log n) + O(n log n)
 This is the same as O(n log n) time
DR. NEEPA SHAH 231

CLASSIFICATION OF SORTING ALGORITHMS


 Comparison sorts
 Bubble sort
 Insertion sort
 Selection sort
 Shell sort
 Merge sort
 Quick sort
 Heap sort
 Non-comparion sorts (LINEAR TIME)
 Count Sort
 Radix sort
 Bucket Sort

DR. NEEPA SHAH 232

Dr. Neepa Shah 116


Unit 6: Searching and Sorting

HOW FAST CAN WE SORT?

 Selection Sort, Bubble Sort, Insertion Sort: O(n2)


 Heap Sort, Merge sort: O(nlogn)
 Quicksort: O(nlogn) - average
 What is common to all these algorithms?

 Make comparisons between input elements

ai < aj, ai ≤ aj, ai = aj, ai ≥ aj, or ai > aj

DR. NEEPA SHAH 233

RADIX SORT

 Radix is the base of a number system.

 Radix sort is a multiple pass distribution sort.

 Radix sort uses bucket sort as the stable sorting algorithm, where the initial relative order of
equal keys is unchanged

DR. NEEPA SHAH 234

Dr. Neepa Shah 117


Unit 6: Searching and Sorting

RADIX SORT

 ASCII (r = 26)
 Decimal (r = 10)
 Bit String (r = 2)
 Octal (r = 8)
 Hexadecimal (r = 16)

DR. NEEPA SHAH 235

RADIX SORT EXAMPLE


Input data:

a b a b a c c a a a c b b a b c c a a a c b b a

DR. NEEPA SHAH 236

Dr. Neepa Shah 118


Unit 6: Searching and Sorting

RADIX SORT EXAMPLE


Pass 1: Looking at rightmost position.

a b a b a c c a a a c b b a b c c a a a c b b a

Place into appropriate pile.

a b c
DR. NEEPA SHAH 237

RADIX SORT EXAMPLE


Pass 1: Looking at rightmost position.

a b a b a c c a a a c b b a b c c a a a c b b a

b b a Join piles.

c c a

c a a b a b a a c

a b a a c b b a c
a b c
DR. NEEPA SHAH 238

Dr. Neepa Shah 119


Unit 6: Searching and Sorting

RADIX SORT EXAMPLE


Pass 2: Looking at next position.

a b a c a a c c a b b a a c b b a b b a c a a c

Place into appropriate pile.

a b c
DR. NEEPA SHAH 239

RADIX SORT EXAMPLE


Pass 2: Looking at next position.

a b a c a a c c a b b a a c b b a b b a c a a c

a a c Join piles.

b a c

b a b b b a a c b
c a a a b a c c a
a b c
DR. NEEPA SHAH 240

Dr. Neepa Shah 120


Unit 6: Searching and Sorting

RADIX SORT EXAMPLE


Pass 3: Looking at last position.

c a a b a b b a c a a c a b a b b a c c a a c b

Place into appropriate pile.

a b c
DR. NEEPA SHAH 241

RADIX SORT EXAMPLE


Pass 3: Looking at last position.

c a a b a b b a c a a c a b a b b a c c a a c b

Join piles.

a c b b b a

a b a b a c c c a

a a c b a b c a a
a b c
DR. NEEPA SHAH 242

Dr. Neepa Shah 121


Unit 6: Searching and Sorting

RADIX SORT EXAMPLE


Result is sorted.

a a c a b a a c b b a b b a c b b a c a a c c a

DR. NEEPA SHAH 243

CLASSIFICATION OF RADIX SORT

 Radix sort is classified based on how it works internally:


 least significant digit (LSD) radix sort
 most significant digit (MSD) radix sort

DR. NEEPA SHAH 244

Dr. Neepa Shah 122


Unit 6: Searching and Sorting

RADIX-SORT CONTD.

DR. NEEPA SHAH 245

RADIX SORT EXAMPLE

 Least-significant-digit-first

Example: 275, 087, 426, 061, 509, 170, 677, 503

DR. NEEPA SHAH 246

Dr. Neepa Shah 123


Unit 6: Searching and Sorting

RADIX SORT CONTD.

DR. NEEPA SHAH 247

Example for LSD Radix Sort


d (digit) = 3, r (radix) = 10 ascending order
179, 208, 306, 93, 859, 984, 55, 9, 271, 33
front[0] NULL rear[0]
front[1] 271 NULL rear[1]
front[2] NULL rear[2]
front[3] 93 33 NULL rear[3]
front[4] 984 NULL rear[4]
front[5] 55 NULL rear[5]
front[6] 306 NULL rear[6]
front[7] NULL rear[7]
front[8] 208 NULL rear[8]
front[9] 179 859 9 NULL rear[9]
DR. NEEPA SHAH 248
271, 93, 33, 984, 55, 306, 208, 179, 859, 9 After the first pass

Dr. Neepa Shah 124


Unit 6: Searching and Sorting

front[0] 306 208 9 null rear[0]


front[1] null rear[1]

front[2] null rear[2]

front[3] 33 null rear[3]

front[4] null rear[4]

front[5] 55 859 null rear[5]

front[6] null rear[6]

front[7] 271 179 null rear[7]


front[8] 984 null rear[8]
DR. NEEPA SHAH
front[9] 93 null 249

306, 208, 9, 33, 55, 859, 271, 179, 984, 93 (second pass) rear[9]

front[0] 9 33 55 93 null rear[0]


front[1] 179 null rear[1]

front[2] 208 271 null rear[2]

front[3] 306 null rear[3]

front[4] null rear[4]

front[5] null rear[5]

front[6] null rear[6]

front[7] null rear[7]

front[8] 859 null rear[8]


DR. NEEPA SHAH
front[9] 984 null 250

9, 33, 55, 93, 179, 208, 271, 306, 859, 984 (third pass) rear[9]

Dr. Neepa Shah 125


Unit 6: Searching and Sorting

RADIX SORT

 Every integer can be represented by at most k digits


 d1d2…dk where di are digits in base r
 d1: most significant digit
 dk: least significant digit

DR. NEEPA SHAH 251

RADIX SORT CONTD.

 Algorithm
 sort by the least significant digit first
=> Numbers with the same digit go to same bin
 reorder all the numbers: the numbers in bin 0 precede the numbers in bin 1, which precede the
numbers in bin 2, and so on
 sort by the next least significant digit
 continue this process until the numbers have been sorted on all k digits

DR. NEEPA SHAH 252

Dr. Neepa Shah 126


Unit 6: Searching and Sorting

STEPS / PROCEDURE

1. For k=1 to number of digits do steps 2 and 3


2. For i=0 to n-1 do
Temp=x[i]
d = kth digit of temp
Place temp at rear of Q[d]

3. For j=0 to 9 do
delete elements of Q[j] and place in next sequential position of queue x

DR. NEEPA SHAH 253

ALGORITHM
// base 10
// FIFO

// d times of counting sort

// scan A[i], put into correct slot

// re-order back to original array

DR. NEEPA SHAH 254

Dr. Neepa Shah 127


Unit 6: Searching and Sorting

COMPARISON OF SORTING ALGORITHMS


Algorithm Average Worst case Space usage
Selection n2 n2 In place
Bubble n2 n2 In place
Insertion n2 n2 In place
Merge O(nlog2n) O(nlog2n) Extra n entries
Quick O(nlog2n) O(n2) In place
Heap O(nlog2n) O(nlog2n) In place
Radix O(m + n) O(m + n) Extra space for links

DR. NEEPA SHAH 255

Dr. Neepa Shah 128

You might also like