# explanation of array and bubble sort

### Question

• please have a look at this site:

C:\Documents and Settings\Doorer Sathi\Desktop\Bubble sort algorithm with example in C#.htm

or

http://www.developerin.net/a/55-Algorithm-in-CSharp/37-Bubble-Sort

my question is :-

q1]  how the program works?

q2]   and  how does the array  gets initialised

for those who would like to see the program here itself

```public void BubbleSort(int[] intArray)
{
Console.WriteLine("==========UnSorted Array Input===============");
for (int i = 0; i < intArray.Length; i++)
{
Console.WriteLine(intArray[i]);
}

for (int i = intArray.Length - 1; i > 0; i--)
{
for (int j = 0; j <= i - 1; j++)
{
if (intArray[j] > intArray[j + 1])
{
int highValue = intArray[j];

intArray[j] = intArray[j + 1];
intArray[j + 1] = highValue;
}
}
}

Console.WriteLine("==========Sorted Array Using BubbleSort===============");
for (int i = 0; i < intArray.Length; i++)
{
Console.WriteLine(intArray[i]);
}
}```

Thursday, December 05, 2013 7:00 AM

• There is no indication on where they defined the array. However I guess something like this will get you same result:

```//Using Array Initialisers from memory
int[] array = new int[]() { 100, 15, 11, 12, 10, -1, 0 };```

Alternatively you could make a array with Random Numbers each execution using the Random Class.

The basic idea behind bubble sort is that after each pass at least one more number will be at the right position. This number is excluded from further comparsion - the previous pass has made certain that it is where it should be and no other number is bigger then it (otherwise the bigger one would have landed here during that pass).

Using the example array after one pass it will look like:

15, 11, 12, 10, -1, 0, 100.
The 100 is now where it should be and no further pass will compare 100 to anything (that is what the 'i' index is for).

• Marked as answer by Friday, December 06, 2013 7:07 AM
Thursday, December 05, 2013 12:36 PM

### All replies

• find the link showing bubble sort algo with dry run

http://www.trinity.nottingham.sch.uk/computing/csharp/basics/bubbleSort.aspx

Please mark as answer, if you find this post helpful. Thanks Deepak Kalra

• Proposed as answer by Thursday, December 05, 2013 7:42 AM
Thursday, December 05, 2013 7:42 AM
• There is no indication on where they defined the array. However I guess something like this will get you same result:

```//Using Array Initialisers from memory
int[] array = new int[]() { 100, 15, 11, 12, 10, -1, 0 };```

Alternatively you could make a array with Random Numbers each execution using the Random Class.

The basic idea behind bubble sort is that after each pass at least one more number will be at the right position. This number is excluded from further comparsion - the previous pass has made certain that it is where it should be and no other number is bigger then it (otherwise the bigger one would have landed here during that pass).

Using the example array after one pass it will look like:

15, 11, 12, 10, -1, 0, 100.
The 100 is now where it should be and no further pass will compare 100 to anything (that is what the 'i' index is for).