/* * Generates n random strings and then sorts them using bubble sort. * Uses JavaScript types and functions wherever possible. * - Strings to be sorted are JavaScript Strings. * - random methos is a JavaScript function. * Execution Command: java -cp bsh-2.0b1.jar bsh.Interpreter bsort.bsh */ int num = 1000; // number of strings to sort int ssize = 20; // size of each string. char[] chars = new char[] { '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z', 'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z' }; void bsort(array){ int n = array.size(); for (int i = n - 1; i > 0; i--){ for (int j = 0; j < i; j++){ if (array.get(j).compareTo(array.get(j+1)) < 0){ t = array.get(j); array.set(j, array.get(j+1)); array.set(j+1, t); } } } } void printa(array){ for (int i = 0; i < array.size(); i++){ print("[" + i + "] " + array.get(i)); } } void populate(array, num){ ran = new java.util.Random(); for (int i = 0; i < num; i++){ String s = ""; for (var j = 0; j < ssize; j++){ r = ran.nextInt(chars.length); s += chars[r]; } array.add(s); } } array = new ArrayList(); // Do not specify the size on purpose print("Populating the array with " + num + " Strings, each of " + ssize + " characters ..."); st = (new Date()).getTime(); populate(array, num); et = (new Date()).getTime(); print(" ... done. Elapsed time: " + (et - st) + " millis."); //print("Original Array::"); printa(array); print("Sorting the array ..."); st = (new Date()).getTime(); bsort(array); //array.sort(); et = (new Date()).getTime(); print(" ... done. Elapsed time: " + (et - st) + " millis."); //print("Sorted Array::"); printa(array);