This article describes the advanced selection sorting implementation method of Java sorting. Share it for your reference. The details are as follows:
Selection sort is a simple and intuitive sorting algorithm. It works as follows. First, find the smallest (large) element in the unsorted sequence, store it at the starting position of the sorted sequence, and then continue to look for the smallest (large) element from the remaining unsorted elements, and then place it at the end of the sorted sequence. And so on until all elements are sorted.
The main advantages of selecting sorting are related to data movement. If an element is in the correct final position, it will not be moved. Select Sort: swap a pair of elements at each time, at least one of them will be moved to its final position, so that the table of n elements is sorted for a total of up to n-1 exchanges. Among all sorting methods that rely entirely on exchange to move elements, selecting sorting is a very good one.
Worst time complexity О(n²)
Optimal time complexity О(n²)
Average time complexity О(n²)
Worst space complexity О(n) total, O(1) auxiliary
Code implementation:
package com.baobaotao.test; /** * Sort research* */ public class Sort { /** * Select sort* @param array array*/ public static void selectSort(int[] array) { int length = array.length ; int index = 0 ; for(int i=0;i<length-1;i++) { index = i ; for(int j=i+1;j<length;j++) { if(array[j] < array [index]) { index = j ; } } swap(array, i, index) ; printArr(array) ; } } /** * Swap arrays in order from small to large* @param a Array passed in * @param b The number to be exchanged incoming b * @param c The number to be exchanged incoming c */ public static void swap(int[] a, int b, int c) { if(b == c) return ; int temp = a[b] ; a[b] = a[c] ; a[c] = temp ; } /** * Print array* @param array */ public static void printArr(int[] array) { for( int c : array) { System.out.print(c + " "); } System.out.println(); } public static void main(String[] args) { int[] number={11,95,45 ,15,78,84,51,24,12} ; selectSort(number) ; } }
Output:
11 95 45 15 78 84 51 24 1211 12 45 15 78 84 51 24 9511 12 15 24 45 51 84 78 9511 12 15 24 45 51 84 78 9511 12 15 24 45 51 78 84 9511 12 15 24 45 51 78 84 95
I hope this article will be helpful to everyone's Java programming.