tankdthedruid

bubbleSort.java

Jul 3rd, 2012
61
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.36 KB | None | 0 0
  1.  
  2. public class bubbleSort {
  3.  
  4.     /**
  5.      * @param args
  6.      */
  7.     public static void main(String a[])
  8.     {
  9.         int i; // Creating Variable 'i'
  10.         int array[] = {12,9,4,99,120,1,3,10}; // Create Array Named 'array' Containing 12,9,4,99,120,1,3, and 10
  11.         System.out.println("Values Before the sort:\n"); // Print Statement On A New Line
  12.         for(i = 0; i < array.length; i++) // For Loop where 'i' is equal to 0; Loop until 'i' equals length of 'array'; each loop add 1 to 'i'.
  13.             System.out.print( array[i]+"  "); // Print Number Stored at Index 'i' plus a space
  14.         System.out.println(); // Print Blank New Line
  15.         bubble_srt(array, array.length); // Run bubble_srt using the integer array named 'array' and the length of 'array'.  
  16.         System.out.print("Values after the sort:\n"); // Print Statement On A New Line
  17.         for(i = 0; i < array.length; i++) // For Loop where 'i' is equal to 0; Loop until 'i' equals length of 'array'; each loop add 1 to 'i'.
  18.             System.out.print(array[i]+"  "); // Print Number Stored at Index 'i' plus a space
  19.         System.out.println(); // Print Blank New Line
  20.         System.out.println("PAUSE"); // Print Statement On A New Line
  21.     }
  22.     public static void bubble_srt( int a[], int n )
  23.     {
  24.         int i, j,t=0;
  25.         for(i = 0; i < n; i++)
  26.         {
  27.             for(j = 1; j < (n-i); j++)
  28.             {
  29.                 if(a[j-1] > a[j])
  30.                 {
  31.                     t = a[j-1];
  32.                     a[j-1]=a[j];
  33.                     a[j]=t;
  34.                 }
  35.             }
  36.         }
  37.     }
  38. }
Advertisement
Add Comment
Please, Sign In to add comment