Lab 13: Array manipulation, Sorting algorithm
Write a program with the following requirements:
Accepts a list of integer then displays the list in ascending order (lab 12).
Enter an integer an insert it in the right order of the list (the list is still in sorted order).
The program should have the interface as following:
Sorting program
Enter number of integer (n<20) n =
Item 1, i[0]=
Item 2, i[1]=
…
Item n, i[n-1]=
Sorted list:
Enter an integer m=
New list:
Press any key to continue.
#include <stdio.h>
#include <conio.h>
#include<string.h>
int main()
{
while(1)
{
int n, i,j,b,m;
int a[21];
//int l = strlen(a);
printf("Nhap so phan tu n cua day: ");
scanf("%d",&n);
for (i=1;i<=n;i++)
{
printf("Phan tu thu %d ",i);
scanf("%d",&a[i]);
}
printf("Day duoc sap xep:\n");
for (i=1;i<=n;i++)
for (i=1;i<=n;i++)
for(j=1;j<=n-1;j++) if (a[j+1]<a[j])
{
b=a[j];
a[j]=a[j+1];
a[j+1]=b;
}
for(i=1;i<=n;i++)
printf("%d\n",a[i]);
printf("Nhap phan tu m bo sung: ");
scanf("%d",a[i+1]);
printf("Day duoc sap xep lai:\n");
for (i=1;i<=n+1;i++)
for (i=1;i<=n+1;i++)
for(j=1;j<=n-1;j++) if (a[j+1]<a[j])
{
b=a[j];
a[j]=a[j+1];
a[j+1]=b;
}
for(i=1;i<=n+1;i++)
printf("%d\n",a[i]);
printf("\nPress any key to continue...\n");
getch();
}
}