-
Notifications
You must be signed in to change notification settings - Fork 577
Expand file tree
/
Copy pathGnome-sort.c
More file actions
44 lines (34 loc) · 845 Bytes
/
Gnome-sort.c
File metadata and controls
44 lines (34 loc) · 845 Bytes
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
#include <stdio.h>
void gnomeSort(int arr[], int n) {
int index = 0;
while (index < n) {
if (index == 0)
index++;
if (arr[index] >= arr[index - 1])
index++; // in correct order, move forward
else {
// swap elements
int temp = arr[index];
arr[index] = arr[index - 1];
arr[index - 1] = temp;
index--; // move backward after swap
}
}
}
int main() {
int n;
printf("Enter number of elements: ");
scanf("%d", &n);
int arr[n];
printf("Enter elements: ");
for (int i = 0; i < n; i++) {
scanf("%d", &arr[i]);
}
gnomeSort(arr, n);
printf("\nSorted array: ");
for (int i = 0; i < n; i++) {
printf("%d ", arr[i]);
}
printf("\n");
return 0;
}