Answered step by step
Verified Expert Solution
Question
1 Approved Answer
JAVA programming assignment. Desired output is in TBI comment block. Also, EMPTY_SLOT = -1 (null). /* * TBI... Two modifications must be made to radix_sort().
JAVA programming assignment. Desired output is in TBI comment block. Also, EMPTY_SLOT = -1 (null).
/* * TBI... Two modifications must be made to radix_sort(). * * (0) Modify radix_sort() method so that its array elements are in * the domain: Integer.MIN_VALUE <= a[i] <= Integer.MAX_VALUE. * * (1) Modify the method so that the "buckets" are not array length * sized array-of-ints (i.e. memory hogs). You are free to * implement this as you please. The following is one option: * Each bucket, when used, becomes a list of IntNode objects. * * The output for this method should match the following: * * original: 201 -8 1024 23 301 98 -25 7 0 401 98 7 * sorted: -25 -8 0 7 7 23 98 98 201 301 401 1024 */ static void radix_sort() { int[] a = { 201, 1024, 23, 301, 98, 7, 0, 401, 98, 7, }; int[] s = new int[a.length]; int max = EMPTY_SLOT; for (int i = 0; i < a.length; i++) { s[i] = a[i]; if (s[i] > max) max = s[i]; } int[][] buckets = new int[10][a.length]; init_buckets(buckets); if (tracing) print(s); int div = 1; do { // fill in the buckets... for (int i = 0; i < s.length; i++) { int digit = (s[i] / div) % 10; for (int j = 0; j < buckets[i].length; j++) { if (buckets[digit][j] == EMPTY_SLOT) { buckets[digit][j] = s[i]; if (tracing) System.out.println("buckets[" + digit + "][" + j + "] = " + s[i]); break; } } } // copy buckets into sorted array and reset buckets... for (int i = 0, k = 0; i < buckets.length; i++) for (int j = 0; j < buckets[i].length; j++) if (buckets[i][j] != EMPTY_SLOT) { s[k++] = buckets[i][j]; buckets[i][j] = EMPTY_SLOT; } if (tracing) print(s); div *= 10; } while (div < max); System.out.print(UNSORTED_HEADING); print(a); System.out.print(SORTED_HEADING); print(s); } static void init_buckets(int[][] x) { for (int i = 0; i < x.length; i++) { for (int j = 0; j < x[i].length; j++) x[i][j] = EMPTY_SLOT; } } static void print(int[] x) { for (int i = 0; i < x.length; i++) System.out.print(x[i] + " "); System.out.println(); } }
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started