Chapter 2 Simple Sorting and Searching Algorithms
Chapter 2 Simple Sorting and Searching Algorithms
Chapter 2 Simple Sorting and Searching Algorithms
Chapter 2
Simple Sorting and Searching Algorithms
Jimma, Ethiopia.
Outline
A comparison is simply comparing one value in a list with another, and a data
movement (swapping) is an assignment.
A = { 3 1 6 2 1 3 4 5 9 0 }
A = { 0 1 1 2 3 3 4 5 6 9 }
Cont...
Original array:
1 23 2 56 9 8 10 100
1 1 23 2 56 9 8 10 100
2 1 23 2 56 9 8 10 100
3 1 2 23 56 9 8 10 100
4 1 2 23 56 9 8 10 100 Sort the following elements using
5 1 2 23 9 56 8 10 100 bubble sort algorithm
6 1 2 23 9 8 56 10 100
7 1 2 23 9 8 10 56 100 8 5 7 3 2
---- finish the first traversal ----
---- start again ----
8 1 2 23 9 8 10 56 100
9 1 2 9 23 8 10 56 100
10 1 2 9 8 23 10 56 100
11 1 2 9 8 10 23 56 100
---- finish the second traversal ----
---- start again ----
Algorithm for Bubble Sort
Pseudo code
1.1 If E1 E2
- OK
(n-1)+(n-2)+…+1= n*(n-1)/2=n2/2
(n-1)+(n-2)+…+1= O(n2)
(n-1)+(n-2)+…+1= O(n2)
Each element is swapped directly with the element that occupies its correct position
First find the smallest in the array and exchange it with the element in the first
position,
then find the second smallest element and exchange it with the element in the
second position, and
At ith step, first i element is sorted. All element are bigger that first i elements
Example
1. for i 0 to n - 1
2. ki
5. end for
7. end for
Implementation
(n-1)+(n-2)+…+1= n*(n-1)/2=n2/2
Insertion sort keeps making the left side of the array sorted until the whole array
is sorted.
A[ i ] is inserted in its proper position in the ith iteration in the sorted subarray
A[0 .. i-1]
In the ith step, the elements from index i-1 down to 0 are scanned, each time
comparing A[i] with the element at the correct position.
23 17 45 18 12 22
20
Example: sorting numbered cards
23 17 45 18 12 22
1 2 3 4 5 6
1 2 3 4 5 6
21
Example: sorting numbered cards
17 45 18 12 22
1 2 3 4 5 6
23
1 2 3 4 5 6
22
Example: sorting numbered cards
45 18 12 22
1 2 3 4 5 6
17 23
1 2 3 4 5 6
23
Example: sorting numbered cards
18 12 22
1 2 3 4 5 6
17 23 45
1 2 3 4 5 6
24
Example: sorting numbered cards
12 22
1 2 3 4 5 6
17 18 23 45
1 2 3 4 5 6
25
Example: sorting numbered cards
1 2 3 4 5 6
12 17 18 22 23 45
1 2 3 4 5 6
26
Algorithm: INSERTION SORT
int temp;
for(int i=1;i<n;i++){
temp=list[i];
j=i;
while(j>0 && list[j-1]>temp) { // work backwards through the array finding where temp
should go
list[j]=list[j-1];
j--;
list[j]=temp;
} //end of insertion_sort
Analysis of Insertion Sort
1+ 2+ 3+ …+ (n-1)= n*(n-1)/2
1+ 2+ 3+ …+ (n-1)= O(n2)
Empirically it’s known that Insertion sort is over twice as fast as the bubble sort
and is just as easy to implement as the selection sort.
Searching Algorithm
Linear Searching
Binary Searching
Cont...
Binary Search
Sequential (Linear) Search
Algorithm:
Search list from the beginning until key is found or end of list
reached.
Cont...
Each element of an array is read one by one sequentially and it is compared with
the search key.
Let A be an array of having n elements, A[0], A[1], A[2], ......, A[n-1]. “item” is
the element to be searched. Then this algorithm will find the location “loc” of
item in A.
Set loc = – 1, if the search is unsuccessful.
Example
List index 0 1 2 3 4 5 6 7
Data C A E F N Q R K
in searching a record.
In the best case, the desired element is present in the first position of the
In the Average case, the desired element is found in the half position of the
In the worst case the desired element is present in the nth (or last) position
Assumption
the given array is a sorted one, otherwise first we have to sort the array
elements.
Binary search begins by examining the value in the middle position of the array;
call this position mid and the corresponding value k mid.
1. Find the middle element of the array (i.e., n/2 is the middle element if the
array or the sub-array contains n elements).
2. Compare the middle element with the data to be searched, and then there
are following three cases.
1. If it is a desired element, then search is successful.
2. If it is greater than desired data, then search only the first half of the
array, i.e. the elements which come to the left side of the middle
element.
3. If it is less than the desired data, then search only the second half of
the array, i.e., the elements which come to the right side of the middle
element.
3. Repeat the same step until an element is found or exhaust the search area.
Example
Algorithm for Binary Search
1. input “n” elements of the array and the element going to be searched “x”;
2. initialize i=0 and repeat through step 3 if (i<n)
3. do step “ 4” while left < right and flag =0
4. mid=(right+left)/2;
if(array[mid]==x){
flag=1
return mid
}
if(array[mid]<x)
left=mid+1
otherwise
right= mid-1
5. if flag = 0 index= -1 otherwise index=mid
6. return index
Implementation
int Binary_Search(int list[],int k){
if(found==0)
int left=0; index=-1;
int right=n-1; else
int found=0; index=mid;
do{ return index;
mid=(left+right)/2; }
if(key==list[mid])
found=1;
else{
if(key<list[mid])
right=mid-1;
else
left=mid+1;
}
}while(found==0&&left<right);
Time Complexity
Observe that in each comparison the size of the search area is reduced by
half.
Hence in the worst case, at most logn comparisons required. So f(n)= O([log n ]
+1).
The End !!
45