Absrtact: The direct selection sort belongs to the selection sort, but its sorting algorithm is faster than the bubble sort, because its algorithm is relatively simple, so it is more suitable for beginners to learn mastery.
Suitable crowd: There is a certain Java SE Foundation, understand the Java data type, array definition, initialization and common array methods, as well as the Java loop operation.
Pre-Preparation: It is best to have a development tool such as: Eclipse or MyEclipse can, of course, you use a DOS system to compile and run, but the bug will be a bit troublesome.
- Sorting principle: The principle of direct selection is to compare the specified sort position with other array elements, if the value of the position is swapped if the condition is met, note that the bubble sort is not exchanged, instead of exchanging adjacent elements, instead of exchanging the elements that satisfy the condition with the specified sort position.
- Compare bubble Sort: The direct selection sort is much smaller than the bubble sort, so the speed is a bit faster.
- Advantages and disadvantages of direct ordering: The operation speed is faster than bubble sort, but because there is an interchange between the nonadjacent elements in the direct selection sort, the direct selection of the sorting is an unstable sort method.
- Algorithm implementation:
1 PackageLiuenci.cn.package_3;2 3 Public classSelectsort {4 5 /**6 * Direct Select sort7 */8 Public Static voidMain (string[] args) {9 //TODO auto-generated Method StubTen //create an array of random order One int[] array={2,55,62,44,25,16}; A //creating objects that are sorted directly -Selectsort sorter=NewSelectsort (); - //call the direct selection algorithm to sort the array the Sorter.sort (array); - } - Public voidSortint[] Array) { - intindex; + for(inti=1;i<array.length;i++){ -Index=0; + for(intj=1;j<=array.length-i;j++){ A if(array[j]>Array[index]) { atindex=J; - } - //swap two numbers on position array.length-i and index (maximum) - inttemp=array[array.length-i]; -array[array.length-i]=Array[index]; -array[index]=temp; in } - to } + Showarray (array); - } the Public voidShowarray (int[] Array) { * //TODO auto-generated Method Stub $ for(inti=0;i<array.length;i++){Panax Notoginseng intnum=Array[i]; -System.out.print (num+ ""); the } + System.out.println (); A } the +}
How "Java SE" uses Java to directly select sort