-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path23_gnome_sort.c
More file actions
48 lines (41 loc) · 918 Bytes
/
23_gnome_sort.c
File metadata and controls
48 lines (41 loc) · 918 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
45
46
47
48
#include<stdio.h>
// Function to Sort Array using Gnome Sort
void gnome_sort(int arr[], int n)
{
int pos = 0, temp;
while(pos < n)
if ((pos == 0) || (arr[pos] >= arr[pos - 1]))
pos++;
else
{
temp = arr[pos];
arr[pos] = arr[pos - 1];
arr[pos - 1] = temp;
pos--;
}
}
int main()
{
int arr[10];
int n, index;
printf("Enter Number of Elements : ");
scanf("%d", &n);
if(n > 10 || n < 0)
{
printf("Invalid Input");
return 0;
}
printf("Enter Elements\n");
for(index = 0 ; index < n ; index++)
{
printf("Element %d : ", index + 1);
scanf("%d", &arr[index]);
}
gnome_sort(arr, n);
printf("\nSorted Array\n");
for(index = 0 ; index < n ; index++)
{
printf("Element %d : %d\n", index + 1, arr[index]);
}
return 0;
}