forked from sat5297/hacktober-coding
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgnomeSort.java
More file actions
33 lines (31 loc) · 744 Bytes
/
gnomeSort.java
File metadata and controls
33 lines (31 loc) · 744 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
//Implementation of gnome sort in java
import java.util.Arrays;
class gnomeSort {
void gnomeSort(int[] nums)
{
int i=1;
int j=2;
while(i < nums.length) {
if ( nums[i-1] <= nums[i] )
{
i = j; j++;
} else {
int tmp = nums[i-1];
nums[i-1] = nums[i];
nums[i--] = tmp;
i = (i==0) ? j++ : i;
}
}
}
// Method to test above
public static void main(String args[])
{
gnomeSort ob = new gnomeSort();
int nums[] = {7, -5, 3, 2, 1, 0, 45};
System.out.println("Original Array:");
System.out.println(Arrays.toString(nums));
ob.gnomeSort(nums);
System.out.println("Sorted Array");
System.out.println(Arrays.toString(nums));
}
}