(DS-5)

 5A.Write a program to implement bubble sort. 

#include<stdio.h>

int main()

{

int a[10],i,j,temp,n;

printf("\nEnter no. of elements for list:");

scanf("%d",&n);

printf("\nEnter %d integers :\n",n);

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("\nBubble Sorted list in ascending order is :");

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

{

printf("\t %d",a[i]);

}

}

5B.Write a program to implement selection sort.

#include<stdio.h>

int main()

{

int a[20],n,i,j,position,t;

printf("\nEnter no. of elements for list:");

scanf("%d",&n);

printf("\nEnter %d integers :\n",n);

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(position != i)

{

t=a[i];

a[i]=a[position];

a[position]=t;

}

}

printf("\nSelection Sorted list in ascending order is :\n");

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

{

printf("%d \n",a[i]);

}

return 0;

}

5C.Write a program to implement insertion sort.

#include<stdio.h>

int main()

{

int n,a[10],i,j,temp;

printf("\nEnter no. of elements for list :");

scanf("%d",&n);

printf("\nEnter %d integers :\n",n);

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

{

scanf("%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--;

}

a[j+1]=temp;

}

printf("\nInsertion sorted list in ascending order is :\n");

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

{

printf("%d \n",a[i]);

}

return 0;

}

Comments

Popular posts from this blog

python(BI)

(PP-7)Create a class called Numbers, which has a single class attribute called MULTIPLIER, and a constructor which takes the parameters x and y (these should all be numbers). i. Write a method called add which returns the sum of the attributes x and y. ii. Write a class method called multiply, which takes a single number parameter a and returns the product of a and MULTIPLIER. iii. Write a static method called subtract, which takes two number parameters, b and c, and returns b - c. iv. Write a method called value which returns a tuple containing the values of x and y. Make this method into a property, and write a setter and a deleter for manipulating the values of x and y.