Created
October 13, 2016 20:33
-
-
Save batuhankirmizi/485d9fafa0ad4e4a3be20fc475ff7b4c to your computer and use it in GitHub Desktop.
A simple sorting algorithm using java
This file contains hidden or 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
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