파이썬 코드 구현
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)
자바 코드 구현
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));
}
}