下面是我的实现选择排序的:的Java:选择排序我的实现与另一
package algorithm.selectionsort;
public class SelectionSort {
public static void main(String[] args) {
int[] myArray = selectionSort(new int[] { 9, 9, 9, 8, 7, 73, 32, 109, 1100, 432, 321, 0 });
for (int element : myArray) {
System.out.print("" + element + " ");
}
}
public static int[] selectionSort(int[] a) {
int min;
for (int i = 0; i < a.length - 1; i++) {
min = i;
for (int j = i + 1; j < a.length; j++) {
if (a[j] < a[min]) {
min = j;
int temp = a[i];
a[i] = a[min];
a[min] = temp;
}
}
}
return a;
}
}
我发现我的导师代码它略有不同:
public static int[] selectionSort(int[] a) {
int min;
for (int i = 0; i < a.length - 1; i++) {
min = i;
for (int j = i + 1; j < a.length; j++) {
if (a[j] < a[min]) {
min = j;
}
}
int temp = a[i];
a[i] = a[min];
a[min] = temp;
}
return a;
}
两种实施工作。我很好奇这里有什么不同。效率如何?
我明白了 - 我不必要地一遍又一遍地重复相同的代码。效果是一样的,但这会让我放慢抓取庞大的数据集。 –
你的老师实现了选择排序,你发明了另一种看起来非常相似但效率较低的排序。 – alfasin
https://codereview.stackexchange.com – nullpointer