Selection Sort
private static int[] Selection(int data[]) {
int min;
int temp;
for (int i = 0; i < data.length; i++) {
min = i; //assume i is minimum
for (int j = i + 1; j < data.length; j++) {
if (data[j] < data[min]) {
min = j; //get really minimum
}
}
if (min != i) { //if i not minimum, then exchange
temp = data[i];
data[i] = data[min];
data[min] = temp;
}
Show(data, i);
}
return data;
}
int min;
int temp;
for (int i = 0; i < data.length; i++) {
min = i; //assume i is minimum
for (int j = i + 1; j < data.length; j++) {
if (data[j] < data[min]) {
min = j; //get really minimum
}
}
if (min != i) { //if i not minimum, then exchange
temp = data[i];
data[i] = data[min];
data[min] = temp;
}
Show(data, i);
}
return data;
}
留言
張貼留言