15,743,299 members
See more:
Hi i work in visual studio 2015 c# console applications .

I try to make bubble sort to array unsorted but i cannot do that

i search for internet about that i understand idea .

it is make comparison between small and largest then swap items to make in order array

but i cannot understand this code .

so that what outer loop represent and inner loop represent ?

bold lines for outer loop and inner loop

C#
```/*
* C# Program to Perform Bubble Sort
*/
using System;
class bubblesort
{
static void Main(string[] args)
{
int[] a = { 30, 20, 50, 40, 10 };
int t;
Console.WriteLine("The Array is : ");
for (int i = 0; i < a.Length; i++)
{
Console.WriteLine(a[i]);
}
for (int j = 0; j <= a.Length - 2; j++) outer loop
{
for (int i = 0; i <= a.Length - 2; i++) inner loop
{
if (a[i] > a[i + 1])
{
t = a[i + 1];
a[i + 1] = a[i];
a[i] = t;
}
}
}
Console.WriteLine("The Sorted Array :");
foreach (int aray in a)
Console.Write(aray + " ");
}
}```

What I have tried:

i need more explain to this code
Posted
Updated 22-Dec-16 18:40pm

Solution 1

Hi Ahmed,

What do you mean by "i cannot do that"? This is because the code is fine, only that you are missing the comments indicator (//).
C#
```for (int j = 0; j <= a.Length - 2; j++) //outer loop
{
for (int i = 0; i <= a.Length - 2; i++) //inner loop
...........................
...........................```
The algorithm checks for every two consecutive numbers and swaps them if necessary (for sorting). After one complete sweep (and swapping of numbers as needed), it starts sweeping again the whole list (starting from the beginning) and swaps numbers as necessary. The first loop (outer) makes sure it traverses the entire array n times (n = number of elements in the array). The second loop (inner) makes sure it swaps numbers in each traversal.

There are several variants of bubble sort. Complexity of this one is O(n-squared) as it traverses 'n x n' times in total. So in terms of algorithmic complexity this one is not a very good one.

The best way to understand the sorting is running the application in Visual Studio IDE in debug mode.

1) Place a breakpoint on the for loop and start the application (F5).
2) Then open the `autos` window (click on `menu->Debug->Windows->Autos`)
3) Also open the `locals` window (click on `menu->Debug->Windows->Autos`)
4) These two windows will enable you to view the values as you debug line-by-line.
5) Now press F10 and the code will be executed line-by-line. You can see changes in variables in RED in the `autos` and `locals` windows. The array 'a' should be inside `locals` window.

Please Google more for a lot of articles on different variants of bubble sort.

v2

Solution 2

With Bubble sort, to sort 10 values, you need to sort 9 times 1 value. And to find the next biggest value you need to compare it against the other values.
Study the algorithm, the debugger will show you how the program works.
Bubble sort - Wikipedia[^]
Note that the program is poorly designed, the workload can be divided by 2.

You should learn to use the debugger as soon as possible. Rather than guessing what your code is doing, It is time to see your code executing and ensuring that it does what you expect.

Debugger - Wikipedia, the free encyclopedia[^]
Mastering Debugging in Visual Studio 2010 - A Beginner's Guide[^]

The debugger is here to show you what your code is doing and your task is to compare with what it should do.
There is no magic in the debugger, it don't find bugs, it just help you to. When the code don't do what is expected, you are close to a bug.

Top Experts
Last 24hrsThis month
 Graeme_Grant 90 OriginalGriff 60 merano99 40 Dave Kreskowiak 20 Richard MacCutchan 10
 OriginalGriff 1,698 Richard MacCutchan 1,328 merano99 609 Rick York 455 Dave Kreskowiak 296

CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900