DELETION OF ARRAY
INSERTION SORT :-
ALGORITHM:=
STEP1. SET A[0]:= INFINITY
STEP2. REPEAT STEP 3 TO 5 FOR K =2,3,...N
STEP3. SET TEMP:= A[K] AND PTR:=K-1
STEP 4. REPEAT WHILE TEMP<A[PTR]
A) SET A[PTR+1]:=A[PTR]
B) set PTR:=PTR-1
STEP5. SET A[PTR+1];=TEMP
STEP6. RETURN
CODE:-
#include<stdio.h>
#include<conio.h>
void insertionSort(int arr[], int size) {
int i, j, key;
for (i = 1; i < size; i++)
{
key = arr[i];
j = i - 1;
while (j >= 0 && arr[j] > key) {
arr[j + 1] = arr[j];
j = j - 1;
}
arr[j + 1] = key;
}
}
void main() {
int a[5], i, size = 5;
clrscr();
printf("Enter array elements:\n");
for (i = 0; i < size; i++) {
scanf("%d", &a[i]);
}
printf("Array before sorting:\n");
for (i = 0; i < size; i++) {
printf("%d ", a[i]);
}
printf("\n");
insertionSort(a, size);
printf("Array after insertion sort:\n");
for (i = 0; i < size; i++) {
printf("%d ", a[i]);
}
printf("\n");
getch();
}
Comments
Post a Comment
Kaushikmadhav77@gmail.com