计算每个方法在完全排序数组之前必须执行的检查次数

时间:2016-02-20 01:26:32

标签: java

所以我正在编写一个比较Bubble,Selection,Merge和Quick Sort的程序。所有4个方法都给出了一个1000个元素的随机数组,我计算一下方法执行完全排序数组需要多少次。我的问题围绕着我的计数。我知道我应该看看每个人的Big O复杂性,以便对数字进行估算,但我只想问我是否把计数放在正确的位置。

冒泡排序:

public static int[] bubbleSort(int[] a)
{
    boolean done = false;
    int n = a.length;
    while(done == false)//runs n times
    {
        done = true;
        for(int i = 0; i < n-1; i++)//runs n times
        {
            if(a[i] > a[i+1])//Swap
            {
                bubbleCount++;
                int temp = a[i];
                a[i] = a[i+1];
                a[i+1] = temp;
                done = false;
            }

        }
    }
    return a;
}

选择排序:

public static void selectionSort(int[] a )
{
    for (int i = 0; i < a.length - 1; i++) 
    {
        int pos = i;
        for (int j = i + 1; j < a.length; j++) 
        {
            // increments selection count
            selectionCount++;
            // if a[j] is less than a[pos], set pos to j
            if (a[j] < a[pos]) 
            {
                pos = j;
            }   
        }
        // swaps a[i] and a[pos]
        int temp = a[i];
        a[i] = a[pos];
        a[pos] = temp;
    }
}

合并排序:

public static void mergeSort(int [] a)
{
    //put counter for checks inside method
    int size = a.length;
    if(size < 2)//Halt recursion
    {
        return;
    }
    int mid = size/ 2;
    int leftSize = mid;
    int rightSize = size - mid;
    int[] left = new int[leftSize];
    int[] right = new int[rightSize];
    //populate left
    for(int i = 0; i < mid; i++)
    {
        mergeCount++;
        left[i] = a[i];
    }
    //populate right
    for(int i = mid; i < size; i++)
    {
        mergeCount++;
        right[i-mid] = a[i];
    }
    mergeSort(left);
    mergeSort(right);
    //merge
    merge(left, right, a);
}

public static void merge(int[] left, int[] right, int[] a)
{
    int leftSize = left.length;
    int rightSize = right.length;
    int i = 0;//index for left
    int j = 0;//index for right
    int k = 0;//index for a
    while(i < leftSize && j < rightSize)//compares until the end is reach in either 
    {
        if(left[i] <= right[j])
        {
            //assigns a[k] to left[i] and increments both i and k
            a[k] = left[i];
            i++;
            k++;
        }
        else
        {
            //assigns a[k] to right [j] and increments j  and k
            a[k] = right[j];
            j++;
            k++;
        }
    }
    //fills in the rest
    while(i<leftSize)
    {
        a[k] = left[i];
        i++;
        k++;
    }
    while(j<rightSize)
    {
        a[k] = right[j];
        j++;
        k++;
    }
}

快速排序

public static void quickSort(int[] a, int left, int right)
{
    int index = partition(a, left, right);
    if(left < index - 1)
    {
        //increments quickCount and calls quickSort recursively
        quickCount++;
        quickSort(a, left, index-1);    
    }
    if(index < right)
    {
        //increments quickCount and calls quicSort recursively
        quickCount++;
        quickSort(a, index, right);
    }
}
public static int partition(int[] a, int left, int right)
{
    int i = left;
    int j = right;
    int pivot = a[((left+right)/2)];
    while(i<=j)
    {
        while(a[i] < pivot)
        {
            i++;//correct position so move forward
        }
        while(a[j] > pivot)
        {
            j--;//correct position 
        }
        if(i <= j)
        {
            //swaps and increments i and decrements j
            int temp = a[i];
            a[i] = a[j];
            a[j] = temp;
            i++;
            j--;
        }
    }
    return i;
}

2 个答案:

答案 0 :(得分:0)

您的计数应该是您对两个值进行比较的地方。

bubbleCount应该向上移动到if之上,因为你想要计算每个比较,不确定if子句为真的那些。 selectionCount似乎没问题。 mergeCount应该移动到比较值的位置。

答案 1 :(得分:0)

你需要计算比较,所以我会创建一个比较和计数的方法:

while(i<=j)

并始终使用该方法来比较值:

而不是:

while(!isGreaterThan(i, j))

做的:

data.table