Selection Sort 視覺化

互動排序演算法教學

← 回到首頁

輸入區

尚未開始

統計

0
比較
0
交換

演算法說明

選擇排序每一輪從未排序區段中挑出最小元素,然後與當前位置交換,逐步將最小值移到前端。

偽代碼:

SELECTION-SORT(A)
  n = A.length
  for i = 1 to n-1
    minIndex = i
    for j = i+1 to n
      if A[j] < A[minIndex]
        minIndex = j
    swap A[i] with A[minIndex]

複雜度:最佳/平均/最壞皆 O(n²),空間 O(1)

優點:實作簡單、空間使用少;缺點:不穩定、對大資料效能較差。