ソートの選択
9799 ワード
ソートの選択
出典:https://visualgo.net/en/sorting
思考アルゴリズム
アルゴリズム実装
Pythonコードの実装
import random
def selection_sort(data):
for stand in range(len(data) - 1):
lowest = stand
for index in range(stand + 1, len(data)):
if data[lowest] > data[index]:
lowest = index
data[lowest], data[stand] = data[stand], data[lowest]
return data
data_list = random.sample(range(100), 10)
selection_sort(data_list)
Javaコードの実装import java.util.Arrays;
public class SelectionSort {
static void swap(int[] a, int i, int j) {
int temp = a[i];
a[i] = a[j];
a[j] = temp;
}
static void selectionSort(int[] a) {
for (int i = 0; i < a.length - 1; i++) {
int minIndex = i;
for (int j = i + 1; j < a.length; j++ ) {
if (a[minIndex] > a[j]) {
minIndex = j;
}
}
swap(a, i, minIndex);
}
}
public static void main(String[] args) {
int[] a = { 17, 14, 11, 19, 13, 15, 18, 12, 16, 20 };
selectionSort(a);
System.out.println(Arrays.toString(a));
}
}
時間分析の実行
、ʥ7、実際に詳細に計算すると、n(n1)2frac{n*(n-1)}{2(n\1)、67918、
Reference
この問題について(ソートの選択), 我々は、より多くの情報をここで見つけました https://velog.io/@rtong77/선택-정렬-selection-sortテキストは自由に共有またはコピーできます。ただし、このドキュメントのURLは参考URLとして残しておいてください。
Collection and Share based on the CC Protocol