top of page
Click here to go to the home page of AskTheCode.

Insertion Sorting in C

Team ATC

Random Post

 

#include<stdio.h>

void swap(int *m,int *n){

int temp=*m;

*m=*n;

*n=temp;

}


void insertionSort(int arr[],int n){

int i,j,flag=0;

for (i=1; i<n; i++) {

flag=arr[i];

j = i - 1;

while (j>=0 && arr[j]>flag) {

arr[j + 1]=arr[j];

j=j-1;

}

arr[j + 1]=flag;

}

}


int main(){

int i,j,n,arr[n];

printf("Enter the number of elements: ");

scanf("%d",&n);

printf("\nEnter the elements:\n");

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

scanf("%d",&arr[i]);

}

printf("The ascending order of elements using Insertion Sorting is:\n");

insertionSort(arr,n);

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

printf("%d ",arr[i]);

}

return 0;

}

Recent Posts

See All

Komentar


bottom of page