Advertisement
Guest User

Untitled

a guest
Nov 20th, 2018
79
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.54 KB | None | 0 0
  1. //Use selection sort to sort an Integer array
  2. public static void selectionSort(List<Integer> myList){
  3. //loop through array length
  4. for(int a = 0; a < myList.size(); a++) {
  5. int smallestNumberIndex = a;
  6. //loop through array length to find smallest number in the range of: a - myList.size()
  7. for (int i = a; i < myList.size(); i++) {
  8. //if number is smallest, set current smallest number index to the index the new smallest number is at
  9. if (myList.get(smallestNumberIndex) > myList.get(i)) {
  10. smallestNumberIndex = i;
  11. }
  12. }
  13. //set the number at the smallestNumberIndex right at the start of the range from: a - myList.size(); in the array
  14. myList.add(a, myList.remove(smallestNumberIndex));
  15. }
  16. }
  17.  
  18. //Use insertion sort to sort an Integer array
  19. public static void insertionSort(List<Integer> myList){
  20. //loop through array length excluding last array
  21. for(int a = 0; a < myList.size() - 1; a++){
  22. //loop through Integers in array ranging from 0 to (a + 1)
  23. for(int i = 0; i < a + 1; i++){
  24. //check if current Integer being checked is > the new Integer outside of the range
  25. if(myList.get(i) > myList.get(a + 1)){
  26. //set the outside Integer right before the current Integer in the array
  27. myList.add(i, myList.remove(a + 1));
  28. }
  29. }
  30. }
  31. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement