Pure Programmer
Blue Matrix


Cluster Map

Project: Bubble Sort

The [[Bubble sort]] is perhaps the easiest sort to implement. Write a function that takes a list of integers and sorts the list, in place, using a bubble sort. Test the function by creating a random list of integers, print the random list, sort the list, then print out the sorted list. Accept the number of integers to put in the list and the maximum integer to randomly generate on the command line. What is the worst case performance of the Bubble sort?

Output
$ javac -Xlint BubbleSort.java $ java -ea BubbleSort 10 10 Random List [0, 9, 6, 8, 6, 7, 7, 6, 4, 8] Sorted List [0, 4, 6, 6, 6, 7, 7, 8, 8, 9] $ javac -Xlint BubbleSort.java $ java -ea BubbleSort 20 100 Random List [67, 45, 98, 44, 33, 10, 34, 23, 49, 9, 1, 66, 90, 91, 40, 1, 3, 23, 98, 98] Sorted List [1, 1, 3, 9, 10, 23, 23, 33, 34, 40, 44, 45, 49, 66, 67, 90, 91, 98, 98, 98]

Solution