新的修改選擇排序算法(在某些情況下優(yōu)于插入排序)與標(biāo)準(zhǔn)選擇排序算法類似,但它只在 Array 中搜索最小值,而是在一次迭代中搜索最小值和最大值。然后它將最小值交換到數(shù)組的開頭,將最大值交換到數(shù)組的末尾。遞增start_pointer、遞減end_pointer,然后再次迭代。我認為對 N 大小的數(shù)組進行排序所需的時間復(fù)雜度是:N + (N-2) + (N-4) + (N-6) + ... + 1。有人可以給我這個公式的近似值嗎?我將不勝感激。public static void DoubleSelectionSort(int[] Array, int startpos, int endpos) { /*Double Selection Sort Algorithm , made by Milan Domonji 1986 , MilanDomonji@yahoo.com*/ while(startpos < endpos) { int min = Integer.MAX_VALUE; int max = Integer.MIN_VALUE; int minpos = startpos; int maxpos = endpos; for(int i = startpos; i <= endpos; i++) { //main loop that finds minimum and maximum from an Array if (Array[i] <= min) { min = Array[i]; minpos = i; } if (Array[i] >= max) { max = Array[i]; maxpos = i; } } /* I added missing part of algorithm so it works now (Edited) */ if (maxpos==startpos && minpos==endpos) { Swap(Array, minpos, maxpos); }else { if (maxpos==startpos) { Swap(Array,minpos,startpos); Swap(Array,minpos,endpos); }else { Swap(Array,minpos,startpos); Swap(Array,maxpos,endpos); } } startpos++; endpos--; }}private static void Swap(int[] Array, int A, int B) { int tmp = Array[A]; Array[A] = Array[B]; Array[B] = tmp;}算法對所有時間進行正確排序。
您能否給我我制作的這個新選擇排序算法的時間近似值(例如 n^2 , n*2 , nlogn )
函數(shù)式編程
2023-07-28 09:35:16