2018-11-08 15:37:38 +08:00
|
|
|
//sorting of linked list using insertion sort
|
2017-03-23 22:15:17 +08:00
|
|
|
#include <stdio.h>
|
|
|
|
|
2018-11-08 15:37:38 +08:00
|
|
|
/*Displays the array, passed to this method*/
|
|
|
|
void display(int arr[], int n){
|
|
|
|
|
|
|
|
int i;
|
|
|
|
for(i = 0; i < n; i++){
|
|
|
|
printf("%d ", arr[i]);
|
|
|
|
}
|
|
|
|
|
|
|
|
printf("\n");
|
|
|
|
|
|
|
|
}
|
2017-10-18 04:32:52 +08:00
|
|
|
|
2018-11-08 15:37:38 +08:00
|
|
|
/*This is where the sorting of the array takes place
|
|
|
|
arr[] --- Array to be sorted
|
|
|
|
size --- Array Size
|
|
|
|
*/
|
|
|
|
void insertionSort(int arr[], int size){
|
|
|
|
int j,temp,i;
|
|
|
|
for(i=0; i<size; i++) {
|
|
|
|
temp = arr[(j=i)];
|
|
|
|
while(--j >= 0 && temp < arr[j]) {
|
|
|
|
arr[j+1] = arr[j];
|
|
|
|
arr[j] = temp;
|
2017-03-23 22:15:17 +08:00
|
|
|
}
|
2018-11-08 15:37:38 +08:00
|
|
|
}
|
|
|
|
}
|
2017-03-23 22:15:17 +08:00
|
|
|
|
2018-11-08 15:37:38 +08:00
|
|
|
int main(int argc, const char * argv[]) {
|
|
|
|
int n;
|
|
|
|
printf("Enter size of array:\n");
|
|
|
|
scanf("%d", &n); // E.g. 8
|
|
|
|
|
|
|
|
printf("Enter the elements of the array\n");
|
|
|
|
int i;
|
|
|
|
int arr[n];
|
|
|
|
for(i = 0; i < n; i++){
|
|
|
|
scanf("%d", &arr[i] );
|
|
|
|
}
|
|
|
|
|
|
|
|
printf("Original array: ");
|
|
|
|
display(arr, n); // Original array : 10 11 9 8 4 7 3 8
|
|
|
|
|
|
|
|
insertionSort(arr, n);
|
|
|
|
|
|
|
|
printf("Sorted array: ");
|
|
|
|
display(arr, n); // Sorted array : 3 4 7 8 8 9 10 11
|
|
|
|
|
|
|
|
return 0;
|
2017-03-23 22:15:17 +08:00
|
|
|
}
|
2018-11-08 15:37:38 +08:00
|
|
|
|