You are on page 1of 5

Name: Ruturaj Uttarwar Class: TY ET-D

GR No: 12020036 Roll No: 60

DAAOA Lab 1

Implementation Insertion Sort and Bubble Sort

1. Write a program of Insertion Sort

Code:
//Name - Ruturaj nitin uttarwar
//Roll no. ET-D3_60
//Title:Insertion Sort of integers
//Description: the below c program is built to sort n elements in
array in ascending form
//input: n elements of array in unsorted form
//output: sorted array

#include<stdio.h>

int main()
{
int a[100],n,i,j,t,temp;
printf("Number of elements\n");
scanf("%d",&n);
printf("Enter %d integer\n",n);
for(i=0;i<n;i++)
{
scanf("%d",&a[i]);

}
for(i=1;i<n;i++)
{
t=i;
j=t-1;
Name: Ruturaj Uttarwar Class: TY ET-D
GR No: 12020036 Roll No: 60

while(j>=0 && a[t]<a[j])


{
temp=a[j];
a[j]=a[t];
a[t]=temp;
j--;
t--;
}
}

printf("Sorted list:\n");
for(i=0;i<n;i++)
{
printf("%d\n",a[i]);
}

Output:
Name: Ruturaj Uttarwar Class: TY ET-D
GR No: 12020036 Roll No: 60

2. Write a program of Bubble Sort

Code:
//Name - Ruturaj nitin uttarwar
//Roll no. ET-D3_60
//Title:Bubble Sort of integers
//Description: the below c program is built to sort n elements in
array in ascending form
//input: n elements of array in unsorted form
//output: sorted array

int main()
{
int n, c, d, swap; //Introducing the required variable

printf("Enter number of elements\n"); //Asking the user to enter


the no of elements to be sorted
scanf("%d", &n);
int array[n]; //Array introduced after taking size

printf("Enter %d integers\n", n);

for (c = 0; c < n; c++) //creating array with elements given


scanf("%d", &array[c]);

for (c = 0 ; c < n - 1; c++) //two loops for sorting


{
for (d = 0 ; d < n - c - 1; d++)
{
Name: Ruturaj Uttarwar Class: TY ET-D
GR No: 12020036 Roll No: 60

if (array[d] > array[d+1]) //swapping elements for ascending


order
{
swap = array[d];
array[d] = array[d+1];
array[d+1] = swap;
}
}
}

printf("Sorted list in ascending order:\n"); //printing sorted


array

for (c = 0; c < n; c++)


printf("%d\n", array[c]);

return 0;
}

Output:
Name: Ruturaj Uttarwar Class: TY ET-D
GR No: 12020036 Roll No: 60

You might also like