A brief description is: first find the smallest element in the array, and exchange it with the first element of the array; then find the smallest element among the remaining elements and exchange the position with the second element of the array; and so on, until the Sort the entire array.
For an array of length N, it takes about (N^2)/2 comparisons and N exchanges to select sort
2. Insertion sort
Insertion sort, insert each element into the appropriate position in other ordered local arrays, in order to make room for the inserted element, we need to move the remaining elements in the appropriate position by one place;
Simply put, first determine an index value i, starting from this i, define j=i, compare a[j] with a[j-1], if a[j-1]>a[j] exchange the two ,J–, if a[j-1]<a[j], it means that you have reached the correct position, exit the small loop, otherwise continue to repeat the operation until j=0; after completing the small loop, put the index i++.
3. Hill sort
To put it simply, first change the swap interval of "insertion sort" from 1 to h, and then h=h/3 after the round is arranged, and then repeat the operation until h>=1.
The following is the code
import java.util.*;publicclassExample{
//选择排序publicstaticvoidSelectSort(Comparable[] a){
for(int i =0; i < a.length; i++){
int min = i;for(int j = i+1; j < a.length; j++){
if(less(a[j], a[min])){
min = j;}}exch(a, i, min);}}/*插入排序,简单来说就是先确定一个索引值i,从这个i开始,定义j=i,a[j]与a[j-1]比较,若a[j-1]>a[j]则交换二者,j--,
若a[j-1]<a[j]则表明已经到了正确的位置,退出小循环,否则继续重复操作,直到j=0;完成小循环后把索引i++*/publicstaticvoidInsertSort(Comparable[] a){
for(int i =1; i < a.length; i++){
for(int j = i; j >0&&less(a[j], a[j-1]); j--){
exch(a, j, j-1);}}}//希尔排序publicstaticvoidShellSort(Comparable[] a){
int h =1;while(h < a.length/3){
h =3*h +1;}while(h >=1){
for(int i = h; i < a.length; i++){
for(int j = i; j >= h &&less(a[j], a[j-h]); j-=h){
exch(a, j, j-h);}}
h = h /3;}}//做比较判断,返回bool类型publicstaticbooleanless(Comparable v, Comparable w){
return v.compareTo(w)<0;}//交换数组元素privatestaticvoidexch(Comparable[] a,int i,int j){
Comparable t = a[i];
a[i]= a[j];
a[j]= t;}//打印出数组publicstaticvoidshow(Comparable[] a){
for(int i =0; i < a.length; i++){
System.out.print(a[i]+" ");}
System.out.println();}publicstaticvoidmain(String[] args){
Scanner in =newScanner(System.in);int n = in.nextInt();
Comparable[] arr =newComparable[n];for(int i =0; i < arr.length; i++){
arr[i]= in.nextInt();}SelectSort(arr);InsertSort(arr);ShellSort(arr);show(arr);}}