What's the most elegant way to bubble-sort in C#?

后端 未结 10 1837
旧巷少年郎
旧巷少年郎 2020-12-06 03:33

Can this be cleaned up?

using System;  
class AscendingBubbleSort 
{     
    public static void Main()
    {
        int i = 0,j = 0,t = 0;
        int []c=         


        
相关标签:
10条回答
  • 2020-12-06 03:46
        public int[] BubbleSortInAesc(int[] input)
        {
            for (int i = input.Length; i > 0; i--)
            {
                for (int j = 0; j < i-1; j++)
                {
                    if (input[j] > input[j + 1])
                    {
                        //Swap the numbers
                        input[j] = input[j + 1]+input[j];
                        input[j + 1] = input[j] - input[j + 1];
                        input[j] = input[j] - input[j + 1];
                    }
                }
            }
            return input;
        }
    
    0 讨论(0)
  • 2020-12-06 03:47

    What you've pasted there isn't a bubble sort. It's a sort of "brute force" sort but it's not bubble sort. Here's an example of a generic bubble sort. It uses an arbitrary comparer, but lets you omit it in which case the default comparer is used for the relevant type. It will sort any (non-readonly) implementation of IList<T>, which includes arrays. Read the above link (to Wikipedia) to get more of an idea of how bubble sort is meant to work. Note how on each loop we go through from start to finish, but only compare each item with its neighbour. It's still an O(n2) sort algorithm, but in many cases it will be quicker than the version you've given.

    public void BubbleSort<T>(IList<T> list)
    {
        BubbleSort<T>(list, Comparer<T>.Default);
    }
    
    public void BubbleSort<T>(IList<T> list, IComparer<T> comparer)
    {
        bool stillGoing = true;
        while (stillGoing)
        {
            stillGoing = false;
            for (int i = 0; i < list.Count-1; i++)
            {
                T x = list[i];
                T y = list[i + 1];
                if (comparer.Compare(x, y) > 0)
                {
                    list[i] = y;
                    list[i + 1] = x;
                    stillGoing = true;
                }
            }
        }
    }
    
    0 讨论(0)
  • 2020-12-06 03:48

    I think your algorithm in ok, but I would put the sort functionality in a seperate class and method.

    0 讨论(0)
  • 2020-12-06 03:50
    int[] array = {4,5,7,1,8};           
    
    int n1, n2;
    bool stillgoing = true;
    
    while (stillgoing)
    {
        stillgoing = false;
        for (int i = 0; i < (array.Length-1); i++)
        {                  
            if (array[i] > array[i + 1])
            {
                n1 = array[i + 1];
                n2 = array[i];
    
                array[i] = n1;
                array[i + 1] = n2;
                stillgoing = true; 
            }
        }
    }
    for (int i = 0; i < array.Length; i++)
    {
        Console.WriteLine(array[i]);
    }
    

    Took some ideas from Jon skeet...

    0 讨论(0)
  • 2020-12-06 03:52

    Most people would not bother making a bubble sort elegant. In general, though, I find that doing this:

    for (int i = 0; i < items.Length; i++) {
        Item item = items[i];
        // do something with item
    }
    

    is both more elegant and more maintainable than doing this:

    Item item;
    int i;
    for (i = 0; i < items.Length; i++) {
        item = items[i];
        // do something with item
    }
    

    In other words, declare your variables within the smallest applicable scope. Otherwise you might find yourself doing something with i or item at some other point in the code and then using them again where you shouldn't be.

    0 讨论(0)
  • 2020-12-06 03:55
    • I would use a swap methed to swap the two array items. (details of how to write swap method left as homework!)

    • You should think about the case when the items are already in order

    • You should read up on Insertion sort for more marks :-)

    • Rather then reading the test data from the keyboard, see if you can learn how to use nUnit

    0 讨论(0)
提交回复
热议问题