1
CHAPTER THREE
Simple sorting and searching
algorithms
Mulugeta G.
2
Simple Searching Algorithms
• Searching
• is a process of finding an element in a list of items or
determining that the item is not in the list.
• Is a process of finding the location of an element within
the data structure
• There are two simple Searching algorithms:
A. Linear (Sequential) Search
B. Binary Search
3
Simple Searching Algorithms
• Linear Search (sequential Search)
• can be implemented on the unsorted list
• search is made over all items one by one
• Every item is checked and if a match is found then that
particular item is returned, otherwise the search
continues till the end of the data collection.
• It compares the element to be searched with all the
elements in an array, if the match is found, then it returns
the index of the element else it returns -1.
4
Simple Searching Algorithms
• Linear Search : How it works?
• In a linear search, we start with top (beginning) of the list
and compare the element at top with the key.
• If we have a match, the search terminates, and the index
number is returned. If not, we go on the next element in
the list. If we reach the end of the list without finding a
match, we return -1.
5
Simple Searching Algorithms
• Linear Search : Pseudocode
procedure linear_search (list, value)
for each item in the list
if match item == value
return the item's location
else
return the item is not found
end if
end for
end procedure
6
Simple Searching Algorithms
• Linear Search : Implementation
int linearSearch(int a[], int n, int val)
{
for (int i = 0; i < n; i++)
{
if (a[i] == val)
return i;
}
return -1;
}
7
Simple Searching Algorithms
• Linear Search : Implementation
int main() {
int a[] = {69, 39, 29, 10, 56, 40, 24, 13, 51}; // given array
int val = 56; // value to be searched
int n = sizeof(a) / sizeof(a[0]); // size of array
int res = linearSearch(a, n, val); // Store result
cout<<"The elements of the array are - ";
for (int i = 0; i < n; i++)
cout<<a[i]<<" ";
cout<<"\nElement to be searched is - "<<val;
if (res == -1)
cout<<"\nElement is not present in the array";
else
cout<<"\nElement is present at "<<res<<" position of array";
return 0; }
8
Simple Searching Algorithms
• Linear Search : Complexity Analysis
• the best case occurs when the element we are looking is located at the first position of the array.
• the worst case occurs when the element we are looking is present at the end of the array.
• The worst-case in linear search could be when the target element is not present in the given
array, and we must traverse the entire array.
Case Time Complexity
Best Case O(1)
Average Case O(n)
Worst Case O(n)
• However, The time complexity of linear search is O(n) because every element in the
array is compared only once.
9
Simple Searching Algorithms
• Binary Search
• is the simplest algorithm that searches the element very
quickly and works only on an ordered list.
• works on the principle of divide and conquer approach.
• It is used to search the element from the sorted list
• The elements must be stored in sequential order or the
sorted manner to implement the binary algorithm.
• Binary search cannot be implemented if the elements are
stored in a random manner.
• It is used to find the middle element of the list.
10
Simple Searching Algorithms
• Binary Search: the basic idea is
• Locate midpoint of array to search
• Determine if target is in lower half or upper half of an
array.
• If in lower half, make this half the array to search
• If in the upper half, make this half the array to search
• Loop back to step 1 until the size of the array to search is one,
and this element does not match, in which case return –1.
11
Simple Searching Algorithms
• Binary Search: How it works?
• In a binary search, we look for the key in the middle of the list.
If we get a match, the search is over.
• If the key is greater than the element in the middle of the list, we
make the top (upper) half the list to search.
• If the key is smaller, we make the bottom (lower) half the list to
search.
• Repeat the above three steps until one element remains.
• If this element matches return the index of the element, else
return -1 index. (-1 shows that the key is not in the list).
12
Simple Searching Algorithms
• Binary Search: Algorithm
Binary_Search(a, lower_bound, upper_bound, val)
Step 1: set beg = lower_bound, end = upper_bound, pos = - 1
Step 2: repeat steps 3 and 4 while beg <=end
Step 3: set mid = (beg + end)/2
Step 4: if a[mid] = val
set pos = mid
• 'a' is the given array,
print pos • 'lower_bound' is the index of the first ar
go to step 6 ray element,
else if a[mid] > val • 'upper_bound' is the index of the last ar
set end = mid - 1 ray element,
else • 'val' is the value to search
set beg = mid + 1
[end of if]
[end of loop]
Step 5: if pos = -1
print "value is not present in the array"
[end of if]
Step 6: exit
13
Simple Searching Algorithms
• Binary Search: Example 1
• Let the elements of array are
• Let the element to search is, K = 56
• We have to use the below formula to calculate the mid of
the array :- mid = (beg + end)/2
• mid = (0 + 8)/2 = 4. So, 4 is the mid of the array.
14
Simple Searching Algorithms
• Binary Search: Example 1
• Now, the element to search is found
• So algorithm will return the index of the element matched.
15
Simple Searching Algorithms
• Binary Search: Example 2
16
Simple Searching Algorithms
• Binary Searching : Implementation
int binarySearch(int a[], int beg, int end, int val) {
int mid;
if(end >= beg) {
mid = (beg + end)/2;
/* if the item to be searched is present at middle */
if(a[mid] == val) {
return mid;
}
/* if the item to be searched is smaller than middle, then it can only be in left subarray */
else if(a[mid] < val) {
return binarySearch(a, mid+1, end, val);
}
/* if the item to be searched is greater than middle, then it can only be in right subarray */
else {
return binarySearch(a, beg, mid-1, val);
}
}
return -1;
}
17
Simple Searching Algorithms
• Binary Searching : Implementation
int main()
{
int a[] = {10, 12, 24, 29, 39, 40, 51, 56, 70}; // given array
int val = 51; // value to be searched
int n = sizeof(a) / sizeof(a[0]); // size of array
int res = binarySearch(a, 0, n-1, val); // Store result
cout<<"The elements of the array are - ";
for (int i = 0; i < n; i++)
cout<<a[i]<<" ";
cout<<"\nElement to be searched is - "<<val;
if (res == -1)
cout<<"\nElement is not present in the array";
else
cout<<"\nElement is present at "<<res<<" position of array";
return 0;
}
18
Simple Searching Algorithms
• Binary Searching : Complexity Analysis
• the best case occurs when the element to search is found in first
comparison,
• i.e., when the first middle element itself is the element to be searched.
• the worst case occurs, when we must keep reducing the search space till
it has only one element.
Case Time Complexity
Best Case O(1)
Average Case O(logn)
Worst Case O(logn)
• Therefore, the time complexity of binary search algorithm is O(logn).
19
PART II
Sorting Algorithms
20
Simple Sorting Algorithms
• Sorting Algorithms:
• It is a process of reordering a list of items in either
increasing or decreasing order.
• The following are simple sorting algorithms used to sort
small-sized lists.
• Insertion Sort
• Selection Sort
• Bubble Sort
21
Simple Sorting Algorithms
• Sorting Algorithms: Insertion Sort
• it inserts each item into its proper place in the final list.
• The simplest implementation of this requires two list
structures:
• the source list and the list into which sorted items are
inserted.
• Basic Idea:
• Find the location for an element and move all others up,
and insert the element.
22
Simple Sorting Algorithms
• Sorting Algorithms: Insertion Sort
• The process involved in insertion sort is as follows:
1. The left most value can be said to be sorted relative to itself. Thus,
we don’t need to do anything.
2. Check to see if the second value is smaller than the first one. If it is,
swap these two values. The first two values are now relatively sorted.
3. Next, we need to insert the third value in to the relatively sorted
portion so that after insertion, the portion will still be relatively sorted.
4. Remove the third value first. Slide the second value to make room
for insertion. Insert the value in the appropriate position.
5. Now the first three are relatively sorted.
6. Do the same for the remaining items in the list.
23
Simple Sorting Algorithms
• Sorting Algorithms: Insertion Sort Implementation
Void insertion_sort(int list[]){
int temp;
for(int i=1;i<n;i++){
temp=list[i];
for(int j=i; j>0 && temp<list[j-1];j--)
{ // work backwards through the array finding where temp should go
list[j]=list[j-1];
list[j-1]=temp;
}//end of inner loop
}//end of outer loop
}//end of insertion_sort
24
Simple Sorting Algorithms
• Sorting Algorithms: Insertion Sort Example 1
25
Simple Sorting Algorithms
• Sorting Algorithms: Insertion Sort Example 2
26
Simple Sorting Algorithms
• Sorting Algorithms: Selection Sort
• Basic Idea:
• Loop through the array from i=0 to n-1.
• Select the smallest element in the array from i to n
• Swap this value with value at position i(beginning).
27
Simple Sorting Algorithms
• Sorting Algorithms: Selection Sort Implementation
void selection_sort(int list[]){
int i,j, smallest;
for(i=0;i<n;i++){
smallest=i;
for(j=i+1;j<n;j++){
if(list[j]<list[smallest])
smallest=j;
}//end of inner loop
temp=list[smallest];
list[smallest]=list[i];
list[i]=temp;
} //end of outer loop
}//end of selection_sort
28
Simple Sorting Algorithms
• Sorting Algorithms: Selection Sort Example 1
29
Simple Sorting Algorithms
• Sorting Algorithms: Selection Sort Example 2
30
Simple Sorting Algorithms
• Sorting Algorithms: Bubble Sort
• is the simplest algorithm to implement and the slowest
algorithm on very large inputs.
• Basic Idea:
• Loop through array from i=0 to n and swap adjacent
elements if they are out of order.
31
Simple Sorting Algorithms
• Sorting Algorithms: Bubble Sort Implementation
void bubble_sort(list[]){
int i,j,temp;
for(i=0;i<n; i++){
for(j=n-1;j>i; j--){
if(list[j]<list[j-1]){
temp=list[j];
list[j]=list[j-1];
list[j-1]=temp;
}//swap adjacent elements
}//end of inner loop
}//end of outer loop
}//end of bubble_sort
32
Simple Sorting Algorithms
• Sorting Algorithms: Bubble Sort Example 1
33
Simple Sorting Algorithms
• Sorting Algorithms: Bubble Sort Example 2
34
?
END OF CHAPTER THREE
Next: Chapter Four: Data Structures