Searching Sorting
Searching Sorting
!int i;!
i = low;!
if (s[i] == key) {!
return (i);!
}!
else {!
return(-1);!
}!
}!
Binary Search
REPEAT
get middle element of list
if middle element < key
then reset bounds to make the right sublist
the list to be searched
else reset bounds to make the left sublist
the list to be searched
UNTIL list is empty or key is found
Implementation of
Binary_Search in Modula
CONST n = 100;
REPEAT
mid := (first + last) DIV 2;
IF list[mid] < key
THEN
first := mid + 1
ELSE
last := mid - 1
END
UNTIL (first > last) OR (list[mid] = key);
Implementation of
Binary_Search in Modula
found := key = list[mid];
index := mid
END binary_search
Binary Search
A B D F G J K M O P R
first:
last:
mid:
list[mid]: first mid last
key: P
Binary Search
A B D F G J K M O P R
first: 1
last: 11
mid: 6
list[mid]: J
key: P
Binary Search
A B D F G J K M O P R
first: 1 7
last: 11 11
mid: 6 9
list[mid]: J O
key: P P
Binary Search
A B D F G J K M O P R
first last
mid
first: 1 7 10
last: 11 11 11
mid: 6 9 10
list[mid]: J O P FOUND!
key: P P P
Binary Search
A B D F G J K M O P R
first:
last:
mid:
list[mid]: first mid last
key: E
Binary Search
A B D F G J K M O P R
first: 1
last: 11
mid: 6
list[mid]: J
key: E
Binary Search
A B D F G J K M O P R
first: 1 1
last: 11 5
mid: 6 3
list[mid]: J D
key: E E
Binary Search
A B D F G J K M O P R
first last
mid
first: 1 1 4
last: 11 5 5
mid: 6 3 4
list[mid]: J D F
key: E E E
Binary Search
A B D F G J K M O P R
last first
mid
first: 1 1 4 4
last: 11 5 5 3 first > last: NOT FOUND!
mid: 6 3 4 3
list[mid]: J D F D
key: E E E E
Implementation of binary search in C
(recursive approach)
typedef char item_type;!
!int mid;!
10 9 8 11 4
Bubble Sort
10
Swap
9
8
11
4
Bubble Sort
10 9
9 10
8 8
11 11
4 4
Bubble Sort
10 9
9 10
Swap
8 8
11 11
4 4
Bubble Sort
10 9 9
9 10 8
8 8 10
11 11 11
4 4 4
Bubble Sort
10 9 9
Swap
9 10 8
8 8 10
11 11 11
4 4 4
Bubble Sort
10 9 9 8
9 10 8 9
8 8 10 10
11 11 11 11
4 4 4 4
Bubble Sort
10 9 9 8
9 10 8 9
8 8 10 10
No Swap
11 11 11 11
4 4 4 4
Bubble Sort
10 9 9 8 8
9 10 8 9 9
8 8 10 10 10
11 11 11 11 11
Swap
4 4 4 4 4
Bubble Sort
10 9 9 8 8 8
9 10 8 9 9 9
8 8 10 10 10 10
11 11 11 11 11 4
4 4 4 4 4 11
Bubble Sort
10 9 9 8 8 8
9 10 8 9 9 9
8 8 10 10 10 10
Swap
11 11 11 11 11 4
4 4 4 4 4 11
Bubble Sort
10 9 9 8 8 8 8
9 10 8 9 9 9 9
8 8 10 10 10 10 4
11 11 11 11 11 4 10
4 4 4 4 4 11 11
Bubble Sort
10 9 9 8 8 8 8
9 10 8 9 9 9 9
Swap
8 8 10 10 10 10 4
11 11 11 11 11 4 10
4 4 4 4 4 11 11
Bubble Sort
10 9 9 8 8 8 8 8
9 10 8 9 9 9 9 4
8 8 10 10 10 10 4 9
11 11 11 11 11 4 10 10
4 4 4 4 4 11 11 11
Bubble Sort
Swap
10 9 9 8 8 8 8 8
9 10 8 9 9 9 9 4
8 8 10 10 10 10 4 9
11 11 11 11 11 4 10 10
4 4 4 4 4 11 11 11
Bubble Sort
10 9 9 8 8 8 8 4
9 10 8 9 9 9 9 8
8 8 10 10 10 10 4 9
11 11 11 11 11 4 10 10
4 4 4 4 4 11 11 11
Implementation of
Bubble_Sort()
Int bubble_sort(int *a, int size) {
int i,j, temp;
/* swap */
temp = a[j+1];
a[j+1] = a[j];
a[j] = temp;
}
}
}
}
Bubble Sort
• A few observations:
– we don’t usually sort numbers; we usually
sort records with keys
» the key can be a number
» or the key could be a string
» the record would be represented with a struct
– The swap should be done with a function
(so that a record can be swapped)
Bubble Sort
•
write a driver program to test:
– the bubble sort with a swap function
– the bubble sort with structures
– compute the order of time complexity of the
bubble sort
Selection Sort
Initial Array 29 10 14 37 13
DataType temp;!
int index_of_largest, index, last;!
If anything to be partitioned
choose a pivot
DO
scan from left to right until we find an element
> pivot: i points to it
IF i < j
exchange ith and jth element
WHILE i <= j
Implementation of Quicksort()
exhange pivot and jth element
10 9 8 11 4 99
sentinel
Quicksort
10 9 8 11 4 99
QS(A, , )
L:
R:
i: i j
j:
pivot:
Quicksort
10 9 8 11 4 99
i j
QS(A,1,6)
L: 1
R: 6
i: 1
j: 6
pivot: 10
Quicksort
10 9 8 11 4 99
i j
QS(A,1,6)
L: 1
R: 6
i: 1 2 3 4
j: 6 5
pivot: 10
Quicksort
10 9 8 4 11 99
i j
QS(A,1,6)
L: 1
R: 6
i: 1 2 3 4
j: 6 5
pivot: 10
Quicksort
10 9 8 4 11 99
j i
QS(A,1,6)
L: 1
R: 6
i: 1 2 3 4 5
j: 6 5 4
pivot: 10
Quicksort
4 9 8 10 11 99
j i
QS(A,1,6)
L: 1
R: 6
i: 1 2 3 4 5
j: 6 5 4
pivot: 10
Quicksort
4 9 8 10 11 99
i j
L: 1 L: 1 L: 5
R: 6 R: 4 R: 6
i: 1 2 3 4 5 i: i:
j: 6 5 4 j: j:
pivot: 10 pivot: 4 pivot: 11
Quicksort
4 9 8 10 11 99
i j
QS(A,1,4) QS(A,5,6)
L: 1 L: 5
R: 4 R: 6
i: 1 i: 5
j: 4 j: 6
pivot: 4 pivot: 11
Quicksort
4 9 8 10 11 99
j i
QS(A,1,4) QS(A,5,6)
L: 1 L: 5
R: 4 R: 6
i: 1 2 i: 5
j: 4 3 2 1 j: 6
pivot: 4 pivot: 11
Quicksort
4 9 8 10 11 99
i j
L: 1 L: 1 L: 2 L: 5
R: 4 R: 1 R: 4 R: 6
i: 1 2 i: i: i: 5
j: 4 3 2 1 j: j: j: 6
pivot: 4 pivot: 4 pivot: 9 pivot: 11
Quicksort
4 9 8 10 11 99
i j
L: 1 L: 2 L: 5
R: 1 R: 4 R: 6
i: i: i: 5
j: j: j: 6
pivot: 4 pivot: 9 pivot: 11
Quicksort
4 9 8 10 11 99
i j
QS(A,2,4) QS(A,5,6)
L: 2 L: 5
R: 4 R: 6
i: 2 i: 5
j: 4 j: 6
pivot: 9 pivot: 11
Quicksort
4 9 8 10 11 99
j i
QS(A,2,4) QS(A,5,6)
L: 2 L: 5
R: 4 R: 6
i: 2 3 4 i: 5
j: 4 3 j: 6
pivot: 9 pivot: 11
Quicksort
4 8 9 10 11 99
i j
L: 2 L: 2 L: 4 L: 5
R: 4 R: 3 R: 4 R: 6
i: 2 3 4 i: i: i: 5
j: 4 3 j: j: j: 6
pivot: 9 pivot: 8 pivot: 10 pivot: 11
Quicksort
4 8 9 10 11 99
i j
L: 2 L: 4 L: 5
R: 3 R: 4 R: 6
i: 2 i: i: 5
j: 3 j: j: 6
pivot: 8 pivot: 10 pivot: 11
Quicksort
4 8 9 10 11 99
j i
L: 2 L: 4 L: 5
R: 3 R: 4 R: 6
i: 2 3 i: i: 5
j: 3 2 j: j: 6
pivot: 8 pivot: 10 pivot: 11
Quicksort
4 8 9 10 11 99
i j
L: 2 L: 2 L: 3 L: 4 L: 5
R: 3 R: 2 R: 3 R: 4 R: 6
i: 2 3 i: i: i: i: 5
j: 3 2 j: j: j: j: 6
pivot: 8 pivot: 8 pivot: 9 pivot: 10 pivot:
11
Quicksort
4 8 9 10 11 99
i j
QS(A,4,4) QS(A,5,6)
L: 4 L: 5
R: 4 R: 6
i: i: 5
j: j: 6
pivot: 10 pivot:
11
Quicksort
4 8 9 10 11 99
i j
L: 5 L: 5 L: 6
R: 6 R: 5 R: 6
i: 5 i: i:
j: 6 j: j:
pivot: 11 pivot: 11 pivot: 99
Partitioning the Array
Alg. PARTITION (A, p, r)
r
x A[p]
p
1.
A: 5 3 2 6 4 1 3 7
2. ip–1
3. jr+1 i j
A[p…q] ≤ A[q+1…r]
4. while TRUE
5. do repeat j j – 1 A: ap ar
6. until A[j] ≤ x
7. do repeat i i + 1 j=q i
8. until A[i] ≥ x
Each element is
9. if i < j visited once!
10. then exchange A[i] A[j] Running time: O(n)
n=r–p+1
11. else return j
Recurrence
if p < r
then q PARTITION(A, p, r)
QUICKSORT (A, p, q)
Recurrence:
T(n) = T(q) + T(n – q) + n
Worst Case Partitioning
• Worst-case partitioning
– One region has one element and the other has n – 1 elements
– Maximally unbalanced
n n
• Recurrence: q=1 1 n-1 n
1 n-2 n-1
T(n) = T(1) + T(n – 1) + n,
n 1 n-3 n-2
T(1) = O(1) 1
2 3
T(n) = T(n – 1) + n
1 1 2
O(n2)
T(1) = c1 If n-k=1=>k=n
=T(1)+n2-n(n-1)/2
=>O(n2)
Best Case Partitioning
• Best-case partitioning
– Partitioning produces two regions of size n/2
• Recurrence: q=n/2
T(n) = 2T(n/2) + n,
T(n) = O(nlgn) (Master theorem)
Best Case Partitioning
• Best-case partitioning
– Partitioning produces two regions of size n/2
• Recurrence: q=n/2
T(n) = 2T(n/2) + n & T(1)=c1
=4T(n/4) + 2n
=8T(n/8) + 3n
=2kT(n/2k) + kn
n/2k =1 => k=log2n
=2log2nT(1) + log2n . n
= n.c1 + nlog2n
=> O(nlogn)