Advertisement
starbeamrainbowlabs

Reverse Bubble Sort

Nov 1st, 2014
659
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 2.00 KB | None | 0 0
  1. using System;
  2.  
  3. /*
  4.  * Bubble Sort
  5.  * By Starbeamrainbowlabs (https://starbeamrainbowlabs.com/)
  6.  *
  7.  * Remove lines with '//debug' on them for normal use.
  8.  */
  9.  
  10. public class BubbleSort
  11. {
  12.     public static void Main() {
  13.         int[]tosort = generatearray(10, 0, 100);
  14.         Console.Write("Original: ");
  15.         printarray(tosort);
  16.         Console.WriteLine();
  17.         DoBubbleSort(tosort);
  18.     }
  19.  
  20.     static void DoBubbleSort(int[]arraytosort) {
  21.         int endingpoint = 1,
  22.             temp;
  23.         bool issorted;
  24.  
  25.         int swaps = 0,
  26.             iterations = 0,
  27.             passes = 0; //debug
  28.        
  29.         do {
  30.             issorted = true;
  31.  
  32.             for(int i = arraytosort.Length - 1; i >= endingpoint; i--)
  33.             {
  34.                 iterations++; //debug
  35.                 //Console.WriteLine("i: " + i + " i-1: " + (i - 1));
  36.                
  37.                 if (arraytosort[i - 1] > arraytosort[i])
  38.                 {
  39.                     swaps++; //debug
  40.                     //swap the numbers around
  41.                     temp = arraytosort[i - 1];
  42.                     arraytosort[i - 1] = arraytosort[i];
  43.                     arraytosort[i] = temp;
  44.  
  45.                     issorted = false;
  46.                 }
  47.             }
  48.            
  49.             Console.Write("pass: " + passes + " ");
  50.             printarray(arraytosort); //debug
  51.             Console.WriteLine();
  52.            
  53.             passes++; //debug
  54.  
  55.             endingpoint++;
  56.         } while (!issorted);
  57.        
  58.         Console.WriteLine("Sorting Complete!");
  59.         Console.WriteLine("Statistics\n----------");
  60.         Console.WriteLine("Passes: " + passes + ", Iterations: " + iterations + " Swaps: " + swaps);
  61.     }
  62.  
  63.     static int[]generatearray(int size, int min = 0, int max = 25) {
  64.         if (min >= max)
  65.             throw new Exception("The min was bigger than max.");
  66.  
  67.         int[]newarray = new int[size];
  68.         Random generator = new Random();
  69.  
  70.         for (int i = size - 1; i >= 0; i--) {
  71.             newarray[i] = generator.Next(min, max);
  72.         }
  73.  
  74.         return newarray;
  75.     }
  76.  
  77.     static void printarray(int[]toprint) {
  78.         Console.Write("[");
  79.         for (int i = 0; i < toprint.Length; i++) {
  80.             if (toprint[i] < 10) {
  81.                 //the number has only one character
  82.                 Console.Write(" ");
  83.             }
  84.             Console.Write(toprint[i]);
  85.             if (i + 1 != toprint.Length) {
  86.                 Console.Write(", ");
  87.             }
  88.         }
  89.         Console.Write("] ");
  90.     }
  91. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement