java排序高等之選擇排序完成辦法。本站提示廣大學習愛好者:(java排序高等之選擇排序完成辦法)文章只能為提供參考,不一定能成為您想要的結果。以下是java排序高等之選擇排序完成辦法正文
本文實例講述了java排序高等之選擇排序完成辦法。分享給年夜家供年夜家參考。詳細以下:
選擇排序(Selection sort)是一種簡略直不雅的排序算法。它的任務道理以下。起首在未排序序列中找到最小(年夜)元素,寄存到排序序列的肇端地位,然後,再從殘剩未排序元素中持續尋覓最小(年夜)元素,然後放到已排序序列的末尾。以此類推,直到一切元素均排序終了。
選擇排序的重要長處與數據挪動有關。假如某個元素位於准確的終究地位上,則它不會被挪動。選擇排序每次交流一對元素,它們傍邊至多有一個將被移到其終究地位上,是以對n個元素的表停止排序總共停止至少n-1次交流。在一切的完整依附交流去挪動元素的排序辦法中,選擇排序屬於異常好的一種。
最差時光龐雜度 О(n²)
最優時光龐雜度 О(n²)
均勻時光龐雜度 О(n²)
最差空間龐雜度 О(n) total, O(1) auxiliary
代碼完成:
package com.baobaotao.test;
/**
* 排序研討
*
*/
public class Sort {
/**
* 選擇排序
* @param 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) ;
}
}
/**
* 按從小到年夜的次序交流數組
* @param a 傳入的數組
* @param b 傳入的要交流的數b
* @param c 傳入的要交流的數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 ;
}
/**
* 打印數組
* @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) ;
}
}
輸入:
11 95 45 15 78 84 51 24 12 11 12 45 15 78 84 51 24 95 11 12 15 45 78 84 51 24 95 11 12 15 24 78 84 51 45 95 11 12 15 24 45 84 51 78 95 11 12 15 24 45 51 84 78 95 11 12 15 24 45 51 78 84 95 11 12 15 24 45 51 78 84 95
願望本文所述對年夜家的java法式設計有所贊助。