Last active
October 25, 2016 16:06
-
-
Save taichi-ishitani/6a9a78c843ae5f3abbe1579d7775cd02 to your computer and use it in GitHub Desktop.
選択ソート/Selection Sort
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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