Skip to content

Instantly share code, notes, and snippets.

@batuhankirmizi
Created October 13, 2016 20:33
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save batuhankirmizi/485d9fafa0ad4e4a3be20fc475ff7b4c to your computer and use it in GitHub Desktop.
Save batuhankirmizi/485d9fafa0ad4e4a3be20fc475ff7b4c to your computer and use it in GitHub Desktop.
A simple sorting algorithm using java
public class Sorter {
public static void main(String[] args) {
printArray(sortArray(new int[]{5, 0, 40, 10, 30, 25})); // Output is going to be sorted
}
// Sorts the given array
private static int[] sortArray(int[] array) {
int[] arr = new int[array.length];
int bigger = 0;
for (int i = 0; i < array.length; i++) {
for (int j = 0; j < array.length; j++) {
if(array[i] > array[j]) {
bigger++ ;
}
}
arr[bigger] = array[i];
bigger = 0;
}
return arr;
}
// Simply prints the given array
private static void printArray(int[] array) {
for (int i = 0; i < array.length; i++) {
System.out.print(array[i] + " ");
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment