Unit-3 (Sorting Searching) Notes1
Unit-3 (Sorting Searching) Notes1
Syllabus:
Sorting Selection sort, Quick Sort, Insertion sort techniques (Using Arrays)
Searching - Linear search, Binary search techniques (Using Arrays)
Sorting is the process of arranging elements either in ascending (or) descending order.
For example:
If we need to search for, particular record in database, roll numbers in a list, a number in a
telephone directory, a specific page in a book etc.
If the data was kept in an unordered and unsorted form, it becomes difficult to search a
particular thing. But fortunately, the concept of sorting came into existence, making it easier
for everyone to arrange data in an order.
Sorting arranges data in a sequence which makes searching easier.
1. Internal sorting:
This method uses only the primary memory during sorting process.
All data items are held in main memory and no secondary memory is required in the sorting..
If all the data that is to be sorted can be accommodated at a time in memory is called Internal
Sorting.
Limitations:
• They can only process relatively small lists due to memory constraints.
3 types of internal sorts
1) Selection Sort
Ex:- selection sort , Heap sort
2) Insertion sort :
Ex: Insertion sort, Shell sort
3) Exchange Sort
Ex: Quick sort
External Sort:
Sorting large amount of data requires external or secondary memory. This process uses
external memory such as hard disk to store the data which does not fit in main memory so,
primary memory holds the current being sorted data only.
All the external sorts are based on process of merging.
Different parts of data are sorted separately and merged together
The criteria to judge which algorithm is better than the other is as follows −
Example:1
Example:2
As we can observe in the above array that it contains 6 elements. The above array is an unsorted array
whose indexing starts from 0 and ends at 5. The following are the steps used to sort the array:
Step 1: In the above array, the minimum element is 1; swap element 1 with an element 7.
Now, the sorted array contains only one element, i.e., 1, while the unsorted array contains 5
elements, i.e., 4, 10, 8, 3, 7.
Step 2: In the unsorted sub-array, the minimum element is 3, so swap element 3 with an element 4, which
is at the beginning of the unsorted sub-array.
Now the sorted array contains two elements, i.e., 1 and 3, while the unsorted array has four elements, i.e.,
10, 8, 4, 7, as shown in the above figure.
Step 3: Search the minimum element in the unsorted sub-array, and the minimum element is 4. Swap the
element 4 with an element 10, which is at the beginning of the unsorted sub- array.
Now, the sorted array contains three elements, i.e., 1, 3, 4, while the unsorted array has three elements,
i.e., 10, 8, 7
Step 4: Search the minimum element in the unsorted array, and the minimum element is 7. Swap element
7 with an element 10, which is at the beginning of the unsorted sub-array.
Now, the sorted array contains four elements, i.e., 1, 3, 4, 7, while the unsorted array has two elements,
i.e., 10, 8.
Step 5: Search the minimum element in the unsorted array and the minimum element is 8. Swap the
element 8 with an element 10 which is at the beginning of the unsorted sub-array.
Step 6: The last element is left in the unsorted sub-array. Move the last element to the sorted sub array
shown as below:
#include<stdio.h>
void read(int a[],int n);
void print(int a[],int n);
void selection_sort(int a[],int n);
int main( )
{
int a[20],n;
Output:
enter number of elements
5
enter 5 elements
3
2
1
5
6
Before Sorting
Array elements are
32156
After Sorting
Array elements are
12356
Insertion Sort
Insertion sort works similar to the sorting of playing cards in hands. It is assumed that the first card is already sorted
in the card game, and then we select an unsorted card. If the selected unsorted card is greater than the first card, it will
be placed at the right side; otherwise, it will be placed at the left side. Similarly, all unsorted cards are taken and put
in their exact place.
Insertion sort in c is the simple sorting algorithm that virtually splits the given array into sorted and unsorted parts, then the
values from the unsorted parts are picked and placed at the correct position in the sorted part.
Step 1 - If the element is the first element, assume that it is already sorted. Return 1.
Step2 - Pick the next element, and store it separately in a key.
Step3 - Now, compare the key with all elements in the sorted array.
Step 4 - If the element in the sorted array is smaller than the current element, then move to the next element. Else,
shift greater elements in the array towards the right.
Step 5 - Insert the value.
Step 6 - Repeat until the array is sorted.
Example:1
Example:2
Example:3
Here, 31 is greater than 12. That means both elements are already in ascending order. So, for now, 12 is stored in a sorted sub-
array.
Here, 25 is smaller than 31. So, 31 is not at correct position. Now, swap 31 with 25. Along with swapping, insertion sort will
also check it with all elements in the sorted array.
For now, the sorted array has only one element, i.e. 12. So, 25 is greater than 12. Hence, the sorted array remains sorted after
swapping.
Now, two elements in the sorted array are 12 and 25. Move forward to the next elements that are 31 and 8.
Both 31 and 8 are not sorted. So, swap them.
Now, the sorted array has three items that are 8, 12 and 25. Move to the next items that are 31 and 32.
Hence, they are already sorted. Now, the sorted array includes 8, 12, 25 and 31.
Output:
enter number of elements
5
enter 5 elements
4
3
2
1
7
Before Sorting
Array elements are
43217
After Sorting
Array elements are
12347
Difference between selection sort and Insertion sort
Selection sort sorts by identifying the smallest element Insertion sort sorts by interchanging one element at a
from the unsorted group and selecting it to interchange time from the sorted group with the partially sorted
it with the elements from the sorted group and puts in group.
the correct location.
Selection sorts first finds the smallest element from the Transfers one element at a time to the sorted group of
unsorted group and then transfers it accordingly. the elements.
Selection sort is less efficient than insertion sort. Insertion sort is more efficient than selection sort.
Selection sort is very much simpler as compared to Insertion sort is more complex than selection sort.
insertion sort as the process of finding smaller numbers
from a group of numbers is very easier.
Selection sorting is an unstable way of sorting elements Insertion sort is a very stable sorting algorithm if
of an array if compared to insertion sorting. compared to selection sort.
In selection sorting, no best case time complexity is O(N) is the best case time complexity in insertion
available. O (N2) is the only time complexity available sorting when the array has to be in ascending order.
for all cases.
While doing selection sorting, the number of swapping While doing sorting through insertion sorting, the
operations performed is less than the number of number of swapping operations performed is higher
comparisons done. than the comparison operation performed.
In selection sorting, immediate data cannot be dealt The major benefit of insertion sorting is that immediate
with as the data is needed to be present at the very data can also be dealt with by this way of sorting as this
beginning of the sorting process. technique is done live.
Quick Sort:
It is a faster and highly efficient sorting algorithm. This algorithm follows the divide and conquer approach. Divide and conquer
is a technique of breaking down the algorithms into subproblems, then solving the subproblems, and combining the results back
together to solve the original problem.
Divide: In Divide, first pick a pivot element. After that, partition or rearrange the array into two sub-arrays such that each
element in the left sub-array is less than or equal to the pivot element and each element in the right sub-array is larger than the
pivot element.
Conquer: Recursively, sort two subarrays with Quicksort.
Combine: Combine the already sorted array.
Quicksort picks an element as pivot, and then it partitions the given array around the picked pivot element. In quick sort, a large
array is divided into two arrays in which one holds values that are smaller than the specified value (Pivot), and another array
holds the values that are greater than the pivot.
After that, left and right sub-arrays are also partitioned using the same approach. It will continue until the single element remains
in the sub-array
Quick sort is based on partition. It is also known as partition exchange sorting. The basic concept of
quick sort process is pick one element from an array and rearranges the remaining elements around
it.This element divides the main list into two sub lists. This chosen element is called pivot. Once pivot
is chosen, then it shifts all the elements less than pivot to left of value pivot and all the elements greater
than pivot are shifted to the right side. This procedure of choosing pivot and partition the list is
applied recursively until sub-lists consisting of only one element.
#include<stdio.h>
void read(int a[],int n);
void print(int a[],int n);
void quicksort(int a[25],int first,int last);
return 0;
}
INPUT:
How many elements are u going to enter?: 5
Enter 5 elements: 5 4 3 2 1
OUTPUT:
elements of an array before sorting: 5 4 3 2 1
Order of Sorted elements: 1 2 3 4 5
Time complexity is the amount of time taken to run an algorithm. It is the measure of the number of elementary
operations performed by an algorithm and an estimate of the time required for that operation. It also depends on
external factors such as the compiler, the processor’s speed, etc.
Space complexity is the amount of memory space an algorithm/program uses during its entire execution. It measures
the number of variables created to store values, including both the inputs and the outputs .
1 Searching algorithms are designed to find and Sorting algorithms are used to organize a list of
retrieve data elements from a data structure. data or an array in a specific order.
2 Searching algorithms are generally divided into Sorting algorithms can be classified into two
two categories – Interval Search and Sequential types- Internal Sorting and External Sorting.
Search.
3 The Linear Search and the Binary Search are the The Bubble Sort, Insertion Sort, Selection Sort,
examples of Searching Algorithms. Merge Sort, Quick Sort etc are the examples of
Sorting Algorithms.
1. Sequential Search
Now, the element to be searched is found. So algorithm will return the index of the element matched.
Linear SearchAlgorithm
LinearSearch(array, key)
for each itemin the array
if item == value
return its index
Program for Linear Search(Sequential Search)
#include <stdio.h>
int main()
{
int num;
if (flag == 1)
printf("Element is present in the array at position %d",i+1);
else
printf("Element is not present in the array\n");
return 0;
}
OUTPUT:
Enter size of array:5
Enter the elements one by one
5
3
6
2
1
Enter the element to be searched 6
Element is present in the array at position 3
Linear Search Program Using Functions:
#include<stdio.h>
void read(int a[],int n)
{
int i;
printf("enter %d elements\n",n);
for(i=0;i<n;i++)
scanf("%d",&a[i]);
}
void print(int a[],int n)
{
int i;
printf("Array elements are\n");
for(i=0;i<n;i++)
printf("%d ",a[i]);
printf("\n");
}
int main(void)
{
int a[20],n,key,pos;
printf("enter number of elements\n");
scanf("%d",&n);
read(a,n);
printf("enter key\n");
scanf("%d",&key);
pos=linear_search(a,n,key);
if(pos!=-1)
printf("%d found at position %d\n",key,pos+1);
else
printf("%d not found\n",key);
return 0;
}
OUTPUT:
enter number of elements
5
enter 5 elements
2
3
6
3
7
enter key
3
3 found at position 1
Program:
Write a C program to search a string in a list of strings using linear search. If the string is found display the
position, otherwise print “string not present”.
#include<stdio.h>
#include<string.h>
int main()
{
char str[20][50], s1[50];
int n, i, found=0;
We have to use the below formula to calculate the mid of the array -mid = (beg + end)/2
So, in the given array -
beg = 0
end = 8
mid = (0 + 8)/2 = 4. So, 4 is the mid of the array.
Now, the element to search is found. So algorithm will return the index of the element matched.
Iteration Method
while low <= high: (do until the pointers low and high meet each other.
mid = (low + high)/2
if (x == arr[mid])
return mid
else if (x > arr[mid])
low = mid + 1
else(x < arr[mid])
high = mid - 1
Recursive Method
binarySearch(arr, x, low, high)
if low > high
return False
else
mid = (low + high) / 2
if x == arr[mid]
return mid
else if x > arr[mid]
return binarySearch(arr, x, mid + 1, high)
else
return binarySearch(arr, x, low, mid – 1)
Binary search in C using recursion
#include <stdio.h>
void binary_search(int [], int, int, int);
void bubble_sort(int [], int);
int main()
{
int key, n, i;
int list[25];
printf("Enter size of a list: ");
scanf("%d", &size);
printf("Enter elements\n");
for(i = 0; i < sn; i++)
{
scanf("%d",&list[i]);
}
bubble_sort(list, n);
printf("\n");
printf("Enter key to search\n");
scanf("%d", &key);
binary_search(list, 0, size, key);
}
void bubble_sort(int list[], int n)
{
int temp, i, j;
for (i = 0; i < n; i++)
{
for (j = i; j < n; j++)
{
if (list[i] > list[j])
{
temp = list[i];
list[i] = list[j];
list[j] = temp;
}
}
}
}
void binary_search(int list[], int lo, int hi, int key)
{
int mid;
if (lo > hi)
{
printf("Key not found\n");
return;
}
mid = (lo + hi) / 2;
if (list[mid] == key)
{
printf("Key found\n");
}
else if (list[mid] > key)
{
binary_search(list, lo, mid - 1, key);
}
else if (list[mid] < key)
{
binary_search(list, mid + 1, hi, key);
}
}
#include<stdio.h>
int main()
{
int arr[50],i,n,x,flag=0,first,last,mid;
for(i=0;i<n;++i)
scanf("%d",&arr[i]);
first=0;
last=n-1;
while(first<=last)
{
mid=(first+last)/2;
if(x==arr[mid])
{
flag=1;
break;
}
else if(x>arr[mid])
first=mid+1;
else
last=mid-1;
}
if(flag==1)
printf("\nElement found at position %d",mid+1);
else
printf("\nElement not found");
return 0;
}
OUTPUT:
Enter size of array:5
Enter array element(ascending order)
10
20
30
40
50
Enter the element to search:30
1. Time Complexity:
Definition The linear search starts searching from the It finds the position of the searched element
first element and compares each element with by finding the middle element of the array.
a searched element till the element is not
found.
Sorted data In a linear search, the elements don't need to The pre-condition for the binary search is
be arranged in sorted order. that the elements must be arranged in a
sorted order.
Implementation The linear search can be implemented on any The implementation of binary search is
linear data structure such as an array, linked limited as it can be implemented only on
list, etc. those data structures that have two-way
traversal.
Approach It is based on the sequential approach. It is based on the divide and conquer
approach.
Size It is preferrable for the small-sized data sets. It is preferrable for the large-size data sets.
Efficiency It is less efficient in the case of large-size data It is more efficient in the case of large-size
sets. data sets.
Worst-case In a linear search, the worst- case scenario for In a binary search, the worst-case scenario
scenario finding the element is O(n). for finding the element is O(log2n).
Best-case In a linear search, the best-case scenario for In a binary search, the best-case scenario for
scenario finding the first element in the list is O(1). finding the first element in the list is O(1).
50 30 10 90 80 20 40 70
i j
pivot
50 30 10 90 80 20 40 70
i j
pivot
50 30 10 90 80 20 40 70
i j
pivot
50 30 10 90 80 20 40 70
i j
pivot
50 30 10 90 80 20 40 70
i j
pivot
50 30 10 90 80 20 40 70
i j
50 30 10 90 80 20 40 70
i j
pivot
50 30 10 40 80 20 90 70
i j
pivot
50 30 10 40 80 20 90 70
i j
pivot
50 30 10 40 80 20 90 70
i j
90=>50 True j - -
pivot
50 30 10 40 80 20 90 70
i j
pivot
50 30 10 40 80 20 90 70
i j
50 30 10 40 20 80 90 70
i j
pivot
50 30 10 40 20 80 90 70
i j
pivot
50 30 10 40 20 80 90 70
i j
80 => 50 True j—
pivot
50 30 10 40 20 80 90 70
j i
pivot
50 30 10 40 20 80 90 70
j i
pivot
50 30 10 40 20 80 90 70
j i
20 30 10 40 50 80 90 70
j i
Now, the element on the right side and left side are greater than and smaller
than 50 respectively.
Sublist1 Sublist1
20 30 10 40 80 90 70
pivot
20 30 10 40
i j
pivot
20 30 10 40
i j
pivot
20 30 10 40
i j
40=>20 True j- -
pivot
20 30 10 40
i j
pivot
20 10 30 40
i j
pivot
20 10 30 40
i j
30<=20 false
pivot
20 10 30 40
i j
pivot
20 10 30 40
j i
pivot
20 10 30 40
j i
pivot
20 10 30 40
j i
pivot
10 20 30 40
j i
Sublist2
pivot
80 90 70
i j
pivot
80 90 70
i j
pivot
80 90 70
i j
pivot
80 90 70
i j
pivot
80 70 90
i j
80 70 90
i j
80 70 90
i j
90=>80 true j- -
80 70 90
j i
70=>80 false stop
80 70 90
j i
80 70 90
j i
70 80 90