none
Sort int-array with Bubble Sort C#

    Question

  • Hello, I would like to know of there is any easy way of sorting int-array with bubble sort in c#. I have started of with, just need som guidelines:

    static int[] RandomIntArray(int size)
            {
                Random rnd = new Random();
                int[] arr = new int[size];
                for (int i = 0; i < size; i++)
                    arr[i] = rnd.Next(100);

                return arr;
            }

    Regards S

    Saturday, April 07, 2012 12:39 PM

Answers

All replies

  • Something from here might help.

    Since arrays already provide a Sort method I take it that this is probably 'homework' so do not provide code.

    If you attempt the code and get stuck then post it with the error and I'll gladly help.


    Regards David R
    ---------------------------------------------------------------
    The great thing about Object Oriented code is that it can make small, simple problems look like large, complex ones.
    Object-oriented programming offers a sustainable way to write spaghetti code. - Paul Graham.
    Every program eventually becomes rococo, and then rubble. - Alan Perlis
    The only valid measurement of code quality: WTFs/minute.

    Saturday, April 07, 2012 1:29 PM
  • Below are the best sites which give you understanding of different sorting algorithms (including bubble sort). Check them out. But note that, the algorithms are not specific to C#. They are just pseudo codes. But they are effective in learning the techniques.

    http://www.sorting-algorithms.com/

    http://en.wikipedia.org/wiki/Sorting_algorithm

    http://epaperpress.com/sortsearch/download/sortsearch.pdf

    I hope these help you.


    Please mark this post as answer if it solved your problem. Happy Programming!

    Saturday, April 07, 2012 2:36 PM
  • Simple? No algoritm isnnt simple, here is an example of sorting code:

    static void Main(string[] args)
            {
                int size = 10;
                Random rnd = new Random();
                int[] arr = new int[size];
                for (int i = 0; i < size; i++)
                    arr[i] = rnd.Next(100);
    
                // sort our array
                bubble_sort_generic(arr);
            }
    
            static void bubble_sort_generic<T>(T[] array) where T : IComparable
            {
                long right_border = array.Length - 1;
                long last_exchange = 0;
                do
                {
                    last_exchange = 0;
                    for (long i = 0; i < right_border; i++)
                    {
                        if (array[i].CompareTo(array[i + 1]) > 0)
                        {
                            T temp = array[i];
                            array[i] = array[i + 1];
                            array[i + 1] = temp;
    
                            last_exchange = i;
                        }
                    }
                    right_border = last_exchange;
                }
                while (right_border > 0);
            }


    Mitja

    Saturday, April 07, 2012 2:51 PM
  • Hello,

    Please refer this link for bubble sort in c#

    http://en.literateprograms.org/Bubble_sort_(C_Sharp)

    http://stackoverflow.com/questions/1595244/whats-the-most-elegant-way-to-bubble-sort-in-c

    _Regards_


    Tarun singh Disclaimer: This posting is provided "AS IS" with no warranties or guarantees , and confers no rights

    • Marked as answer by Leee00 Sunday, April 08, 2012 10:44 AM
    Saturday, April 07, 2012 5:56 PM