Remaining Programs

Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 2

Program no.

35
Aim- WAP to sort an array using bubble sort
#include<stdio.h>
int main()
{
int arr[100],a,i,j,temp;
printf("Enter the range of array : ");
scanf("%d",&a);
printf("Enter the value of array : ");
for(int i=0;i<a;i++)
{
scanf("%d",&arr[i]);
}
for(i=0;i<a;i++)
{
for(j=i+1;j<a;j++)
{
if(arr[i]>arr[j])
{
temp=arr[i];
arr[i]=arr[j];
arr[j]=temp;
}
}
}
for(i=0; i<a; i++)
{
printf("%d\t",arr[i]);
}
return 0;
}

Output :
Program no. 36
Aim- WAP to sort an array using insertion sort
#include <stdio.h>
int main()
{
int a[100], range, i, j, temp;
printf("\nEnter the Range of the array : ");
scanf("%d", &range);

printf("\n Please Enter the Array Elements : ");


for(i = 0; i < range; i++)
scanf("%d", &a[i]);

for(i = 1; i <= range - 1; i++)


{
for(j = i; j > 0 && a[j - 1] > a[j]; j--)
{
temp = a[j];
a[j] = a[j - 1];
a[j - 1] = temp;
}
}
printf("\n Insertion Sort Result : ");
for(i = 0; i < range; i++)
{
printf(" %d \t", a[i]);
}
printf("\n");
return 0;
}

Output :

You might also like