亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

您能否給我我制作的這個新選擇排序算法的時間近似值(例如 n^2 , n*2 , nlogn )

您能否給我我制作的這個新選擇排序算法的時間近似值(例如 n^2 , n*2 , nlogn )

函數式編程 2023-07-28 09:35:16
新的修改選擇排序算法(在某些情況下優于插入排序)與標準選擇排序算法類似,但它只在 Array 中搜索最小值,而是在一次迭代中搜索最小值和最大值。然后它將最小值交換到數組的開頭,將最大值交換到數組的末尾。遞增start_pointer、遞減end_pointer,然后再次迭代。我認為對 N 大小的數組進行排序所需的時間復雜度是: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 , [email protected]*/    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;}算法對所有時間進行正確排序。
查看完整描述

1 回答

?
忽然笑

TA貢獻1806條經驗 獲得超5個贊

如果您需要總和S = N + (N-2) + (N-4) + ... + (N - (N-2))(例如N是偶數),則它等于S = 2 + 4 + ... + N = 2 ( 1 + 2 + 3 + ... + N/2) = 2 * N/2 * (N/2 + 1)/2 = N/2 * (N/2 +1) =  Theta(N^2)



查看完整回答
反對 回復 2023-07-28
  • 1 回答
  • 0 關注
  • 145 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號