-
Notifications
You must be signed in to change notification settings - Fork 167
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #29 from gabrielperes97/GnomeSort
Implemented Gnome sort algorithm - Issue #1
- Loading branch information
Showing
3 changed files
with
63 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,57 @@ | ||
import java.util.Random; | ||
|
||
/* | ||
Gnome Sort is based on the technique used by the standard Dutch Garden Gnome (Du.: tuinkabouter). | ||
Here is how a garden gnome sorts a line of flower pots. | ||
Basically, he looks at the flower pot next to him and the previous one; if they are in the right order he steps one pot forward, otherwise, he swaps them and steps one pot backward. | ||
Boundary conditions: if there is no previous pot, he steps forwards; if there is no pot next to him, he is done. | ||
— "Gnome Sort - The Simplest Sort Algorithm". Dickgrune.com | ||
*/ | ||
public class GnomeSort { | ||
|
||
public static void main(String args[]) | ||
{ | ||
System.out.println("Sorting of randomly generated numbers using GNOME SORT"); | ||
Random random = new Random(); | ||
int N = 20; | ||
int[] sequence = new int[N]; | ||
|
||
for (int i = 0; i < N; i++) | ||
sequence[i] = Math.abs(random.nextInt(100)); | ||
|
||
System.out.println("\nOriginal Sequence: "); | ||
printSequence(sequence); | ||
|
||
System.out.println("\nSorted Sequence: "); | ||
printSequence(gnomeSort(sequence)); | ||
} | ||
|
||
static void printSequence(int[] sortedSequence) | ||
{ | ||
for (int i = 0; i < sortedSequence.length; i++) | ||
System.out.print(sortedSequence[i] + " "); | ||
} | ||
|
||
public static int[] gnomeSort(int arr[]){ | ||
int first = 1; | ||
|
||
while(first < arr.length) | ||
{ | ||
if (arr[first-1] <= arr[first]) | ||
{ | ||
first++; | ||
} | ||
else | ||
{ | ||
int tmp = arr[first-1]; | ||
arr[first - 1] = arr[first]; | ||
arr[first] = tmp; | ||
if (-- first == 0) | ||
{ | ||
first = 1; | ||
} | ||
} | ||
} | ||
return arr; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -8,6 +8,7 @@ public enum SortType { | |
SELECTION, | ||
CYCLE, | ||
SHELL, | ||
COMB | ||
COMB, | ||
GNOME | ||
} | ||
|