K-Heapidx Sort in Java

Verified

Added on  2019/10/18

|2
|1031
|262
Practical Assignment
AI Summary
This practical assignment focuses on implementing a k-heapidx sort algorithm in Java. Students are tasked with combining heapsort and radix sort techniques to create a program that sorts a list of integers. The program first takes input values 'n' (number of values) and 'k' (values per heap), then reads 'n' integer values (1..n). It creates min-heaps of size 'k' from the input list and links them. A radix sort is then applied to these heaps, treating each data item as a digit. The algorithm iteratively removes the smallest item from the first heap, prints it, and replaces it with the smallest item from the next heap, continuing until all heaps are empty. Part A requires running the program on provided input files and printing the sorting process. Part B involves comparing the performance (execution time, swaps) for different (n, k) values using a random sequence, analyzing the efficiency of the algorithm.
Document Page
The goal of this assignment is to practice using heaps and sorting data, and to compare the efficiency
of a sorting method with varying parameters. You are to write a Java program to implement a k-headix
sort. This is a combination of heapsort and radix sort. Your program will manipulate a number of heaps
linked together in an order determined by an initial radix sort.
Your program is to first take as input 2 values: n indicates the number of values to be sorted, and k
indicates the number of values to store in each heap. You may assume that n is a multiple of k. The
program should then read a set of n integer values each of which is in the range 1..n.
The program should represent the k values in each heap as an array. Create a min-heap from the first k
values in the input list, a second min-heap from the next k values, etc. Note that because n is a multiple of
k, the last heap also contains exactly k values. You should use the most efficient method to create each of
these heaps. Join the heaps together in a linked list, so that the 1st heap points to the 2nd, etc.
Now apply a radix sort on the heaps, as follows:
Each individual data item in the heap should be considered a “digit” for the purposes of the radix sort.
Therefore a heap of k values can be considered to have k digits. There are n possible values for each
of these digits.
At the end of the radix sort, the first heap in the linked list will be the “lowest” heap, the second will
be the next “lowest”, etc.
Now repeatedly apply the following steps until the list of heaps is empty:
Remove the smallest item from the first heap, and print it. Replace this item with the smallest item
from the second heap and apply trickle-down as necessary.
Repeat this process, inserting the smallest item from the third heap into the second heap, …, the
smallest item from the last heap into the next-to-last heap.
When the last heap becomes empty, remove it from the linked list of heaps.
Part A (70 marks): Submit the results of running your program on the input in the following files,
available from the course home page: assn3input1.txt, assn3input2.txt
For each of the above, print the initial list of values, and then print the item removed and the remaining
data structure (i.e. “list of heaps”) as each item is removed, in a manner similar to the example shown on
the following page.
Part B (20 marks): For this part, you must first construct a random sequence of integer values, with all
values in the range 1..10000. This same random sequence must be used to compare the performance of
your program for all of the following runs:
(n = 10000, k = 10000), (n = 10000, k = 1000), (n = 10000, k = 100), (n = 10000, k = 10),
(n = 10000, k = 1).
You should provide the following information for each run: execution time, total swaps required by
trickle-down during initial construction of the heaps, and total swaps required as a result of a remove
operation. Note: you can check the current time in milliseconds using System.currentTimeMillis().
Alternatively, you can investigate System.nanoTime(). Submit the results of this comparison.
For this part, the sorting output should be given electronically only, and should consist of only n, k, the
initial list of items and the sorted list of items.
tabler-icon-diamond-filled.svg

Paraphrase This Document

Need a fresh take? Get an instant paraphrase of this document with our AI Paraphraser
Document Page
Example: n = 12, k = 4
Initial set of values: 2,10,3,10,2,3,4,2,11,1,4,10
Initial set of arrays:
2 10 3 10 2 3 4 2 11 1 4 10
After converting the above to heaps:
2 10 3 10 2 2 4 3 1 10 4 11
After 1st step of radix sort:
2 2 4 3 2 10 3 10 1 10 4 11
After 2nd step of radix sort:
2 10 3 10 2 2 4 3 1 10 4 11
After 3rd step of radix sort:
2 2 4 3 2 10 3 10 1 10 4 11
After 4th step of radix sort:
1 10 4 11 2 2 4 3 2 10 3 10
After removing smallest item from 1st heap: Output: 1
2 10 4 11 2 2 4 3 3 10 10
After 2nd remove: Output: 2
2 10 4 11 2 3 4 3 10 10
After 3rd remove: Output: 2
2 10 4 11 3 3 4 10 10
After 4th remove: Output: 2
3 10 4 11 3 10 4 10
After 5th remove: Output: 3
3 10 4 11 4 10 10
After 6th remove: Output: 3
4 10 4 11 10 10
After 7th remove: Output: 4
4 10 10 11 10
After 8th remove: Output: 4
10 10 10 11
After 9th remove: Output: 10
10 10 11
After 10th remove: Output: 10
10 11
After 11th remove: Output: 10
11
12th remove: Output: 11
Note: The marker has the freedom to execute your program on any sequence of inputs. Therefore
you should make your program is thoroughly tested and handles bad input nicely.
chevron_up_icon
1 out of 2
circle_padding
hide_on_mobile
zoom_out_icon
[object Object]