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

02 Sort and Search

The document discusses different sorting and searching algorithms including selection sort, insertion sort, bubble sort, quicksort, merge sort, radix sort, linear search, binary search, ternary search, interpolation search, and Fibonacci search. It provides details on how each algorithm works including code examples and complexity analysis.

Uploaded by

Pham Phuong Thao
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
3 views

02 Sort and Search

The document discusses different sorting and searching algorithms including selection sort, insertion sort, bubble sort, quicksort, merge sort, radix sort, linear search, binary search, ternary search, interpolation search, and Fibonacci search. It provides details on how each algorithm works including code examples and complexity analysis.

Uploaded by

Pham Phuong Thao
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 24

LESSON 1.

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

 Given a sequence of n objects r 1 , r 2 , .., r n .

 Each object r i is associated with a key k i (1≤i ≤n).

 The task of sorting is to build an algorithm that


arranges objects in a certain order of key values.
 As an example, we consider the set of objects to be
sorted as the set of numbers.

Page3
SIMPLE SORTING …

Features:
• Easy ideas
• Simple installation
• High complexity

Some simple sorting algorithms:


• Selection Sort Algorithm.
• Insertion Sort Algorithm.
• Bubble Sort algorithm.

Page4
SELECTION SORT
Selection-Sort . Algorithm

Formats : Selection-Sort( Arr , n);


Actions :
for ( i =0; i <n-1; i ++) {
min_idx = i ;
for ( j = i +1; j<n; j++ ) {
if ( Arr [ min_idx ] > Arr [j] )
min_idx = j;
}
temp = Arr [ i ] ; Arr [ i ] = Arr [ min_idx ]; Arr [ min_idx ] = temp;
End .

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

Formats: Bubble-Sort( Arr , n);


Actions:
for ( i = 0; i < n; i ++) {
check = false;
for (j=0; j<ni-1; j++ ) {
if ( Arr [j] > Arr [j+1] ) {
check = true;
temp = Arr [j]; Arr [j] = Arr [j+1]; Arr [j+1] = temp;
}
}
if(!check) break;
}
End.
Page7
QUICK-SORT
Input :
• The sequence Arr[] starts at position l and ends at h.
• Lower bound of subsequence: l
• Upper bound of subsequence: h
Output:
• The exact position of Arr[h] if the array Arr[] is sorted.
Formats: Partition(Arr, l, h);
Actions:
x = Arr[h]; i = (l - 1);
for ( j = l; j <= h- 1; j++) {
if (Arr[j] <= x){
i++;
swap(&Arr[i], &Arr[j]);
}
}
swap(&Arr[i + 1], &Arr[h]);
return(i + 1);
End.

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.

Formats : Quick-Sort(Arr, l, h);

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).

Quick-Sort algorithm testing : Quick-Sort(Arr, 0, 9);


Arr[] = {10, 27, 15, 29, 21, 11, 14, 18, 12, 17};
Lower bound l=0, upper bound h=9.

p = Partition( Arr,l,h ) Price treat Arr []=?

p=5:l=0, h=9 { 10 , 15 , 11 , 14 , 12 }, ( 17 ) ,{ 29 , 18 , 21 , 27 }

P=2:l=0, h=4 { 10 , 1 1}, (12) , { 14 , 1 5}, ( 17 ),{ 29 , 18 , 21 , 27 }

P=1:l=0, h=1 { 10 , 1 1 },(12), { 14 , 1 5}, ( 17 ),{ 29 , 18 , 21 , 27 }

P=4: l=3, h=4 { 10 , 1 1},(12), { 14 , 1 5 }, ( 17 ),{ 29 , 18 , 21 , 27 }

P=8: l=6, h=9 { 10 , 1 1},(12), { 14 , 1 5 }, ( 17 ),{18,21},( 2 7 ),{ 2 9}

P=7:l=6, h=7 { 10 , 1 1},(12), { 14 , 1 5 }, ( 17 ),{18, 21 },( 2 7 ),{ 2 9}

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

Algorithm complexity: O(nlog n).


algorithm test : Merge-Sort(Arr,0,7)
Input :
Arr[] = {38, 27, 43, 3, 9, 82, 10}; n = 7;

Step Conclude fruit Arr []=?


first Arr [] = { 27 , 38 , 43 , 3 , 9 , 82 , 10 }

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

void radixSort( int[] A ) {


int i, m = A [0], B[n], exp = 1, n = A .length;
for (i = 1; i < n; i++) //find the largest number in the sequence
if ( A [i] > m)
m = A [i];
while (m / exp > 0) {
int[] bucket = new int[10];
for (i = 0; i < n; i++) // count distribution of numbers from 0..9
bucket[( A [i] / exp) % 10]++;
for (i = 1; i < 10; i++)
bucket[i] += bucket[i - 1];
for (i = n - 1; i >= 0; i--)
B [--bucket[( A [i] / exp) % 10]] = A [i];
for (i = 0; i < n; i++)
A [i] = B [i];
exp *= 10;
}
}

Page16
SEARCHING

Page17
SEARCHING

1. Linear Search
2. Binary search
3. Interpolation search
4. Fibonacci search

Page18
SEQUENTIAL SEARCH

Sequential -Search( int A[], int n, int x) {


for ( i =0; i <n; i ++ ) {
if ( x ==A[ i ] )
return ( i );
}
return(-1);
}

Page19
BINARY SEARCH

int Binary-Search ( int A[], int n, int x) {


int low = 0;
int high = n-1;
int mid = (low+high)/2;
while ( low <= high) {
if ( x > A[mid] )
low = mid + 1;
else if ( x < A[i] )
high = mid -1;
else
return(mid);
mid = (low + high)/2;
}
return(-1);
}
Page20
TERNARY BINARY SEARCH

int ternarySearch (int[] A, int value, int start, int end) {


if (start > end) return -1;
/* Split the sequence into 3 segments: [start..mid1], [mid1..mid2], [mid 2..end]
*/
int mid1 = start + (end-start) / 3;
int mid2 = start + 2*(end-start) / 3;
if (A[mid1] == value) return mid1;
if (A[mid2] == value) return mid2;
if (value < A[mid1])
return ternarySearch(A, value, start, mid1-1);
if (value > A[mid2])
return ternarySearch (A, value, mid2+1, end);
return ternarySearch(A, value, mid1,mid2);
}
Page21
INTERPOLUTION SEARCH
int Interpolation-Search(int A[], int x, int n){
int low = 0,high = n – 1, mid;
while ( A[low] <= x && A[high] >= x){
if (A[high] - A[low] == 0) return (low + high)/2;
mid = low + ((x - A[low]) * (high - low)) / (A[high] - A[low]);
if (A[mid] < x) low = mid + 1;
else if (A[mid] > x) high = mid - 1;
else return mid;
}
if (A[low] == x)
return low;
return -1;
}

Page22
FIBONACCI SEARCH

int fibsearch(int A[], int n, int x){


// Step 1 . Find the position k is the last position to fib[k] >n.
int inf = 0, pos, k, kk= -1, nn = -1;
if (nn != n) { k = 0;
while (fib[k] < n) k++;
}
else k = kk; //k = - 1 and no more searching

// 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

Step k=? inf =? Fib[k]=? Pos =?


first 6 0 8 8
2 5 0 5 5
3 3 6 2 8
x = 67 4 2 6 first 7
5 first 6 first 7
Page24
6 0 6 0 6

You might also like