0% found this document useful (0 votes)
498 views21 pages

Aim: Write A Program To Implement Insertion Sort. Theory: Insertion Sort Is The Simple Sorting Algorithm That Is

The document contains the theory and code implementations for 7 sorting algorithms: insertion sort, linear search, binary search, selection sort, bubble sort, quicksort, and merge sort. For each algorithm, it provides 1-2 paragraphs explaining the theory/approach and includes the full code implementation in C programming language.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
498 views21 pages

Aim: Write A Program To Implement Insertion Sort. Theory: Insertion Sort Is The Simple Sorting Algorithm That Is

The document contains the theory and code implementations for 7 sorting algorithms: insertion sort, linear search, binary search, selection sort, bubble sort, quicksort, and merge sort. For each algorithm, it provides 1-2 paragraphs explaining the theory/approach and includes the full code implementation in C programming language.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 21

Aim: Write a program to implement Insertion Sort.

Theory: Insertion sort is the simple sorting algorithm that is


commonly used in daily lives while ordering a deck of cards. In
this algorithm, we insert each element onto its proper place in
the sorted array. This is less efficient than the other sort
algorithms like quicksort, merge sort, etc.
#include<stdio.h>
int main()
{
int n,i,a[50],temp,j;
printf("How many elements: ");
scanf("%d",&n);
printf("Enter the elements of array: ");
for(i=0;i<n;i++)
{
scanf("%d",&a[i]);
}
for(i=0;i<n;i++)
{
for(j=i+1;j<n;j++)
{
if(a[i]>a[j])
{
temp=a[i];
a[i]=a[j];
a[j]=temp;
}
}
}
printf("Element of array after sorting: ");
for(i=0;i<n;i++)
{
printf("%d ",a[i]);
}
printf("\n");
return 0;
}
Aim: Write a program to implement Linear Search

Theory: In computer science, a linear search or sequential


search is a method for finding an element within a list. It
sequentially checks each element of the list until a match is
found or the whole list has been searched.
#include<stdio.h>
int main()
{
int a[5],n,flag,i,j;
printf("Enter the elements for array: ");
for(i=0;i<5;i++)
{
scanf("%d",&a[i]);
}
printf("Enter the key: ");
scanf("%d",&n);
for(j=0;j<5;j++)
{
if(a[j]==n)
{
flag=1;
break;
}
}
if(flag==1)
{
printf("Search is successful\n");
printf("loc = %d\n",j+1);
}
else
{
printf("Search is unsuccessful\n");
}
flag=0;
return 0;

}
Aim: Write a program to implement Binary Search

Theory: In computer science, binary search, also known as


half-interval search, logarithmic search, or binary chop, is a
search algorithm that finds the position of a target value within
a sorted array. Binary search compares the target value to the
middle element of the array
#include<stdio.h>
int main()
{
int n,i,j,a[100],search,first,last,middle;
printf("Enter the total number of elements: ");
scanf("%d",&n);
printf("Enter the element in sorted form: ");
for(i=0;i<n;i++)
{
scanf("%d",&a[i]);
}
printf("Enter the element which you want to search: ");
scanf("%d",&search);
first=0;
last=n-1;
middle=(first+last)/2;

while(first<=last)
{
if(a[middle]<search)
{
first=middle+1;
}
else if(a[middle]==search)
{
printf("The number %d is found in the list at the
position %d\n",search,middle+1);
break;
}
else
{
last=middle-1;
}
middle=(first+last)/2;

}
if(first>last)
{
printf("Element not found\n");
}
return 0;
}
Aim: Write a program to implement Selection Sort

Theory: In selection sort, the smallest value among the


unsorted elements of the array is selected in every pass and
inserted to its appropriate position into the array.
First, find the smallest element of the array and place it in the
first position. Then, find the second smallest element of the
array and place it in the second position. The process
continues until we get the sorted array.
#include<stdio.h>
int main()
{
int a[20],temp,n,i,j,position;
printf("How many elements: ");
scanf("%d",&n);
printf("Enter the element of array: ");
for(i=0;i<n;i++)
{
scanf("%d",&a[i]);
}
for(i=0;i<n-1;i++)
{
position=i;
for(j=i+1;j<n;j++)

{
if(a[position]>a[j])
{
position=j;
}
if(a[i]>a[j])
{
temp=a[i];
a[i]=a[j];
a[j]=temp;
}
}
}
printf("The number after selection sorting are: ");
for(i=0;i<n;i++)
{
printf("%d ",a[i]);
}
printf("\n");

return 0;
}
Aim: Write a program to implement Bubble sort

Theory: In Bubble sort, Each element of the array is compared


with its adjacent element. The algorithm processes the list in
passes. A list with n elements requires n-1 passes for sorting.
#include<stdio.h>
int main()
{
int num,i,j,a[50],temp;
printf("How many elements: ");
scanf("%d",&num);
printf("Enter the elements of array: \n");
for(i=0;i<num;i++)
{
scanf("%d",&a[i]);
}
for(i=0;i<num-1;i++)
{
for(j=0;j<num-i-1;j++)
{
if(a[j]>a[j+1])
{
temp=a[j];
a[j]=a[j+1];
a[j+1]=temp;
}
}
}
printf("Element of array after the sorting are: ");
for(i=0;i<num;i++)
{
printf("%d ",a[i]);
}
printf("\n");
}
Aim: Write a program to implement Quick sort

Theory: Quicksort is a sorting algorithm based on the divide


and conquer approach where An array is divided into subarrays
by selecting a pivot element (element selected from the array).
While dividing the array, the pivot element should be positioned
in such a way that elements less than the pivot are kept on the
left side, and elements greater than the pivot are on the right
side of the pivot.
The left and right subarrays are also divided using the same
approach. This process continues until each subarray contains
a single element.
At this point, elements are already sorted. Finally, elements are
combined to form a sorted array.
#include<stdio.h>
void quick_sort(int[],int,int);
int partition(int[],int,int);
int main()
{
int a[50],n,i;
printf("Enter the no of elements: ");
scanf("%d",&n);
printf("Enter numbers to be sorted: ");
for(i=0;i<n;i++)
scanf("%d",&a[i]);
quick_sort(a,0,n-1);
printf("Sorted array is: ");
for(i=0;i<n;i++)
printf("%d ",a[i]);
printf("\n");
return 0;
}
void quick_sort(int a[],int l,int u)
{
int j;
if(l<u)
{
j=partition(a,l,u);
quick_sort(a,l,j-1);
quick_sort(a,j+1,u);
}
}
int partition(int a[],int l,int u)
{
int v,i,j,temp;
v=a[l];
i=l;
j=u+1;
do
{
do
i++;
while(a[i]<v&&i<=u);
do
j--;
while(v<a[j]);
if(i<j)
{
temp=a[i];
a[i]=a[j];
a[j]=temp;
}
}while(i<j);
a[l]=a[j];
a[j]=v;
return(j);
}
Aim: Write a program to implement Merge sort

Theory: Merge Sort is one of the most popular sorting


algorithms that is based on the principle of the Divide and
Conquer Algorithm. Here, a problem is divided into multiple
sub-problems. Each sub-problem is solved individually. Finally,
sub-problems are combined to form the final solution.
#include<stdio.h>
#include<stdlib.h>
void merge(int arr[],int l,int m,int r)
{
int i,j,k;
int n1=m-l+1;
int n2=r-m;
int L[n1],R[n2];
for(i=0;i<n1;i++)
{
L[i]=arr[l+i];
}
for(j=0;j<n2;j++)
{
R[j]=arr[m+j+1];
}
i=0,j=0,k=l;
while(i<n1&&j<n2)
{
if(L[i]<=R[j])
{
arr[k]=L[i];
i++;
}
else
{
arr[k]=R[j];
j++;
}
k++;
}
while(i<n1)
{
arr[k]=L[i];
i++;
k++;
}
while(j<n2)
{
arr[k]=R[j];
j++;
k++;
}
}
void MergeSort(int arr[],int l,int r)
{
if(l<r)
{
int mid=l+(r-l)/2;
MergeSort(arr,l,mid);
MergeSort(arr,mid+1,r);
merge(arr,l,mid,r);
}
}
int main()
{
int n;
printf("Enter number of elements in the array: ");
scanf("%d",&n);
int arr[n];
printf("Enter elements in the array: ");
for(int i=0;i<n;i++)
{
scanf("%d",&arr[i]);
}
MergeSort(arr,0,n-1);
printf("Sorted Array is: ");
for(int i=0;i<n;i++)
{
printf("%d ",arr[i]);
}
printf("\n");
return 0;
}

Output2.
Output1

Output3
Output4

Output5
Output 6

Output7

You might also like