-
Notifications
You must be signed in to change notification settings - Fork 1k
/
Copy pathInsertionSort.c
58 lines (50 loc) · 1.2 KB
/
InsertionSort.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
/*INSERTION SORT
The array is virtually split into a sorted and an unsorted part.
Values from the unsorted part are picked and placed at the correct position in the sorted part.
*/
#include <stdio.h>
void insertion_sort(int[], int);
int main()
{
int arraySize, index, temp, key;
printf("Enter the size of the array:");
scanf("%d", &arraySize);
int arr[arraySize]; // declaring array by - 'arr'
printf("Enter the elements to sort:");
for (index = 0; index < arraySize; index++)
{
scanf("%d", &arr[index]);
}
insertion_sort(arr, arraySize);
printf("Sorted Array:");
for (index = 0; index < arraySize; index++)
{
printf("%d ", arr[index]);
}
return 0;
}
// Function to sort the array using insertion sort method
void insertion_sort(int arr[], int arraySize)
{
int index, key, temp;
for (index = 1; index < arraySize; index++)
{
key = arr[index]; // the key stores the value at the index position
temp = index - 1;
while (temp >= 0 && arr[temp] > key)
{
arr[temp + 1] = arr[temp];
temp--;
}
arr[temp + 1] = key;
}
}
/*
Sample Output
Enter the size of the array:6
Enter the elements to sort:17 8 9 0 2
Sorted Arrray:0 2 8 9 17
Complexities
Time Complexity:O(n^2)
Space Complexity:O(1)
*/