jyoung12387

Bubble Sort

Mar 27th, 2020
119
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 0.83 KB | None | 0 0
  1. using System;
  2.                    
  3. public class Program
  4. {
  5.     public static void Main()
  6.     {
  7.         int[] num = new int[] {5, 9, 4, 12, 8};
  8.         int t;
  9.        
  10.         Console.WriteLine("Original Array:");
  11.         Console.WriteLine("");
  12.         foreach(int n in num)
  13.         {
  14.             Console.Write(n + " ");
  15.         }
  16.         Console.WriteLine("");
  17.         Console.WriteLine("");
  18.        
  19.         for(int i = 0; i< num.Length - 1; i++)
  20.         {
  21.             for(int j = 0; j<num.Length - 1; j++)
  22.             {
  23.                 if(num[j] > num[j+1])
  24.                 {
  25.                     t = num[j+1];
  26.                     num[j+1] = num[j];
  27.                     num[j] = t;
  28.                 }
  29.                 //Dispay steps inside of the bubble sort
  30.                 Console.WriteLine("Inside j loop: ");
  31.                 foreach(int n in num)
  32.                 {
  33.                     Console.Write(n + " ");
  34.                 }
  35.                
  36.             }
  37.         }
  38.        
  39.         Console.WriteLine("Sorted Array: ");
  40.         Console.WriteLine("");
  41.        
  42.         foreach(int n in num)
  43.         {
  44.             Console.Write(n + " ");
  45.         }
  46.        
  47.     }
  48. }
Advertisement
Add Comment
Please, Sign In to add comment