本文介绍了气泡在C#中排序的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

限时删除!!

我正在研究算法,并且我有两个冒泡排序函数/方法,它们两个都提供相似的结果。

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;
            }
        }
    }
}


推荐答案

尝试使用此版本的冒泡排序。

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;
            }
        }
    }
}

这篇关于气泡在C#中排序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

1403页,肝出来的..

09-06 11:48