CIS 2168 / 2: Lab-Assignment #7: Sorting an Array Using a Heap
THIS IS A 1 WEEK ASSIGNMENT !
Simple task: use heapsort to sort an integer array. And since that task sounds dry, i'll provide a nice visualization.
I created the class 'ColorBox', which, if you create an instance of it, shows up like the following image:
The ColorBox object visualizes an array of integer numbers (all values between [0,255]). As you can see, they are currently not sorted
(the array of numbers is a 1 dimensional array, i'll explain in class in what order i convert it to the 2D image. It's not just row by row).
You must read the values of the array, sort them (heap sort), write them back. The ColorBox will display itself like this:
Wonderful. You may use the heap sort program we talked about in class. However, you have to change it a little, since at the end of the
sorting you should print the number of comparisons that were needed to build the heap and to destroy the heap (2 numbers).
Your task in steps:
- Create an instance of ColorBox. The constructor takes the size of the box, set it to 600 first: ColorBox cb=new ColorBox(600);
(600 means you'll have an array of 600*600 = 360000 values!)
- you get the integer array using the method ColorBox.getColorValues()
- sort the array (THAT's YOUR MAIN TASK)
- write it back to the ColorBox, using ColorBox.setColorValues(int []c)
- print the numbers of steps you needed to build the heap, and to destroy the heap.
- for yourself: try different sizes (600,700,800) and see if the heap really performs in O(n log n) (n=size*size!)
Bonus points:
- Get 2 bonus points for your own implementation of a heap. It MUST be an array implementation.
- Get 2 bonus points for a graphic extension of the task: create a second window with two buttons, 'random' and 'sort', which should
activate random init and sorting. use ColorBox.initRandom() for random init.
Here is the JAVA code for ColorBox