Selection Sort (java code implementation)

Algorithm Description:

  • In a disordered array of length N, the n-1 first pass find the minimum number and the first number of the exchange.
  • Traversing the second time n-2 from the number of the next
    number, find the minimum number and the second number of the exchange.
  • Repeat these steps until the n-1 traversal minimum number n-1 and the number of exchange, the sort is complete.
    Here Insert Picture Description

For example

Change in said first state of each step, the details behind the introduction, the conventional random array [624159]

Find the smallest number 1 first trip into the far front (with the first digital switching)

Before the exchange: | 6 | 2 | 4 | 1 | 5 | 9 |

After the exchange: | 1 | 2 | 4 | 6 | 5 | 9 |

A second minimum number of times to find the remaining number [24659] Lane 2, the current exchanged with the first digit of the array, not the actual exchange, already at the first place

Before switching: | 1 | 2 | 4 | 6 | 5 | 9 |

After the exchange: | 1 | 2 | 4 | 6 | 5 | 9 |

Continue to find the remaining third trip [4659] minimum number 4 in the figures, there is no actual exchange, 4 need to be exchanged first position

The fourth times to find the minimum number of 5, and the first digit from the remaining [659] switched in position 6

Before switching: | 1 | 2 | 4 | 6 | 5 | 9 |

After the exchange: | 1 | 2 | 4 | 5 | 6 | 9 |

The fifth trip to find from the remaining [69] where the minimum number of 6 and found it to stay in the correct position, there is no exchange

Sorted output the correct result [124569]

The first details of the trip to find the minimum number of 1

Current array is | 6 | 2 | 4 | 1 | 5 | 9 |

6 first taken out, it played the minimum number of

The current minimum number 6 and number one by the other, it is found more decimal exchanged roles

6 compared with the current minimum number of 2, was found more decimal, switch roles, the minimum number is 2 at this time, compared with the next two remaining digits

2 compared with the current minimum number of 4, fixed

The current minimum number 2 and 1, it is found more decimal, switch roles, at this time is a minimum number, then a comparison with the remaining figures

Compared with the current minimum number of 51, fixed

The current minimum number of comparison 1 and 9, fixed, reach the end

The current minimum number of exchange 1 to the current position of the first number, as shown in FIG.

Before the exchange: | 6 | 2 | 4 | 1 | 5 | 9 |

After the exchange: | 1 | 2 | 4 | 6 | 5 | 9 |

Sorting trip completion, the remaining steps are similar

package sort;

public class 选择排序 {

	/**
	 * @param args
	 */
	public static void main(String[] args) {

		int []arr={101,34,119,1};
		selectSort(arr);
		for (int i = 0; i < arr.length; i++) {
			System.out.println(arr[i]);
		}
	}
	public static void  selectSort(int[] arr) {
		for (int i = 0; i < arr.length-1; i++) {//此时的i < arr.length-1;与i < arr.length无大区别,当循环三次时,最后一个数自然出现
			int minIndex=i;
			int min=arr[i];
			for (int j = i+1; j < arr.length; j++) {//此时的j=i+1 与j=i没有大区别,第一个是假定的第一位最小值 不与本身比较;第二个 是与本身比较
				if(min>arr[j]){
					min=arr[j];
					minIndex=j;
				}		
			}
			
			if(minIndex !=i){
				/*arr[minIndex]=arr[i];
				arr[i]=min;
				*/
				int temp = arr[i];
                arr[i] = arr[minIndex];
                arr[minIndex] = temp;
			}
		}
		
	
	}

}

Published 58 original articles · won praise 20 · views 10000 +

Guess you like

Origin blog.csdn.net/qq_40709110/article/details/101556750