Created
April 13, 2017 23:45
-
-
Save artlovan/84da59c79825076bd9aa91b9c16ef43d to your computer and use it in GitHub Desktop.
SelectionSearch in Java
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
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