Skip to content

Instantly share code, notes, and snippets.

@taichi-ishitani
Last active October 25, 2016 16:06
Show Gist options
  • Save taichi-ishitani/6a9a78c843ae5f3abbe1579d7775cd02 to your computer and use it in GitHub Desktop.
Save taichi-ishitani/6a9a78c843ae5f3abbe1579d7775cd02 to your computer and use it in GitHub Desktop.
選択ソート/Selection Sort
void selection_sort(int values[], int size) {
int i;
int j;
int min_index;
int temp;
for (i = 0;i < (size - 1);i++) {
min_index = i;
for (j = (i + 1);j < size;j++) {
if (values[min_index] > values[j]) {
min_index = j;
}
}
temp = values[i];
values[i] = values[min_index];
values[min_index] = temp;
}
}
def selection_sort(values)
return values if values.size < 2
0.upto(values.size - 2) do |i|
min_index = i
(i + 1).upto(values.size - 1) do |j|
min_index = j if values[min_index] > values[j]
end
values[min_index], values[i] = values[i], values[min_index]
end
values
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment