Created
September 2, 2019 19:19
-
-
Save Unkn0wnCat/f00b9a62038178ad47e2da80ae2532d2 to your computer and use it in GitHub Desktop.
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 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