Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
55 changes: 55 additions & 0 deletions Sorting/Insertion Sort/insertionSort.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
#include <stdlib.h>
#include <stdio.h>

void insertionSort(int *arr, int n)
{
int i, key, j;
for (i = 1; i < n; 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 input(int *arr, int n){
printf("Elements: ");
for (int i=0; i<n; ++i){
scanf("%d",&arr[i]);
}
}

void printArray(int *arr, int n){
int i;
for (i = 0; i < n; i++)
printf("%d ", arr[i]);
printf("\n");
}

int main(){

int *arr, NoOfElements;
// Taking number of Elements in the array from the user
printf("Number of elements: ");
scanf("%d",&NoOfElements);
if(NoOfElements==0){
printf("You entered 0. Nothing to Sort\n");
return 0;
}
arr=(int*)malloc(NoOfElements*sizeof(int));

//calling input function which will take elements of array from the user
input(arr, NoOfElements);

//callling function insertionSort which will sort the array using insertionSort
insertionSort(arr, NoOfElements);
printf("Sorted array is: \n");
//Printing sorted array
printArray(arr, NoOfElements);

return 0;
}