# 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

### 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

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,