ok, so I am suppose to get the algorithm of the selection sort, but modify it were it read from right to left and places the largest number at the end of the array. For example the array {3, 5, 2, 1, 4} will be { 5, 4, 3, 2, 1}. You may say just use the regular sort and then reverse the result, but that is not the way I am looking for. So I attempted this and came up with this result.
int febCount = 50;
long[] feb = new long[febCount];
feb[0] = 1;
feb[1] = 1;
for (int i = 2; i < febCount; i++) {
feb[i] = feb[i - 1] + feb[i - 2];
}
for (int i = 0; i < febCount; i++) {
System.out.print(feb[i] + ((i % 10 == 9) ? "\n" : " "));
if (feb[i] % 3 == 0)
System.out.print("skip");
}
Ok, that is what I came up with. Did I do the algorithm correctly?
答案 0 :(得分:0)
这是Java中选择排序的实现(减少最终顺序)。
static void selectionSort(int[] a) {
for (int i=0; i<a.length-1; i++) {
int max=i;
for (int j=i+1; j<a.length; j++) {
if (a[j]>a[max]) {
max=j;
}
}
swap(a[i], a[max]);
}
}