UNIT5 Part1 Sorting&Searching
UNIT5 Part1 Sorting&Searching
Introduction to Algorithms:
What is an Algorithm?
Significance of Algorithms:
The performance analysis of an algorithm is the process of calculating space and time
complexity required by that algorithm.
What is search?
Search is a process of finding a value in a list of values. In other words, searching is the
process of locating given value position in a list of values.
Step 2: Compare the search element with the first element in the list.
Step 3: If both are matched, then display "Given element is found!!!" and terminate
the function.
Step 4: If both are not matched, then compare search element with the next element
in the list.
Step 5: If both are not matched, then compare search element with the next element
in the list.
Step 6: If last element in the list also doesn't match, then display "Element is not
found!!!" and terminate the function.
1. (a) C Program to implementation of Linear Search Algorithm(non-recursive):
#include<stdio.h>
#include<conio.h>
void main(){
int list[20],size,i,sElement;
printf("Enter size of the list: ");
scanf("%d",&size);
printf("Enter any %d integer values: ",size);
for(i = 0; i < size; i++)
scanf("%d",&list[i]);
printf("Enter the element to be Search: ");
scanf("%d",&sElement);
// Linear Search Logic
for(i = 0; i < size; i++)
{
if(sElement == list[i])
{
printf("Element is found at %d index", i);
break;
}
}
if(i == size)
printf("Given element is not found in the list!!!");
getch();
}
Output:
(b) C Program to implementation of Linear Search Algorithm (Recursive):
# include <stdio.h>
# include <conio.h>
void linear_search(int a[], int data, int position, int n)
{
if (position < n)
{
if(a[position] == data)
printf("\n Data Found at %d ", position);
else
linear_search(a, data, position + 1, n);
}
else
printf("\n Data not found");
}
void main()
{
int a[25], i, n, data;
//
printf("\n Enter the number of elements: ");
scanf("%d", &n);
printf("\n Enter the elements: ");
for(i = 0; i < n; i++)
{
scanf("%d", &a[i]);
}
printf("\n Enter the element to be seached: ");
scanf("%d", &data);
linear_search(a, data, 0, n);//
}
2. Binary Search Algorithm:
Binary Search is a searching algorithm used in a sorted array
by repeatedly dividing the search interval in half.
The idea of binary search is to use the information that the array is sorted
and reduce the time complexity to O (Log n).
The binary search cannot be used for a list of elements arranged in random
order.
This search process starts comparing the search element with the middle
element in the list.
If both are matched, then the result is "element found". Otherwise, we check
whether the search element is smaller or larger than the middle element in
the list.
If the search element is smaller, then we repeat the same process for the
left sub list of the middle element. If the search element is larger, then we
repeat the same process for the right sub list of the middle element.
We repeat this process until we find the search element in the list or until
we left with a sub list of only one element.
And if that element also doesn't match with the search element, then the
result is "Element not found in the list".
#include<stdio.h>
void main()
{
int first, last, middle, size, i, sElement, list[100];
printf("Enter the size of the list: ");
scanf("%d",&size);
printf("Enter %d integer values in Assending order\n", size);
for (i = 0; i < size; i++)
scanf("%d",&list[i]);
printf("Enter value to be search: ");
scanf("%d", &sElement);
first = 0;
last = size - 1;
middle = (first+last)/2;
while (first <= last)
{
if (list[middle] < sElement)
first = middle + 1;
else if (list[middle] == sElement) {
printf("Element found at index %d.\n",middle);
break;
}
else
last = middle - 1;
Output:
(b) C Program to implementation of Binary Search Algorithm (Recursive):
# include <stdio.h>
void bin_search(int a[], int data, int low, int high)
{
int mid ;
if( low <= high)
{
mid = (low + high)/2;
if(a[mid] == data)
printf("\n Element found at location: %d ", mid + 1);
else
{
if(data < a[mid])
bin_search(a, data, low, mid-1);
else
bin_search(a, data, mid+1, high);
}
}
else
printf("\n Element not found");
}
void main()
{
int a[25], i, n, data;
printf("\n Enter the number of elements: ");
scanf("%d", &n);
printf("\n Enter the elements in ascending order: ");
for(i = 0; i < n; i++)
scanf("%d", &a[i]);
printf("\n Enter the element to be searched: ");
scanf("%d", &data);
bin_search(a, data, 0, n-1);
}
Differences between Linear Search and Binary Search:
It is a way in which the elements are organized systematically for some purpose. For
example, a dictionary in which words is arranged in alphabetical order and telephone
director in which the subscriber names are listed in alphabetical order. There are
many sorting techniques out of which we study the following:
1. Selection sort
2. Bubble sort
3. Insertion sort
4. Quick sort
1. Selection Sort
Step 1: Select the first element of the list (i.e., Element at first position in the list).
Step 2: Compare the selected element with all the other elements in the list.
Step 3: In every comparison, if any element is found smaller than the selected
element (for Ascending order), then both are swapped.
Step 4: Repeat the same procedure with element in the next position in the list till
the entire list is sorted.
((n-1) + (n-2) + (n-3) +......+1) = (n (n-1))/2 number of comparisons in the worst case.
#include<stdio.h>
void main()
{
int size,i,j,temp,list[100];
printf("Enter the size of the List: ");
scanf("%d",&size);
printf("Enter %d integer values: ",size);
for(i=0; i<size; i++)
scanf("%d",&list[i]);
//Selection sort logic
for(i=0; i<size; i++){
for(j=i+1; j<size; j++)
{
if(list[i] > list[j])
{
temp=list[i];
list[i]=list[j];
list[j]=temp;
}
}
}
printf("List after sorting is: ");
for(i=0; i<size; i++)
printf(" %d",list[i]);
}
(b) Recursive program for Selection sort:
#include <stdio.h>
void selection(int [], int, int, int, int);
int main()
{
int list[30], size, temp, i, j;
printf("Enter the size of the list: ");
scanf("%d", &size);
printf("Enter the elements in list:\n");
for (i = 0; i < size; i++)
{
scanf("%d", &list[i]);
}
printf(" The list before sorting \n");
for (i = 0; i < size; i++)
{
printf("%d ", list[i]);
}
selection(list, 0, 0, size, 1);
printf("The sorted list in ascending order is\n");
for (i = 0; i < size; i++)
{
printf("%d ", list[i]);
}
return 0;
}
void selection(int list[], int i, int j, int size, int flag)
{
int temp;
if (i < size - 1)
{
if (flag)
{
j = i + 1;
}
if (j < size)
{
if (list[i] > list[j])
{
temp = list[i];
list[i] = list[j];
list[j] = temp;
}
selection(list, i, j + 1, size, 0);
}
selection(list, i + 1, 0, size, 1);
}
}
2. Bubble Sort:
Bubble Sort is the simplest sorting algorithm that works by repeatedly swapping
the adjacent elements if they are in the wrong order. This algorithm is not suitable
for large data sets as its average and worst-case time complexity is quite high.
#include <stdio.h>
int main()
{
int arr[50], num, i, j, temp;
printf("Please Enter the Number of Elements you want in the array: ");
scanf("%d", &num);
scanf("%d", &arr[i]);
arr[j + 1] = temp;
}
}
}
return 0;
}
(b) C program to implement Bubble sort using functions:
#include <stdio.h>
int x, y, temp;
int main()
{
int arr[50], n, x;
printf("Please Enter the Number of Elements you want in the array: ");
scanf("%d", &n);
scanf("%d", &arr[x]);
bubbleSortExample(arr, n);
return 0;
}
INSERTION SORT
Define insertion sort:
Insertion sort is a simple sorting algorithm that works like the way you sort playing
cards in your hands. In insertion sort algorithm, every iteration moves an element
from unsorted portion to sorted portion until all the elements are sorted in the list.
Step 1: Assume that first element in the list is in sorted portion and all the remaining
elements are in unsorted portion.
Step 2: Take first element from the unsorted portion and insert that element into the
sorted portion in the order specified.
Step 3: Repeat the above process until all the elements from the unsorted portion are
moved into the sorted portion.
#include<stdio.h>
void main()
{
int size, i, j, temp, list[100];
printf("Enter the size of the list: ");
scanf("%d", &size);
printf("Enter %d integer values: ", size);
for (i = 0; i < size; i++)
scanf("%d", &list[i]);
//Insertion sort logic
for (i = 1; i < size; i++)
{
temp = list[i];
j = i - 1;
while ((temp < list[j]) && (j >= 0))
{
list[j + 1] = list[j];
j = j - 1;
}
list[j + 1] = temp;
}
printf("List after Sorting is: ");
for (i = 0; i < size; i++)
printf(" %d", list[i]);
}
Differences between Selection sort, Bubble sort and Insertion sort
Selection Sort Bubble Sort Insertion sort
Selection Sort algorithm is used to Bubble Sort is the simplest sorting In insertion sort algorithm, every
arrange a list of elements in a algorithm that works by repeatedly iteration moves an element from
particular order. swapping the adjacent elements if unsorted portion to sorted portion
they are in the wrong order. until all the elements are sorted in the
list.
2
Time Complexity - Best case: O (N ) Time Complexity - Best case: O (N) Time Complexity - Best case: O (N)
Advantages: Advantages:
Advantages: