Bubble sort,Insertion sort,Selection sort programs

Q-1: write a program to implement sorting with BUBBLESORT METHOD.
ANS :

#include <stdio.h>
#include <conio.h>
Void main()
{
Int i,j,n,a[20],temp;
Clrscr();
Printf(“enter the number of elements:”);
Scanf(“%d”,&n);
Printf(“enter  the elements of array:”);
Scanf(“%d”,&a[i]);
For(i=0;i<n;i++)
{
Scanf(“%d”,&a[i]);
}
For(i=0;i<n-i;i++)
{
For(j=0;j<n-i;j++)
{
If(a[j]>a[j+1])
{
Temp=a[j];
a[j]=a[j+1];
a[j+1]=temp;
}
}
}
Printf (“the ascending order is:n”);
For (i=0;i<n;i++)
{
Printf(“%d n”,a[j]);
}
getch();
}

__________________________________________________________________________Q-2: write a program to implement sorting with INSERTIONSORT  METHOD.
ANS :

#include<stdio.h>
#include<conio.h>

void main()
{
int A[20], n, Temp, i, j;

clrscr();

printf("nn Enter number of elements..: ");

scanf("%d", &n);

printf("nt Enter the array.:");

for(i=0; i<n; i++)
{
scanf("n%d", &A[i]);
}
for(i=1; i<n; i++)
{
Temp = A[i];
j = i-1;

while(Temp<A[j] && j>=0)
{
A[j+1] = A[j];
j = j-1;
}
A[j+1] = Temp;
}
printf("ntTHE ASCENDING ORDER LIST IS...:n");

for(i=0; i<n; i++)
{
printf("nt%d", A[i]);
}
getch();
}

____________________________________________________________________________

Q-3: write a program to implement sorting with SELECTIONSORT  METHOD.
ANS :

#include <stdio>
#include<conio.h>
#define MAX 5

void SelSort(int X[],int start,int stop)
{
int begin=start;
int small=begin;
int tmp;

if(start<stop)
{
for(int i=begin+1;i<=stop;i++)
{
if(X[i] < X[small])
small=i;
}

tmp=X[begin];
X[begin]=X[small];
X[small]=tmp;
SelSort(X,start+1,stop);
}
}

int main()
{
int abc[MAX]={5,4,3,2,1};
printf("UNSORTED ARRAY:n");

for(int i=0;i<5;i++)
{
printf("%d  ",abc[i]);
SelSort(abc,0,MAX-1);
printf("nSORTED ARRAY:n");
}

for(int i=0;i<5;i++)
{
printf("%d  ",abc[i]);
}
return 0;
}

________

Leave a Reply