Professional Documents
Culture Documents
1) Lec6 SearchingSorting PDF
1) Lec6 SearchingSorting PDF
Input:
Output:
2
Linear Search
-23 97 18 21 5 -86 64 0 -37
3
Linear Search Example #0
-23 97 18 21 5 -86 64 0 -37
element
4
Linear Search Example #1
-23 97 18 21 5 -86 64 0 -37
element
5
Linear Search Example #2
-23 97 18 21 5 -86 64 0 -37
element
6
Linear Search Example #3
-23 97 18 21 5 -86 64 0 -37
element
7
Linear Search Example #4
-23 97 18 21 5 -86 64 0 -37
element
8
Linear Search Example #5
-23 97 18 21 5 -86 64 0 -37
element
9
Linear Search Code
int LinearSearch(int a[], int n, int key)
{
int i;
for(i = 0; i < n; i++) {
if(a[i] = = key) {
return i;
}
}
return -1;
}
10
Linear Search: Worst Case
How long will our search take?
In the worst case, the key value is in the last element
of the array.
So the search takes an amount of time proportional
to the length of the array.
Computer scientists denote this O(n) .
11
Binary Search
1. Initially, the search region is the whole array.
2. Look at the data value in the middle of the
search region.
3. If youve found your key, stop.
4. If key is less than middle data value, the new
search region is the lower half of the data.
5. If key is greater than middle data value, the new
search region is the higher half of the data.
6. Continue from Step 2.
12
Binary Search Example
-86 -37 -23 0 5 18 21 64 97
13
Binary Search Example
-86 -37 -23 0 5 18 21 64 97
low high
middle
14
Binary Search Example
-86 -37 -23 0 5 18 21 64 97
low
high
middle
15
Binary Search Code
int BinarySearch (int a[], int n, int key)
{
int low, high, mid, i;
low = 0; high = n - 1;
while(low <= high) {
mid = (low + high) / 2;
if(a[mid] = = key)
return mid;
else if(a[mid] > key)
high = mid - 1;
else
low = mid + 1;
}
return -1;
} 16
Sorting
Sorting takes an unordered collection and makes
it an ordered one.
1 2 3 4 5 6
77 42 35 12 101 5
1 2 3 4 5 6
5 12 35 42 77 101
Bubble Sort
Traverse a collection of elements
Move from the front to the end
Bubble the largest value to the end using pair-wise
comparisons and swapping
0 1 2 3 4 5
77 42 35 12 101 5
"Bubbling Up" the Largest
Element
Traverse a collection of elements
Move from the front to the end
Bubble the largest value to the end using pair-wise
comparisons and swapping
0 1 2 3 4 5
42Swap42
77 77 35 12 101 5
"Bubbling Up" the Largest
Element
Traverse a collection of elements
Move from the front to the end
Bubble the largest value to the end using pair-wise
comparisons and swapping
0 1 2 3 4 5
42 35Swap35
77 77 12 101 5
"Bubbling Up" the Largest
Element
Traverse a collection of elements
Move from the front to the end
Bubble the largest value to the end using pair-wise
comparisons and swapping
0 1 2 3 4 5
42 35 12Swap12
77 77 101 5
"Bubbling Up" the Largest
Element
Traverse a collection of elements
Move from the front to the end
Bubble the largest value to the end using pair-wise
comparisons and swapping
0 1 2 3 4 5
42 35 12 77 101 5
No need to swap
"Bubbling Up" the Largest
Element
Traverse a collection of elements
Move from the front to the end
Bubble the largest value to the end using pair-wise
comparisons and swapping
1 2 3 4 5 6
42 35 12 77 5 Swap101
101 5
"Bubbling Up" the Largest
Element
Traverse a collection of elements
Move from the front to the end
Bubble the largest value to the end using pair-wise
comparisons and swapping
0 1 2 3 4 5
42 35 12 77 5 101
35 12 42 5 77 101
N-1
12 35 5 42 77 101
12 5 35 42 77 101
5 12 35 42 77 101
Exchanging Elements: SWAP
void swap(int *a, int *b)
{
int temp;
temp = *a;
*a = *b;
*b = temp;
}
26
Bubble Sort
void BubbleSort(int a[], int n)
{
int i, j, flag = 0;
for(i = 0; flag = = 0 && i < n-1; i++) {
flag = 1;
for(j = 0; j <= n-i-2; j++) {
if(a[j] > a[j+1]) {
swap(&a[j], &a[j+1]);
flag = 0;
}
}
}
} 27
Selection Sort
1. i = 0;
2. Find the index min of the minimum element
between a[i] to a[n-1]
3. Swap a[i] with a[min]
4. i = i +1
5. Goto Step 2 if i != n-1.
0 1 2 3 4 5
77 42 35 12 101 5
28
Selection Sort
0 1 2 3 4 5
77 42 35 12 101 5 Min=1
77 42 35 12 101 5 Min=2
77 42 35 12 101 5 Min=3
77 42 35 12 101 5 Min=3
29
77 42 35 12 101 5 Min=5
5 42 35 12 101 77 Min=3
29
Selection Sort
void SelectionSort(int a[], int n)
{
int i, j,min;
for(i = 0; i < n-1; i++) {
min = i;
for(j = i+1; j < n; j++) {
if(a[j] < a[min])
min = j;
}
swap(&a[i], &a[min]);
}
} 30
Insertion Sort
1. i = 1;
2. Insert the i-th element in the sorted array a[0] to
a[i-1] so that a[0] to a[i] is sorted.
3. i = i +1
4. Goto Step 2 if i != n.
31
Last Iteration of Insertion Sort
0 1 2 3 4 5
5 12 35 42 77 6 Key = 6
5 12 35 42 77 77
5 12 35 42 42 77
5 12 35 35 42 77
32
5 12 12 35 42 77
5 6 12 35 42 77
32
Insertion Sort
void InsertionSort(int a[], int n)
{
int i, j, key;
for(i = 1; i < n; i++) {
key = a[i];
for(j = i-1;j > -1 && a[j] > key; j--)
a[j+1] = a[j];
a[j+1] = key;
}
}
33
How to merge two sorted array?
1 2 3 4 5 6
A 5 26 35 92 101 500
i
1 2 3 4 5
B -9 -1 30 60 101
j
1 2 3 4 5 6 7 8 9 10 11
C 0 0 0 0 0 0 0 0 0 0 0
k 34
How to merge two sorted array?
1 2 3 4 5 6
A 5 26 35 92 101 500
i
1 2 3 4 5
B -9 -1 30 60 101
j
1 2 3 4 5 6 7 8 9 10 11
C -9 0 0 0 0 0 0 0 0 0 0
k 35
How to merge two sorted array?
1 2 3 4 5 6
A 5 26 35 92 101 500
i
1 2 3 4 5
B -9 -1 30 60 101
j
1 2 3 4 5 6 7 8 9 10 11
C -9 -1 0 0 0 0 0 0 0 0 0
k 36
How to merge two sorted array?
1 2 3 4 5 6
A 5 26 35 92 101 500
i
1 2 3 4 5
B -9 -1 30 60 101
j
1 2 3 4 5 6 7 8 9 10 11
C -9 -1 5 0 0 0 0 0 0 0 0
k 37
How to merge two sorted array?
1 2 3 4 5 6
A 5 26 35 92 101 500
i
1 2 3 4 5
B -9 -1 30 60 101
j
1 2 3 4 5 6 7 8 9 10 11
C -9 -1 5 26 0 0 0 0 0 0 0
k 38
How to merge two sorted array?
1 2 3 4 5 6
A 5 26 35 92 101 500
i
1 2 3 4 5
B -9 -1 30 60 101
j
1 2 3 4 5 6 7 8 9 10 11
C -9 -1 5 26 30 0 0 0 0 0 0
k 39
How to merge two sorted array?
1 2 3 4 5 6
A 5 26 35 92 101 500
i
1 2 3 4 5
B -9 -1 30 60 101
j
1 2 3 4 5 6 7 8 9 10 11
C -9 -1 5 26 30 35 0 0 0 0 0
k 40
How to merge two sorted array?
1 2 3 4 5 6
A 5 26 35 92 101 500
i
1 2 3 4 5
B -9 -1 30 60 101
j
1 2 3 4 5 6 7 8 9 10 11
C -9 -1 5 26 30 35 60 0 0 0 0
k 41
How to merge two sorted array?
1 2 3 4 5 6
A 5 26 35 92 101 500
i
1 2 3 4 5
B -9 -1 30 60 101
j
1 2 3 4 5 6 7 8 9 10 11
C -9 -1 5 26 30 35 60 92 0 0 0
k 42
How to merge two sorted array?
1 2 3 4 5 6
A 5 26 35 92 101 500
i
1 2 3 4 5
B -9 -1 30 60 101
j
1 2 3 4 5 6 7 8 9 10 11
C -9 -1 5 26 30 35 60 92 101 0 0
k 43
How to merge two sorted array?
1 2 3 4 5 6
A 5 26 35 92 101 500
i
1 2 3 4 5
B -9 -1 30 60 101
j
1 2 3 4 5 6 7 8 9 10 11
C -9 -1 5 26 30 35 60 92 101 101 0
k 44
How to merge two sorted array?
1 2 3 4 5 6
A 5 26 35 92 101 500
i
1 2 3 4 5
B -9 -1 30 60 101
j
1 2 3 4 5 6 7 8 9 10 11
C -9 -1 5 26 30 35 60 92 101 101 500
k
45
Merge
void merge(int a[], int low, int mid, int high)
{
int i = low, j = mid+1, count = 0;
int b[high-low+1];
while(count < high-low+1 && i <= mid && j <= high)
{
if(a[i] <= a[j])
b[count++] = a[i++];
else
b[count++] = a[j++];
} 46
Merge
1 2 3 4 5 6 7 8
A 92 35 26 101 5 500 101 92
1 2 3 4 5 6 7 8
A 92 35 26 101 5 500 101 92
48
Merge Sort : Conquer
1 2 3 4 5 6 7 8
A 5 26 35 92 92 101 101 500
1 2 3 4 5 6 7 8
A 26 35 92 101 5 92 101 500
1 2 3 4 5 6 7 8
A 35 92 26 101 5 500 92 101
1 2 3 4 5 6 7 8
A 92 35 26 101 5 500 101 92
49
Merge Sort
void mergesort(int a[], int low, int high)
{
if(low < high) {
int mid = (high+low)/2;
mergesort(a, low, mid);
mergesort(a, mid+1, high);
merge(a, low, mid, high);
}
}
50
Quick Sort
0 1 2 3 4 5 6 7
A 2 8 7 1 3 5 6 4
51
i=-1 j=0 1 2 3 4 5 6 7
A 2 8 7 1 3 5 6 4
i=0 j=1 2 3 4 5 6 7
A 2 8 7 1 3 5 6 4
i=0 1 j=2 3 4 5 6 7
A 2 8 7 1 3 5 6 4
0 i=1 2 j=3 4 5 6 7
A 2 1 7 8 3 5 6 4
0 1 i=2 3 j=4 5 6 7
A 2 1 3 8 7 5 6 4
52
0 1 i=2 3 j=4 5 6 7
A 2 1 3 8 7 5 6 4
0 1 i=2 3 4 j=5 6 7
A 2 1 3 8 7 5 6 4
0 1 i=2 3 4 5 j=6 7
A 2 1 3 8 7 5 6 4
0 1 i=2 3 4 5 6 j=7
A 2 1 3 4 7 5 6 8
Partition
int partition(int a[], int low, int high)
{
int i, j;
i = low-1;
for(j = low; j < high; j++) {
if(a[j] < a[high]) {
i++;
swap(&a[i],&a[j]);
}
}
swap(&a[i+1], &a[high]);
return i+1;
} 54
Quick Sort
void quicksort(int a[], int low, int high)
{
if(low < high) {
int pivot = partition(a, low, high);
quicksort(a, low, pivot-1);
quicksort(a, pivot+1, high);
}
}
55