02 Sort and Search
02 Sort and Search
SORTING AND
SEARCHING
Page1
ORDER ALTERNATIVES
1.Sorting problem
2.Simple Sorting Algorithms
3.Quick-Sort . Algorithm
4.Merge-sort algorithm
5.Radix-Sort . Algorithm
Page2
SORTING PROBLEM
Page3
SIMPLE SORTING …
Features:
• Easy ideas
• Simple installation
• High complexity
Page4
SELECTION SORT
Selection-Sort . Algorithm
Page5
INSERTION SORT
Input:
• Sequence of objects (numbers): Arr[0], Arr[1],..,Arr[n-1].
• Number of objects to sort: n.
Output:
• Sorted array of objects (numbers): Arr[0], Arr[1],..,Arr[n-1].
Formats: Insertion-Sort(Arr, n);
Actions:
for (i = 1; i < n; i++) {
key = Arr[i];
j = i-1;
while (j >= 0 && Arr[j] > key) {
Arr[j+1] = Arr[j];
j = j-1;
}
Arr[j+1] = key;
}
End.
Page6
BUBBLE SORT
Page8
QUICK-SORT
Input :
• The sequence Arr[] has n elements.
• Lower bound of the sequence: l.
• Upper bound of the sequence : h
Output :
• The array Arr[] is sorted.
Actions :
if( l<h) {
p = Partition(Arr, l, h);
Quick-Sort(Arr, l, p-1);
Quick-Sort(Arr, p+1, h);
}
End .
Page9
QUICK-SORT
Algorithmic complexity:
• Worst case: O(n 2 ).
• Best case: O(n log(n).
p=5:l=0, h=9 { 10 , 15 , 11 , 14 , 12 }, ( 17 ) ,{ 29 , 18 , 21 , 27 }
Conclude thesis Range Okay about to Arr [] ={ 10, 11, 12, 14, 15, 17, 18, 21, 27, 29} Page10
MERGE – SORT
For two half belong to one Range Arr [1,..,m] and Arr [m+1,..,r] already Okay
about to arrange .
Duties service mine is _ fit best two half belong to Range Arr [1,..,m] and Arr
[m+1,..,r] to return Fort one Range Arr [1, 2,..,r] also Okay about to arrange .
Page11
MERGE – SORT
Merge algorithm test:
Input : Arr[] = { (19, 17), ( 20, 22, 24 ), ( 15, 18, 23, 25 ), (35, 28, 13)}
l = 2, m = 4, r = 8.
Output : Arr[] = { (19, 17), ( 15, 18, 20, 22, 23, 24, 25 ), (35, 28, 13)}
Calculation :
n1 = m-l+1 = 3; n2 = rm= 5.
L = {20, 22, 24}.
R = {15, 18, 23, 25}.
i =? j=?, k=? (L[ i ]<=R[j])? Arr [k] =?
i = 0; j=0, k=2 (20<=15):No Arr [2] = 15;
i = 0; j=1, k=3 (20<=18):No Arr [3] = 18;
i = 0; j=2, k=4 (20<=23):Yes Arr [4] = 20;
i = 1; j=2, k=5 (22<=23):Yes Arr [5] = 22;
i = 2; j=2, k=6 (24<=23):No Arr [6] = 23;
i = 2; j=3, k=7 (24<=25):Yes Arr [7] = 24;
(( i =3)<3): No; j=3; k=7
Conclude result : Arr [] = { (19,17),( 15,18,20, 22, 23, 24, 25 ), (35, 28, 13)}
Page12
MERGE – SORT
Input
• Sequence : Arr[];
• Lower margin: l;
• Close to above m;
Output :
• The array Arr[] is sorted in ascending order.
Formats : Merge-Sort(Arr, l, r);
Actions :
if ( l< r ) {
m = (l + r -1) / 2;
Merge-Sort(Arr, l, m);
Merge-Sort(Arr, m+1, r);
Merge(Arr, l, m, r);
}
End .
Page13
MERGE – SORT
2 Arr [] = { 27 , 38 , 3 , 43 , 9 , 82 , 10 }
3 Arr [] = { 3 , 27 , 38 , 43 , 9 , 82 , 10 }
4 Arr [] = { 3 , 27 , 38 , 43 , 9 , 82 , 10 }
5 Arr [] = { 3 , 27 , 38 , 43 , 9 , 10 , 82 }
6 Arr []= { 3 , 9 , 10 , 27 , 38 , 43 , 82 }
Page14
RADIX-SORT
Suppose we need to sort any sequence of integers, for example A[] = { 570, 821,
742, 563, 744, 953, 166, 817, 638, 639}.
Idea: Sort by digits from the units order up to the end
Illustration:
57 0 82 1 74 2 56 3 95 3 74 4 16 6 81 7 63 8 63 9
8 17 8 21 6 38 6 39 7 42 7 44 9 53 9 63 1 66 5 70
166 570 638 639 742 744 817 821 953 963
Page15
RADIX-SORT
Page16
SEARCHING
Page17
SEARCHING
1. Linear Search
2. Binary search
3. Interpolation search
4. Fibonacci search
Page18
SEQUENTIAL SEARCH
Page19
BINARY SEARCH
Page22
FIBONACCI SEARCH
// Step 2 . Find the position of x in A based on the jump of the number Fib
while (k > 0) {
pos = inf + fib[--k];
if ((pos >= n) || (x < A[pos]));
else if (x > A[pos]){
inf = pos + 1; k--;
}
else
return pos;
}
return -1; // x is not present in A[]
}
Page23
FIBONACCI SEARCH
Algorithm testing:
Assume the sequence A[] = {-100, -50, 2, 3 , 45, 56 ,67 ,78 , 89.9 , 100 , 101}.
n=12, x =-50 and x = 67.
Algorithm implementation :
Step 1 . Find k is the last position to fib[k] >n: k=7 because fib[7]=13.
Step 2 (repeat):
Step k=? inf =? Fib[k]=? Pos =?
first 6 0 8 8
x = -50 2 5 0 5 5
3 4 0 3 3
4 3 0 2 2
5 2 0 first first