basic thought
Select the minimum value from arr[0]~arr[n-1] for the first time,
Exchange with arr[0], select the minimum value from arr[1]~arr[n-1] for the second time, exchange with arr[1], select the minimum value from arr[2]~arr[n-1] for the third time, and exchange with arr[2]
Exchange,..., select the minimum value from arr[i-1]~arr[n-1] for the ith time, exchange with arr[i-1], select the minimum value from arr[n-2]~arr[n-1] for the nth time,
Exchange with arr[n-2] for a total of n-1 times to obtain an ordered sequence arranged from small to large according to the sorting code—— Shang Silicon Valley
Simply put: each time, the smallest value is selected from the unordered array, and then the value is exchanged with the first value in the unordered array.
Animation demonstration
Insert sort example
Java code:
package sort; import java.util.Random; import java.util.Scanner; public class SelectSort { public static void main(String[] args) { int num, min, max;//Number of arrays, minimum, maximum int direction; int[] arr;//array Scanner scanner = new Scanner(System.in); System.out.printf("Please enter the number of arrays to generate randomly:"); num = scanner.nextInt(); System.out.printf("Please enter the minimum value to randomly generate the array:"); min = scanner.nextInt(); System.out.printf("Please enter the maximum value of the array to be randomly generated:"); max = scanner.nextInt(); arr = randomArrays(num,min,max);//Generate array System.out.printf("Generated array:"); traversalOutput(arr); selectSorting(arr);//Select sort } /** * Randomly generated array function * @param num Number of arrays * @param min Array value minimum * @param max Maximum array value * @return */ public static int[] randomArrays (int num, int min, int max){ Random rd = new Random(); int[] arr = rd.ints(num, min, max + 1).toArray(); return arr; } /** * Array traversal output * @param arr array */ public static void traversalOutput (int[] arr){ for (int i : arr) { System.out.printf("%d ", i); } System.out.println(""); } /** * Exchange elements * @param arr array * @param minIndex Current minimum subscript * @param min Current minimum * @param i The subscript of the location where the current minimum value is stored */ public static void exchange (int[] arr, int minIndex, int min, int i){ arr[minIndex] = arr[i]; arr[i] = min; } /** * Selective exchange * @param arr */ public static void selectSorting(int[] arr){ int minIndex, min; for (int i = 0; i < arr.length -1; i++) { minIndex = i;//Subscript of minimum value min = arr[minIndex];//minimum value for (int j = i + 1; j < arr.length; j++) { if (min > arr[j]){ minIndex = j; min = arr[minIndex]; } } if (minIndex != i){ exchange(arr,minIndex,min,i); } System.out.printf("The first%d Array after secondary exchange:",i+1); traversalOutput(arr); } } }
code analysis
/** * Select swap sort * @param arr */ public static void selectSorting(int[] arr){ int minIndex, min; for (int i = 0; i < arr.length -1; i++) { minIndex = i;//Subscript of minimum value min = arr[minIndex];//minimum value for (int j = i + 1; j < arr.length; j++) { if (min > arr[j]){ minIndex = j; min = arr[minIndex]; } } exchange(arr,minIndex,i); System.out.printf("The first%d Array after secondary exchange:",i+1); traversalOutput(arr); } }
The outer loop determines the number of comparisons, and the inner loop determines where the elements to be compared start.
It is assumed that the first element in the unordered element (in fact, any element in the unordered element can be) is the smallest element. Define two variables, one to store the value of the element and the other to store the subscript of the element.
This assumed element is compared with all the remaining unordered elements. If an element is more obvious than it that it is not the smallest element, the subscript and minimum value of the minimum value should be set as the subscript and value of the current element.
When the cycle is completed, the position of the current smallest element can be determined.
Exchange in the outer loop.
Time complexity: this O(n2) (double loop).
Spatial complexity: o(1),.
Algorithm stability: stable.
optimization
When the minimum value of the first selection is the minimum value in the unordered element, the exchange operation in the outer loop is meaningless.
java code:
package sort; import java.util.Random; import java.util.Scanner; public class SelectSort { public static void main(String[] args) { int num, min, max;//Number of arrays, minimum, maximum int direction; int[] arr;//array Scanner scanner = new Scanner(System.in); System.out.printf("Please enter the number of arrays to generate randomly:"); num = scanner.nextInt(); System.out.printf("Please enter the minimum value to randomly generate the array:"); min = scanner.nextInt(); System.out.printf("Please enter the maximum value of the array to be randomly generated:"); max = scanner.nextInt(); arr = randomArrays(num,min,max);//Generate array System.out.printf("Generated array:"); traversalOutput(arr); selectSorting(arr);//Select sort } /** * Randomly generated array function * @param num Number of arrays * @param min Array value minimum * @param max Maximum array value * @return */ public static int[] randomArrays (int num, int min, int max){ Random rd = new Random(); int[] arr = rd.ints(num, min, max + 1).toArray(); return arr; } /** * Array traversal output * @param arr array */ public static void traversalOutput (int[] arr){ for (int i : arr) { System.out.printf("%d ", i); } System.out.println(""); } /** * Exchange elements * @param arr array * @param minIndex Current minimum subscript * @param min Current minimum * @param i The subscript of the location where the current minimum value is stored */ public static void exchange (int[] arr, int minIndex, int min, int i){ arr[minIndex] = arr[i]; arr[i] = min; } /** * Selective exchange * @param arr */ public static void selectSorting(int[] arr){ int minIndex, min; for (int i = 0; i < arr.length -1; i++) { minIndex = i;//Subscript of minimum value min = arr[minIndex];//minimum value for (int j = i + 1; j < arr.length; j++) { if (min > arr[j]){ minIndex = j; min = arr[minIndex]; } } if (minIndex != i){ exchange(arr,minIndex,min,i); } System.out.printf("The first%d Array after secondary exchange:",i+1); traversalOutput(arr); } } }
Optimization code:
Add an additional judgment statement when exchanging elements:
if (minIndex != i){ }