Chapter - 3 - Searching and Sorting Algorithms
Chapter - 3 - Searching and Sorting Algorithms
1
Content
This Chapter Covers:
Sequential search
Binary search
Bubble sort
Selection sort
Insertion sort
2
Why do we study sorting and searching algorithms?
they are the most common & useful tasks in any software development
they take more than 25% of running time of computers task
Example:
Searching documents over the internet
Searching files and folders in a hard disk
Sorting students by their name, year and so on
Sorting file, search results by file name, date created and so on
Deleting and recording data from a database
3
Simple searching Algorithm
The act of searching for a piece of information in a list is one of the fundamental
algorithms in computer science.
Searching: is a process of locating a particular element present in a given set of
elements.
5
Sequential Searching
7
Binary Searching
Algorithm:
1. Divide the list into halves
2. see which half of the list the key belongs
3. repeat 1 and 2 until only one element remains
4. if the remaining element is equal to search key then found =true else key not
found.
1st assign first and last
2nd calculate middle= first + last/2
A. if key>middle first=middle+1, last=last
B. . if key<middle first=first, last=middle-1
C. . if key==middle print position or index of Key
8
Binary Searching
9
Binary Searching
Implementation:
int i, arr[10], num, first=0, last=9, middle;
middle = (first+last)/2;
while(first <= last){
if(arr[middle]<num)
first = middle+1;
else if(arr[middle]==num){
cout<<"\nThe number, "<<num<<" found at Position "<<middle+1;
break;}
else
last = middle-1;
middle = (first+last)/2;}
if(first>last)
cout<<"\nThe number, "<<num<<" is not found in given Array";
10
Sorting Algorithms
For searching, sorting is important
Sorting refers to arranging data in a particular format. Sorting algorithm specifies the
way to arrange data in a particular order.
Most common orders are in numerical order.
The importance of sorting lies in the fact that data searching can be optimized to a very
high level, if data is stored in a sorted manner.
Sorting is also used to represent data in more readable formats
11
Bubble Sort Algorithm
It a method which uses the interchanging of adjacent pair of element in the
array.
After each pass through the data one element (the largest or smallest element)
will be moved down to the end of the array in its proper place.
• AIM: Write a program to sort values in ascending order using bubble sort technique in
linear array.
• Description: ‘data’ is linear array and N is the number of values stored in data
• Basic Idea:
Loop through array from i = 0 to N and swap adjacent elements if they are out of order
12
Example data element= 7,2,9,6,4
Algorithms:
13
Selection Sort Algorithm
The selection sort algorithm sorts an array by repeatedly finding the
minimum element (considering ascending order) from unsorted part and
putting it at the beginning
It is in many ways like both simple and bubble algorithm.
Rather than swapping the neighbors continuously as the algorithm traverses
the sub array to be sorted, as done in the bubble sort case, this algorithm
finds the minimum element of the sub array and swaps it with the pivot
elements.
The algorithm maintains two subarrays in a given array.
The subarray which is already sorted.
Remaining subarray which is unsorted.
14
Selection Sort Algorithm
The smallest element is selected from unsorted element and exchange with element in the beginning unsorted
element
i=0
Loop through the array from i = 0 to N – 1. J=1
Min=i
Select the smallest element in the array from i = 1 to N - 1. 7 4 10 8 3 1
Swap this value with value at position i.
Pass 1 1 4 10 8 3 7
Example
Algorithms:
void SelectionSort(int data[]) Pass 2 1 3 10 8 4 7
int smallest
loop through i =0 to N -1
Pass 3 1 3 4 8 10 7
smallest 0
loop through j i+1 to N
Pass 4 1 3 4 7 10 8
if data[j] < data[smallest]
smallest j
swap data[i] and data[smallest]); Pass 1 1 3 4 7 8 10
Example
Implementation:
int smallest;
for(int i=0;i<6;i++){
min=i;
for(int j=i+1;j<6;j++){
if(A[j]<A[min])
min=j;}
int temp=A[min];
A[min]=A[i];
A[i]=temp;
cout<<A[i]<<endl;}}
16
Insertion Sort Algorithm
As each element in the list is examined it is put into its proper place
among the elements that have been examined ( and put in correct
order).
When the last element is put into its proper position the list is sorted
and the algorithm is done.
It behaves in the worst case like the inefficient bubble sort and selection
sort algorithms.
But in many average case it performs better.
17
18
Implementation
• Implementations:
Int a[],
Int n,temp;
For(i=1;i<n;i++){
Temp=a[i]
j=i-1;
While (j>=0&&a[j]>temp)
{a[j+1]=a[j];
j--}
A[j+1]=temp;
19
Assignment - III
Write a C++ program that will implement all of the sorting algorithms. The
program should accept different unsorted data items from user and sort
using all algorithms and should tell which algorithm is efficient for that
unsorted data items. (Hint use how many comparison and swaps are done so
the one with the smaller number of swap and comparison number is
considered as an efficient algorithm for that data items.)
20
…
???
End!!!!!!!!
21