ahmed salah

ahmed salah

  • NA
  • 530
  • 148.7k

what outer loop and inner loop represent in bubble sort

Dec 22 2016 6:13 PM
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 ? 
 
red lines for outer loop and inner loop 
  1. /* 
  2.  * C# Program to Perform Bubble Sort 
  3.  */  
  4. using System;  
  5. class bubblesort  
  6. {  
  7.         static void Main(string[] args)  
  8.         {  
  9.             int[] a = { 30, 20, 50, 40, 10 };    
  10.             int t;  
  11.             Console.WriteLine("The Array is : ");  
  12.             for (int i = 0; i < a.Length; i++)  
  13.             {  
  14.                 Console.WriteLine(a[i]);  
  15.             }  
  16.             for (int j = 0; j <= a.Length - 2; j++) outer loop  
  17.             {  
  18.                 for (int i = 0; i <= a.Length - 2; i++) inner loop  
  19.                 {  
  20.                     if (a[i] > a[i + 1])  
  21.                     {  
  22.                         t = a[i + 1];  
  23.                         a[i + 1] = a[i];  
  24.                         a[i] = t;  
  25.                     }  
  26.                 }  
  27.              
  28.             Console.WriteLine("The Sorted Array :");  
  29.             foreach (int aray in a)                           
  30.                 Console.Write(aray + " ");  
  31.             Console.ReadLine();  
  32.         }  
  33.     }  
 

Answers (1)