1.
Bubble sort without recursion
#include<stdio.h>
#include<conio.h>
void main()
{
int a[20],i,n,j,k,temp,count=0;
clrscr();
printf("Enter number of elements: ");
scanf("%d",&n);
printf("Enter elements:\n");
for (i=0; i<n; i++)
scanf("%d",&a[i]);
printf("\nElements before sorting are: ");
for (i=0; i<n; i++)
printf("%d ",a[i]);
printf("\n\nBubble Sort initiated...");
for(i=0;i<n-1;i++)
{
for (j=0; j<n-1; j++)
{
if (a[j] > a[j+1])
{
temp=a[j];
a[j]=a[j+1];
a[j+1]=temp;
}
}
printf("\nAfter pass %d: ",count++);
for (k=0; k<n; k++)
printf("%d ",a[k]);
}
printf("\n\nElements after sorting are: ");
for (i=0; i<n; i++)
printf("%d ",a[i]);
}
2. Bubblesort using recursion
#include<stdio.h>
#include<conio.h>
int bubblesort_recur(int a[],int n,int count);
void main()
{
int a[20],i,n;
clrscr();
printf("Enter number of elements: ");
scanf("%d",&n);
printf("Enter elements:\n");
for (i=0; i<n; i++)
scanf("%d",&a[i]);
printf("\nElements before sorting are: ");
for (i=0; i<n; i++)
printf("%d ",a[i]);
printf("\n\nBubble Sort initiated...");
bubblesort_recur(a,n,1);
printf("\n\nElements after sorting are: ");
for (i=0; i<n; i++)
printf("%d ",a[i]);
int bubblesort_recur(int a[],int n,int count)
{
int i,temp;
if (count == n)
return -1;
for (i=0; i<n-1; i++)
{
if (a[i] > a[i+1])
{
temp=a[i];
a[i]=a[i+1];
a[i+1]=temp;
}
}
printf("\nAfter pass %d: ",count);
for (i=0; i<n; i++)
printf("%d ",a[i]);
return bubblesort_recur(a,n,count+1);
}
3. Sorting elements using selection sort
// C program for implementation of selection sort
#include <stdio.h>
#include<conio.h>
void selection_sort(int a[],int n);
void main()
{
int a[20],i,n;
clrscr();
printf("Enter number of elements:");
scanf("%d",&n);
printf("Enter elements:\n");
for (i = 0; i < n; i++)
scanf("%d",&a[i]);
printf("Elements before sorting: ");
for (i = 0; i < n; i++)
printf("%d ",a[i]);
selection_sort(a,n);
}
void selection_sort(int a[], int n)
{
int i, j, min, temp, p;
// One by one move boundary of unsorted subarray
for (i = 0; i < n - 1; i++)
{
// Find the minimum element in unsorted array
min = i;
for (j = i + 1; j < n; j++)
if (a[j] < a[min])
min = j;
// Swap the element
temp=a[i];
a[i]=a[min];
a[min]=temp;
printf("\nAfter Pass %d: ",i+1);
for (p = 0; p < n; p++)
printf("%d ",a[p]);
}
printf("\nElements after Sorting: ");
for (p = 0; p < n; p++)
printf("%d ",a[p]);
}
4. Sorting elements using insertion sort
// C program for insertion sort
#include <stdio.h>
#include<conio.h>
void insertion_sort(int a[],int n);
void main()
{
int a[20],i,n;
clrscr();
printf("Enter number of elements:");
scanf("%d",&n);
printf("Enter elements:\n");
for (i = 0; i < n; i++)
scanf("%d",&a[i]);
printf("\nElements before sorting are:");
for (i = 0; i < n; i++)
printf("%d ", a[i]);
insertion_sort(a,n);
}
/* Function to sort an array using insertion sort*/
void insertion_sort(int a[], int n)
{
int i,key,j,p;
for (i = 1; i < n; i++)
{
key = a[i];
j = i - 1;
/* Move elements of arr[0..i-1], that are greater than key,
to one position ahead of their current position */
while (j >= 0 && a[j] > key)
{
a[j + 1] = a[j];
j = j - 1;
}
a[j + 1] = key;
printf("\nAfter Pass %d: ",i);
for (p = 0; p < n; p++)
printf("%d ", a[p]);
}
printf("\nElements after sorting are: ");
for (p = 0; p < n; p++)
printf("%d ", a[p]);
}
5. Sorting elements using Mergesort
#include<stdio.h>
#include<conio.h>
void mergesort(int a[20],int low,int high);
void merge(int a[20],int low,int mid,int high);
void main()
{
int a[20],i,n,low,high;
clrscr();
printf("Enter the size of the array: ");
scanf("%d",&n);
printf("Enter the elements:\n");
for(i=0;i<n;i++)
{
scanf("%d",&a[i]);
}
low=0;
high=n-1;
printf("\nMerge sort initiated...");
mergesort(a,low,high);
printf("\nThe sorted elements are:\n");
for(i=0;i<n;i++)
{
printf("%d ",a[i]);
}
}
void mergesort(int a[20],int low,int high)
{
int mid;
mid=(low+high)/2;
if(low<high)
{
mergesort(a,low,mid);
mergesort(a,mid+1,high);
merge(a,low,mid,high);
}
void merge(int a[20],int low,int mid,int high)
{
int h,j,k,s,temp[20];
h=low;
j=mid+1;
k=low;
s=low;
while((h<=mid) && (j<=high))
{
if(a[h]<a[j])
{
temp[k]=a[h];
k++;
h++;
}
else
{
temp[k]=a[j];
k++;
j++;
}
if(h>mid)
{
while(j<=high)
{
temp[k]=a[j];
k++;
j++;
}
else
{
while(h<=mid)
{
temp[k]=a[h];
k++;
h++;
for(s=low;s<=high;s++)
{
a[s]=temp[s];
}
}
6. Sorting elements using Quicksort
#include<stdio.h>
#include<conio.h>
void quicksort(int a[20],int lb,int ub);
void main()
{
int a[20],i,n,lb,ub;
clrscr();
printf("Enter the size of the array: ");
scanf("%d",&n);
printf("Enter the elements:\n");
for(i=0;i<n;i++)
{
scanf("%d",&a[i]);
}
lb=0;
ub=n-1;
printf("\nQuick sort initiated...");
quicksort(a,lb,ub);
printf("\nThe sorted elements are:\n");
for(i=0;i<n;i++)
{
printf("%d ",a[i]);
}
}
void quicksort(int a[20],int lb,int ub)
{
int up,down,temp,pivot;
pivot=a[lb];
down=lb+1;
up=ub;
if(down<=up)
{
while(down<=up)
{
while(a[down]<pivot)
down++;
while(a[up]>pivot)
up--;
if(down<up)
{
temp=a[down];
a[down]=a[up];
a[up]=temp;
}
}
temp=a[lb];
a[lb]=a[up];
a[up]=temp;
quicksort(a,lb,up);
quicksort(a,down,ub);
}
}