Bubble Sort in C#

泄露秘密 提交于 2020-02-02 15:56:12

问题


I am studying algorithms and I have two bubble sort functions/methods and both of them provide similar result. Can please someone tell me something more about them, such as performance etc?

public void BubbleSort() {
        int temp;
        for (int outer = upper; outer >= 1; outer--) {
            for (int inner = 0; inner <= outer - 1; inner++) {
                if ((int)arr[inner] > arr[inner + 1]) {
                    temp = arr[inner];
                    arr[inner] = arr[inner + 1]; 
                    arr[inner + 1] = temp;
                }
            }
        }
    }

public void BubbleSor2() {
    int temp;
    for (int outer = 0; outer < upper - 1; outer++) {
        for (int inner = 1; inner <= upper; inner++) {
           if ((int)arr[outer] > arr[inner]) {
               temp = arr[inner];
               arr[inner] = arr[outer];
               arr[outer] = temp;
            }
        }
    }
}

回答1:


Try this version of Bubble Sort.

public void BubbleSort()
{
    int temp;
    for (int outer = 0; outer < upper - 1; outer++)
    {
        for (int inner = 0; inner < upper - outer; inner++)
        {
            if (arr[inner + 1] < arr[inner])
            {
                temp = arr[inner + 1];
                arr[inner + 1] = arr[inner];
                arr[inner] = temp;
            }
        }
    }
}



回答2:


They are reverse of each-other, however, the implementation is incorrect. Replace it with this one:

public void BubbleSort() {
        int temp;
        for (int outer = upper; outer >= 1; outer--) {
            for (int inner = outer - 1; inner >= 0; inner--) {
                if (arr[inner] < arr[outer]) {
                    temp = arr[inner];
                    arr[inner] = arr[outer]; 
                    arr[outer] = temp;
                }
            }
        }
    }

public void BubbleSor2() {
    int temp;
    for (int outer = 0; outer < upper - 1; outer++) {
        for (int inner = outer + 1; inner < upper; inner++) {
           if (arr[inner] > arr[outer]) {
               temp = arr[inner];
               arr[inner] = arr[outer];
               arr[outer] = temp;
            }
        }
    }
}

The complexity of both is quadratic (O(n^2))




回答3:


The first one have a n loop outside. Here n = upper - 1; And inside in every iteration it runs one less than the previous iteration. So it runs (n*(n-1))/2 = (n^2-n)/2 times. Which is O(n^2)complexity.

The second one is not a bubble sort although it's result will be the same as the first one. And it runs in O(n^2) complexity.

The first one in the first iteration puts the maximum value in arr in the upperth position. In the second iteration puts the second maximum value in arr in upper-1 th postion and the third iteration put the third maximum in the upper-2th position and so on. Each time the maximum value is put in the (last position of array - iteration number)th postion. Hence the bubble sort.

The second one does the same goal but with different apporach. In the first iteration puts the minimum value in arr in the 0th position. In the second iteration puts the second minimum value in arr in 1 th postion and the third iteration put the third maximum in the 2nd position and so on. Each time the minimum value is put in the iteration number-1th postion.



来源:https://stackoverflow.com/questions/23310345/bubble-sort-in-c-sharp

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!