Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- //Use selection sort to sort an Integer array
- public static void selectionSort(List<Integer> myList){
- //loop through array length
- for(int a = 0; a < myList.size(); a++) {
- int smallestNumberIndex = a;
- //loop through array length to find smallest number in the range of: a - myList.size()
- for (int i = a; i < myList.size(); i++) {
- //if number is smallest, set current smallest number index to the index the new smallest number is at
- if (myList.get(smallestNumberIndex) > myList.get(i)) {
- smallestNumberIndex = i;
- }
- }
- //set the number at the smallestNumberIndex right at the start of the range from: a - myList.size(); in the array
- myList.add(a, myList.remove(smallestNumberIndex));
- }
- }
- //Use insertion sort to sort an Integer array
- public static void insertionSort(List<Integer> myList){
- //loop through array length excluding last array
- for(int a = 0; a < myList.size() - 1; a++){
- //loop through Integers in array ranging from 0 to (a + 1)
- for(int i = 0; i < a + 1; i++){
- //check if current Integer being checked is > the new Integer outside of the range
- if(myList.get(i) > myList.get(a + 1)){
- //set the outside Integer right before the current Integer in the array
- myList.add(i, myList.remove(a + 1));
- }
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement