Slip 4_1: Read the ‘n’ numbers from user and sort using bubble sort

 Solution :
#include <stdio.h>
void main(){
 int a[20],i,n,temp;
 printf("enter number of elements :");
 scanf("%d",&n);
 for(i=0;i<n;i++)
 {
 printf("enter number :");
 scanf("%d",&a[i]);
 }
 printf("/n before sorting :");
 for(i=0;i<n;i++)
 {
 printf("%d\t",a[i]);
 }
 printf("/n after sorting :");
 for(i=0;i<n;i++)
 {
 if(a[i]>a[i+1])
 {
 temp=a[i];
 a[i]=a[i+1];
 a[i+1]=temp;
 }
 }
 for(i=0;i<n;i++)
 {
 printf("%d\t",a[i]);
 }
}

Comments

Popular posts from this blog

Slip 22_2: Read the data from file 'cities.txt' containing names of cities and their STD codes. Accept a name of the city from user and use sentinel linear search algorithm to check whether the name is present in the file and output the STD code, otherwise output “city not in the list”. Solution :

Slip10_2, 30_1 : Read the data from the file “employee.txt” and sort on names in alphabetical order (use strcmp) using bubble sort or selection sort

Slip 23_2: Read the data from file ‘sortedcities.txt’ containing sorted names of cities and their STD codes. Accept a name of the city from user and use binary search algorithm to check whether the name is present in the file and output the STD code, otherwise output “city not in the list”.