Docsity

Programming Assignment 2 - Data Structures and Algorithms | COMPSCI 342, Assignments of Computer Science

Toggle sidebar
bg1
COMPSCI 342: Data Structures and Algorithms
Programming Assignment 2 (60 points + 20 extra credit points)
Due Date: see class website
1 Description
Implement radixsort in Java.
1. Fix the number of elements being sorted at 5000000. Now vary the number of bits that you
use for counting sort as follows.
1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16
Note that the size of the counting array will be k= 2bits. Observe the run-time for each value
of bits. Make a table for the run-time versus d, where dis the number of passes radixsort
makes. Remember that the number of passes made by radixsort is d=dMACHINE WORD SIZE/bitse.
For most machines the constant MACHINE WORD SIZE is 32, but your program should not
rely on this assumption. For example, in Java, we can get the word size from the constant
Integer.SIZE.
2. Now run your best radixsort implementation for the following input sizes:
500000,1000000,3000000,10000000,20000000,30000000
and make a table of input size versus run-time.
Notes:
•For the purposes of testing use random numbers generated using the java.util.Random()
class. Don’t restrict the range of random numbers being generated (so that all 32 bits contain
useful information). The argument seed is a random seed, which can be any non-negative
integer.
public void generate_random_array(int A[], int n, long seed)
{
Random generator = new Random(seed);
for (int i=0; i<n; i++)
A[i] = generator.nextInt(Integer.MAX_VALUE);
}
•The program should take three arguments, one for the number of bits to use in the counting
sort, the second for the size of the input and the last one for a random seed.
java RadixSort <bits> <input-size> <random seed>
pf3

Partial preview of the text

Download Programming Assignment 2 - Data Structures and Algorithms | COMPSCI 342 and more Assignments Computer Science in PDF only on Docsity!

COMPSCI 342: Data Structures and Algorithms

Programming Assignment 2 (60 points + 20 extra credit points)

Due Date: see class website

1 Description

Implement radixsort in Java.

  1. Fix the number of elements being sorted at 5000000. Now vary the number of bits that you use for counting sort as follows.

1 , 2 , 3 , 4 , 5 , 6 , 7 , 8 , 9 , 10 , 11 , 12 , 13 , 14 , 15 , 16

Note that the size of the counting array will be k = 2bits. Observe the run-time for each value of bits. Make a table for the run-time versus d, where d is the number of passes radixsort makes. Remember that the number of passes made by radixsort is d = dMACHINE WORD SIZE/bitse. For most machines the constant MACHINE WORD SIZE is 32, but your program should not rely on this assumption. For example, in Java, we can get the word size from the constant Integer.SIZE.

  1. Now run your best radixsort implementation for the following input sizes:

and make a table of input size versus run-time.

Notes:

  • For the purposes of testing use random numbers generated using the java.util.Random() class. Don’t restrict the range of random numbers being generated (so that all 32 bits contain useful information). The argument seed is a random seed, which can be any non-negative integer.

public void generate_random_array(int A[], int n, long seed) { Random generator = new Random(seed); for (int i=0; i<n; i++) A[i] = generator.nextInt(Integer.MAX_VALUE); }

  • The program should take three arguments, one for the number of bits to use in the counting sort, the second for the size of the input and the last one for a random seed. java RadixSort <input-size>
  • Make sure that you check the sorting by writing a simple check function with the following prototype.

// returns true if A[0..n-1] is sorted. // return false if A[0..n-1] is not sorted. boolean CheckIfSorted(int A[])

  • You should time only the radixsort function. For Java programs, see the example in ~amit/cs342/lab/tim for timing the program accurately. If you are developing on MS Windows, you can do simpler (but less accurate) timing by using System.currentTimeMillis() method as follows.

long totalTime = System.currentTimeMillis(); radixsort(...); totalTime = System.currentTimeMillis() - totalTime;

However, you will need to use the method report cpu time from the NativeTiming class for the final version that you submit.

  • Compare your run time with the run time for sorting with quick-sort (as used in Arrays.sort method).
  • You may find it simpler to test your program on inputs with only 0s and 1s before testing on 32-bit random numbers generated by java.util.Random() class.
  • The Java virtual machine has limited amount of memory. You will need to run it with larger memory for this program. Use the following command as an example of increasing the memory size for the Java Virtual Machine. java -Xms384m -Xmx384m RadixSort 12 20000000 123
  • You are expected to develop this program on your own. Finding an implementation on the Internet or another book or from another person would be considered as cheating.
  • Bit Fiddling. The following function extracts a k-bit field at position start in a integer. The operator >>> is the arithmetic shift right operator in Java (which fills in zeroes at the leading bits).

/*

  • @param x a positive number from which we extract a bit-field
  • @param start starting bit position (bits are numbered from 0)
  • @param n the size of the bit-field.
  • @return the integer value in bits [start..start+n-1] of x. */

private int getbits(int x, int start, int n) { return ((x >>> start) & ((1<<n) - 1)); }