Skip to content

Instantly share code, notes, and snippets.

@artlovan
Created April 13, 2017 23:45
Show Gist options
  • Save artlovan/84da59c79825076bd9aa91b9c16ef43d to your computer and use it in GitHub Desktop.
Save artlovan/84da59c79825076bd9aa91b9c16ef43d to your computer and use it in GitHub Desktop.
SelectionSearch in Java
public class SelectionSearch {
private static int[] data = new int[]{9, 1, 43, 5, 20, 50, 7, 22, 43, 17, 47};
public static void main(String[] args) {
System.out.println(Arrays.toString(data));
selectionSort();
System.out.println(Arrays.toString(data));
}
public static void selectionSort() {
for (int i=0; i < data.length; i++) {
int min = i;
for (int j = i; j < data.length; j++) {
if (data[min] > data[j]) {
min = j;
}
}
swapValues(min, i);
}
}
private static void swapValues(int indexOne, int indexTwo) {
int temp = data[indexOne];
data[indexOne] = data[indexTwo];
data[indexTwo] = temp;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment