Upload files to "DS/C/Lab/Week1"
This commit is contained in:
parent
181f9113d2
commit
51cc29eb50
5 changed files with 242 additions and 0 deletions
43
DS/C/Lab/Week1/insertionsort.c
Normal file
43
DS/C/Lab/Week1/insertionsort.c
Normal file
|
@ -0,0 +1,43 @@
|
|||
#include <stdio.h>
|
||||
|
||||
int main()
|
||||
{
|
||||
int n, i, j, temp;
|
||||
int flag = 0;
|
||||
|
||||
printf("Enter the number of elements you want in the array: ");
|
||||
scanf("%d", &n);
|
||||
|
||||
int arr[n];
|
||||
|
||||
printf("Enter the numbers you want in the array:\n");
|
||||
for (i = 0; i < n; i++)
|
||||
{
|
||||
scanf("%d", &arr[i]);
|
||||
}
|
||||
|
||||
printf("The numbers in the array are:\n");
|
||||
for (i = 0; i < n; i++)
|
||||
{
|
||||
printf("%d\n", arr[i]);
|
||||
}
|
||||
|
||||
// sorting
|
||||
for (i = 0; i < n; i++)
|
||||
{
|
||||
int key = arr[i];
|
||||
int j = i - 1;
|
||||
while (j >= 0 && arr[j] > key)
|
||||
{
|
||||
arr[j + 1] = arr[j];
|
||||
j = j - 1;
|
||||
}
|
||||
arr[j + 1] = key;
|
||||
}
|
||||
|
||||
printf("The numbers in the sorted array are:\n");
|
||||
for (i = 0; i < n; i++)
|
||||
{
|
||||
printf("%d \n", arr[i]);
|
||||
}
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue