This article is participating in the “Java Theme Month – Java Brush questions punch card”, see the activity link for details

1. Algorithm idea

The sequence to be sorted is divided into two parts, one is ordered sequence, the other is unordered sequence. The first trip is to find the smallest number a[I] from a[0] to a[n-1], and then swap a[I] with a[0]. The second trip is to find the smallest number A [j] from a[1] to a[n-1], and then swap a[j] with a[1]. The third trip is to find the smallest number A [j] and a[1]. Find the smallest number a[k] from a[2] to a[n-1], and then swap a[k] with a[2]…

2. Case analysis

13,15,37,89,60,39,12,109,56,72 {}

The first trip: 12 15,37,89,60,39,13,109,56,72 {}

Second run: 12, 13 {37,89,60,39,15,109,56,72}

Third trip: 12, 13, 15 {89,60,39,37,109,56,72}

3. Algorithm code

/** * select sort *@author xcbeyond
 *
 */
public class SelectSort {
	public static void main(String[] args) {
		// TODO Auto-generated method stub
		int a[] ={13.15.37.89.60.39.12.109.56.72};int i;
		int j;
		
		for(i = 0; i<a.length; i++) System.out.print(a[i]+"");
		System.out.println();
		
		for(i = 0; i<a.length; i++){int min = a[i];
			for(j = i+1; j<a.length; j++){if(min>a[j]){
					int temp;
					temp = min;
					min = a[j];
					a[j] = temp;
				}
			}
			a[i] = min;
		}
		
		for(i = 0; i<a.length; i++) System.out.print(a[i]+""); }}Copy the code