Skip to content

Instantly share code, notes, and snippets.

@Unkn0wnCat
Created September 2, 2019 19:19
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 Unkn0wnCat/f00b9a62038178ad47e2da80ae2532d2 to your computer and use it in GitHub Desktop.
Save Unkn0wnCat/f00b9a62038178ad47e2da80ae2532d2 to your computer and use it in GitHub Desktop.
public class selectionSort{
public static void sortArray(int[] array){
for (int i=0;i<array.length ;i++ ) {
printArray(array);
int minimum = findMinimum(array, i, array.length-1);
swap(array,i, minimum);
}
}
private static void swap(int[]array, int position1, int position2){
int tmp=array[position1];
array[position1] = array[position2];
array[position2] = tmp;
}
private static int findMinimum(int[] array,int startIndex, int endIndex){
int position = startIndex;
int currentMinimum = array[startIndex];
for (int i = startIndex+1;i<=endIndex ;i++ ) {
if (array[i]<currentMinimum) {
position = i;
currentMinimum = array[i];
}
}
return position;
}
public static void main(String[] args){
int[] array = {3,5,3,2, 6, 4, 8, 6, 4, 9};
sortArray(array);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment