Source of BubbleSort.java


  1: /* 
  2:  * Copyright (c) 1999-2002, Xiaoping Jia.  
  3:  * All Rights Reserved. 
  4:  */
  5: 
  6: /**
  7:  *  This program implements the bubble sort algorithm to sort an array of integers.
  8:  */
  9: public class BubbleSort {
 10: 
 11:   public static void main(String[]args) {
 12:     int a[] ={21, 9, 45, 17, 33, 72, 50, 12, 41, 39};
 13: 
 14:     for (int i = a.length; --i >= 0; ) {
 15:       for (int j = 0; j < i; j++) {
 16:         if (a[j] > a[j+1]) {
 17:           int temp = a[j];
 18:           a[j] = a[j + 1];
 19:           a[j + 1] = temp;
 20:         }
 21:       }
 22:     }
 23: 
 24:     // print the sorted array
 25:     for (int k = 0; k < a.length; k++) {
 26:       System.out.println("a[" + k + "]: " + a[k]);
 27:     }
 28:   }
 29: 
 30: }