//bubbleSort.java /*--------------------------------------------------------------- ' class bubbleSort for package sort ' ' DESC: This sort runs at pow(n,2) passes and should only be used for CS examples ' or arrays that are very small (under 5000 elements). This example ' takes in int arrays. This can easily be changed to accept float, double or ' long arrays. Just change the paramenter variables from int to what you want. ' ' CONSTRUCTOR: NOT USED (YET) ' ' METHODS: ' ascendSort() -- takes array and then sorts it from ' min to max. ' descendSort() -- takes array and then sorts them from ' max to min. ' printArray() -- print the array out using System.out.print ' ' DATE MODIFIED: 10/15/2000 '---------------------------------------------------------------- */ package sort; public class bubbleSort { // constructor method -- not used yet public bubbleSort () { } //end of constructor method public static void ascendSort (int arrX[]) { int temp; // this is a placeholder; when switching contents // this must be the same datatype as arrX[] for (int pass = 0; pass < arrX.length - 1; ++pass) for (int i = 0; i < arrX.length - pass - 1; ++i) if (arrX[i] > arrX[i+1]) { temp = arrX[i]; arrX[i] = arrX[i+1]; arrX[i+1] = temp; } // end of if statement block } // end of ascendSort method public static void descendSort (int arrX[]) { int temp; // this is a placeholder; when switching contents // this must be the same datatype as arrX[] for (int pass = 0; pass < arrX.length - 1; ++pass) for (int i = 0; i < arrX.length - pass - 1; ++i) if (arrX[i] < arrX[i+1]) { temp = arrX[i]; arrX[i] = arrX[i+1]; arrX[i+1] = temp; } // end of if statement block } // end of decendSort method public static void printArray (int arrX[]) { System.out.print("\n"); for (int i = 0; i < arrX.length; ++i) { System.out.print(arrX[i]); if (i != arrX.length-1) System.out.print(","); else System.out.print("\n"); } //end of for statement } // end of printArray method } // end of class bubbleSort