Advertisement
starbeamrainbowlabs

Reverse Bubble Sort [Function Only]

Nov 1st, 2014
365
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 0.93 KB | None | 0 0
  1. static void DoBubbleSort(int[]arraytosort) {
  2.     int endingpoint = 1,
  3.         temp;
  4.     bool issorted;
  5.  
  6.     int swaps = 0,
  7.         iterations = 0,
  8.         passes = 0; //debug
  9.    
  10.     do {
  11.         issorted = true;
  12.  
  13.         for(int i = arraytosort.Length - 1; i >= endingpoint; i--)
  14.         {
  15.             iterations++; //debug
  16.             //Console.WriteLine("i: " + i + " i-1: " + (i - 1));
  17.            
  18.             if (arraytosort[i - 1] > arraytosort[i])
  19.             {
  20.                 swaps++; //debug
  21.                 //swap the numbers around
  22.                 temp = arraytosort[i - 1];
  23.                 arraytosort[i - 1] = arraytosort[i];
  24.                 arraytosort[i] = temp;
  25.  
  26.                 issorted = false;
  27.             }
  28.         }
  29.        
  30.         Console.Write("pass: " + passes + " ");
  31.         printarray(arraytosort); //debug
  32.         Console.WriteLine();
  33.        
  34.         passes++; //debug
  35.  
  36.         endingpoint++;
  37.     } while (!issorted);
  38.    
  39.     Console.WriteLine("Sorting Complete!");
  40.     Console.WriteLine("Statistics\n----------");
  41.     Console.WriteLine("Passes: " + passes + ", Iterations: " + iterations + " Swaps: " + swaps);
  42. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement