Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /**
- * Selection Sort
- *
- * Prompt: Given an unsorted array of integers, return the array sorted
- * using selection sort.
- *
- * Input: input {Array}
- * Output: {Array}
- *
- * Example: [3,9,1,4,7] --> [1,3,4,7,9]
- *
- */
- import java.util.*;
- class BasicSort {
- public static void main(String[] args){
- int[] arr = {3,9,1,4,7};
- System.out.println(Arrays.toString(selection(arr)));
- }
- public static int[] selection(int[] input) {
- int len = input.length;
- int currMin;
- for(int lastSorted = 0; lastSorted < len -1; lastSorted++){
- currMin = lastSorted;
- //start comparisons at after the lastSorted element
- for(int i = lastSorted + 1; i < len; i++){
- if(input[i] < input[currMin]){
- currMin = i;
- }
- }
- int temp = input[currMin];
- input[currMin] = input[lastSorted];
- input[lastSorted] = temp;
- }
- return input;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement