Java 程式,實現選擇排序
選擇排序是一種簡單的排序演算法。這種排序演算法是一種基於比較的原地排序演算法,其中列表被分為兩部分,左側是有序部分,右側是無序部分。最初,有序部分為空,無序部分是整個列表。
從無序陣列中選擇最小的元素,並將其與最左邊的元素交換,這個元素就變成了有序陣列的一部分。這個過程會繼續,將無序陣列的邊界從一個元素往右移動。
演算法
1.Set MIN to location 0 2.Search the minimum element in the list 3.Swap with value at location MIN 4.Increment MIN to point to next element 5.Repeat until the list is sorted
示例
public class SelectionSort {
public static void main(String args[]){
int array[] = {10, 20, 25, 63, 96, 57};
int size = array.length;
for (int i = 0 ;i< size-1; i++){
int min = i;
for (int j = i+1; j<size; j++){
if (array[j] < array[min]){
min = j;
}
}
int temp = array[min];
array[min] = array[i];
array[i] = temp;
}
for (int i = 0 ;i< size; i++){
System.out.print(" "+array[i]);
}
}
}廣告
資料結構
網路
RDBMS
作業系統
Java
iOS
HTML
CSS
Android
Python
C 程式設計
C++
C#
MongoDB
MySQL
Javascript
PHP