簡體   English   中英

如何為下面的新快速排序算法找到確切的時間復雜度和空間復雜度

[英]How to find exact Time complexity and Space Complexity for below new Quick sort algorithm

我創建了一種新的排序算法,該算法的基本概念是從給定列表中找到最小和最大元素,然后將它們與左角和右角元素交換,這將一直重復到我們到達中間元素為止。

與快速排序和合並排序相比,此算法執行的時間要短得多。 我想確定這種算法是否比快速排序更好。

我的算法代碼

public class VeryQuickVersion1 
{

    public static void main(String args[]) 
    {

        long current = System.nanoTime();

        int[] first = { 8 ,1 ,3 ,2, 6, 5, 7, 4, 12 ,9, 11 ,10 ,14 ,13, 15};

        for (int x=0,y=first.length - 1;x<y;x++,y--) 
        {
            int low = 0;
            int high = 0;
            int li = 0;
            int hi = 0;
            for (int i = x; i <= y; i++) 
            {
                if (i == x)
                {
                    low = first[i];
                    high = first[i];
                }
                if (low > first[i]) 
                {
                    low = first[i];
                    li = i;
                }
                if (high < first[i])
                {
                    high = first[i];
                    hi = i;
                }
            }

            first[li]=first[x];
            first[hi]=first[y];

            first[x]=low;
            first[y]=high;
        }
    /*  for(int i:first){
            System.out.println(i);
        }*/
        System.out.println(System.nanoTime() - current);
     }
}

該算法花費的時間是:10148,而快速排序算法花費的相同列表的時間是:17498

上述算法的時間復雜度似乎是O(n^2)

如您所見,有2個嵌套的for循環。 外部一個從x = 0, y = nx < y ,並且在每個步驟中它都會減少x++y-- 而另一個內部循環從xy

這可以看作是級數n + (n-2) + (n-4) + .... + 0 顯然可以得出時間復雜度O(n^2)


時間復雜度不是按照您的方式計算的。 您應該檢查當輸入大小增加時,此程序花費的時間將如何增加 並使用不同類型的輸入 (如升序,隨機等)進行測試。

收集了非常大的輸入和不同類型的輸入的數據后,您將看到O(nlogn)時間復雜度的算法與O(n^2)時間復雜度的算法之間的區別。


注意:您可以在網站上看到花費時間增加的真正區別。 注意輸入的長度增加到50000后所花費的時間如何增加。

您不會在數據量如此小的任何算法上設置基准。 數組的大小為10,這確實很小。

創建大小為〜10 ^ 5或10 ^ 6的數組,然后檢查性能。

另外,僅通過查看代碼,我就能知道該算法比快速排序更差。 在漸近復雜度中,快速排序是O(n log n),而這個顯然是O(n ^ 2)。

我使用Arrays.sort使用快速排序對數組進行排序。

結果如下:

數組大小1000

For Quick Sort algorithm:
1817634

For my algorithm:
8105038

數組大小100000

For Quick Sort algorithm:
21210010

For my algorithm:
7117304154

您可以清楚地看到區別。

我的代碼僅供參考:(對於我的算法,我只是復制了您的代碼)

import java.util.*;

public class Quick{

public static void main(String args[]) {


    Scanner in = new Scanner(System.in);
    int n = in.nextInt();

    int[] first = new int[n];
    for(int i = 0; i < n; i++){
        first[i] = in.nextInt();
    }

    int second[] = first.clone();

    long current = System.nanoTime();
    Arrays.sort(second);
    System.out.println("For Quick Sort algorithm:\n" + (System.nanoTime() - current) + "\n");

    current = System.nanoTime();

    for (int x=0,y=first.length - 1;x<y;x++,y--) {
        int low = 0;
        int high = 0;
        int li = 0;
        int hi = 0;
        for (int i = x; i <= y; i++) {
            if (i == x) {
                low = first[i];
                high = first[i];
            }
            if (low > first[i]) {
                low = first[i];
                li = i;
            }
            if (high < first[i]) {
                high = first[i];
                hi = i;
            }
        }

        first[li]=first[x];
        first[hi]=first[y];

        first[x]=low;
        first[y]=high;
    }
/*  for(int i:first){
        System.out.println(i);
    }*/
    System.out.println("For my algorithm:\n" + (System.nanoTime() - current));



 }
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM