3-Search and Sort
3-Search and Sort
• Searching
• Sequential/Linear Search
• Binary Search
• Sorting
• Insertion Sort
• Selection Sort
• Bubble Sort
• Quick Sort
• Merge Sort
• Searching
• Sequential Search
• Binary Search
• Sorting
• Insertion Sort
• Selection Sort
• Bubble Sort
• Quick Sort
• Merge Sort
• Linear search:
• Find the position of the value x in the array a having n elements.
Search the value of x=6 in the array a having 8 items:
5 9 2 7 6 5 2 5
There may be
i=0 1 2 3 4 n comparisons
performed.
Search the value of x=12 in the array a having 8 items:
5 9 2 7 6 5 2 5
i=0 1 2 3 4 5 6 7 -1
Linear search
• Analysis
• Example: 44, 55, 12, 42, 94, 18, 06, 67
n 1 n 1
( i 1) / n
i0 2
• Binary Search
• Input: sorted list
(i.e. list[0].key list[1].key ... list[n-1].key )
• Compare searchNum and list[middle].key, where middle = (n-1)/2,
there are three possible outcomes:
• searchNum < list[middle].key
Search: list[0] … list[middle-1]
• searchNum = list[middle].key
Search terminal successfully: return middle
• searchNum > list[middle].key
Search: list[middle+1] … list[n-1]
• Binary search:
Condition for application:
Values in the array were
sorted.
int binarySearch ( int x, int a[],
int n)
{ int i=0, j= n-1, c ;
while (i<=j)
{ c= (i+j)/2;
if ( x== a[c] ) return c ;
if (x < a[c] ) j = c-1;
else i = c +1;
}
return -1;
} return c (=7) i>j return -1
Data Structures & Algorithms 9
…Searching - Binary Search
• Example
• Input : 4, 15, 17, 26, 30, 46, 48, 56, 58, 82, 90, 95
[5]
46
[2] [8]
17 58
Binary search makes no more than O(log n) comparisons (i.e. height of tree)
Data Structures & Algorithms 10
…Searching - Binary Search
Binary search
int BinarySearch(int x, int a[], int n){
int i=0, j=n-1, c;
while(i <= j ) {
c = (i+j)/2;
if (x == a[c]) return c;
else if (x < a[c]) j = c-1;
else i = c+1;
}
return -1;
}
Complexity of algorithms
Linear Search: O(n)
O(log2n) = O(log n)
Data Structures & Algorithms 13
CONTENT
• Searching
• Sequential Search
• Binary Search
• Sorting
• Insertion Sort
• Selection Sort
• Bubble Sort
• Quick Sort
• Merge Sort
Data Structures & Algorithms 14
Sorting
• List Verification
• Determine if all elements on one list appear on another list
– Assume the 2 lists have m and n elements, respectively, and there are no
repetitions
• 1st approach (O(mn) time)
1. Assume both lists are not sorted
2. for each element in the 1st list do
3. search the element in the 2nd list
4. end for
• Insertion sort
• n records are stored in an array A
• n-1 steps in the insertion sorting algorithm
• During step i, i-1 elements A[0],A[1], …, A[i-1] are in sorted order
• We need to insert A[i] into A[0],A[1], …, A[i-1] such that these i
elements are sorted
• the element A[i] is compared with A[0],A[1], …,A[i-1], one by one,
until a place, say k, is decided for A[i]
• the elements A[k],A[k+1], …, A[i-1] are moved to the right one
position and A[i] is copied to A[k]
Insertion sort
1 2356
1 5 11 15 19 26 48 59 61 77
Data Structures & Algorithms 22
…Sorting - Insertion sort
• Analysis
• During step i, we need at most i+1 comparisons and movements
• In the worst case, we need (1+2+…+n-1) = n(n-1)/2= O(n2) comparisons and
movements
• a record A[i] is said left-out-of-order (LOO) if A[i] < max{A[0];A[1];A[2];...;A[i-1]}
• Each LOO record induces at most n comparisons
• Each non-LOO record induces 1 comparison
• Suppose there are k LOO records and n - k non-LOO records
• We need at most kn + (n - k) comparisons
• Since k <= n, time is O(n2)
• Variation
• Binary insertion sort
• sequential search --> binary search
• reduce # of comparisons
• # of moves unchanged
• List insertion sort
• array --> linked list
• sequential search
• no movement, adjust pointers only
• Selection Sort
• n records are stored in an array A
• loop n-1 steps in the selection sorting algorithm
• During step i, A[0],A[1], …, A[i-1] are the smallest i elements,
arranged in sorted order
• Then the smallest among A[i],A[i+1], ... ,A[n-1] is selected and is
exchanged with A[i]
• Analysis
• During step i, there are at most n - i comparisons
• In the worst case, there are
(n - 1 + n - 2 + ... + 1) = n(n - 1)/2 = O(n2) comparisons
• Bubble Sort
• n records are stored in an array A
• n steps in the bubble sorting algorithm
• During step i, A[0],A[1], …, A[i-1] are the smallest i elements,
arranged in sorted order
• Then among A[i],A[i+1], ... ,A[n-1], each pair ((A[n-2], A[n-1]) , …)
will be exchanged if they are out of order, so A[i] will be the smallest
• At step i, the smallest element bubbles
Bubble sort
Example
5 5 5 5 5 0 0 0 0 0 0 0 0 0 0 0
1 1 1 1 0 5 5 5 5 1 1 1 1 1 1 1
7 7 7 0 1 1 1 1 1 5 5 5 4 4 4 4
4 4 0 7 7 7 7 4 4 4 4 4 5 5 5 5
0 0 4 4 4 4 4 7 7 7 6 6 6 6 6 6
6 6 6 6 6 6 6 6 6 6 7 7 7 7 7 7
• Quick Sort
• n records are stored in an array A
• Choose a pivot A[i]
• Re-arrange the list so that A[0],A[1], …, A[i-1] are smaller than A[i]
while A[i+1],A[i+2], ..., A[n-1] are all greater than A[i]
• Then, recursively apply quick sort to the first half A[0],A[1], …, A[i-1]
and the second half A[i+1],A[i+2], ..., A[n-1], respectively
• Sort phase:
• Conquers the halves: apply the same algorithm to each half
• Example
• Analysis
• Time complexity
• Worst case: O(n2)
• Best case: O(nlogn)
• Average case: O(nlogn)
• Space complexity
• Worst case: O(n)
• Best case: O(logn)
• Average case: O(logn)
• Unstable
• Merge Sort
Divide and Conquer algorithm
1. Cut the list in 2 halves
2. Sort each half, respectively, probably by merge sort recursively
3. Merge the 2 sorted halves
How to merge two sorted lists (list[i], ... , list[m] and list[m+1], ... , list[n])
into single sorted list, (sorted[i], ... , sorted[n])?
i j
b bi cj c
bi < cj
true, write bi false, write cj
i = i+1 j = j+1
result
• We then merge the n/2 lists pair-wise, and so on, until a single list
remains
} 2l 2l ...
} 4l ...
Data Structures & Algorithms 49
…Sorting - Merge Sort
• Example
• Analysis
• Time complexity
• T(n) = 2T(n/2) + n-1
• O(n log n) in time
• O(n) in space
Can be improved by an approach with O(1) in space
• Stable
• Searching
• Sequential Search
• Binary Search
• Sorting
• Insertion Sort
• Selection Sort
• Bubble Sort
• Quick Sort
• Merge Sort