Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- public class bubbleSort {
- /**
- * @param args
- */
- public static void main(String a[])
- {
- int i; // Creating Variable 'i'
- int array[] = {12,9,4,99,120,1,3,10}; // Create Array Named 'array' Containing 12,9,4,99,120,1,3, and 10
- System.out.println("Values Before the sort:\n"); // Print Statement On A New Line
- 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'.
- System.out.print( array[i]+" "); // Print Number Stored at Index 'i' plus a space
- System.out.println(); // Print Blank New Line
- bubble_srt(array, array.length); // Run bubble_srt using the integer array named 'array' and the length of 'array'.
- System.out.print("Values after the sort:\n"); // Print Statement On A New Line
- 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'.
- System.out.print(array[i]+" "); // Print Number Stored at Index 'i' plus a space
- System.out.println(); // Print Blank New Line
- System.out.println("PAUSE"); // Print Statement On A New Line
- }
- public static void bubble_srt( int a[], int n )
- {
- int i, j,t=0;
- for(i = 0; i < n; i++)
- {
- for(j = 1; j < (n-i); j++)
- {
- if(a[j-1] > a[j])
- {
- t = a[j-1];
- a[j-1]=a[j];
- a[j]=t;
- }
- }
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment