Podcast Ch15b - Appalachian State University · Radix Sort • The radix sort is a linear algorithm...

15
Podcast Ch15b Title: Radix Sort Description: Overview of radix sort; Radix Sort method; program 15.2 Participants: Barry Kurtz (instructor); John Helfert and Tobie Williams (students) Textbook: Data Structures for Java; William H. Ford and William R. Topp

Transcript of Podcast Ch15b - Appalachian State University · Radix Sort • The radix sort is a linear algorithm...

Page 1: Podcast Ch15b - Appalachian State University · Radix Sort • The radix sort is a linear algorithm that sorts an array of integers. It uses successive digits in the numbers to partially

Podcast Ch15b• Title: Radix Sort• Description: Overview of radix sort;

Radix Sort method; program 15.2• Participants: Barry Kurtz (instructor);

John Helfert and Tobie Williams (students)

• Textbook: Data Structures for Java; William H. Ford and William R. Topp

Page 2: Podcast Ch15b - Appalachian State University · Radix Sort • The radix sort is a linear algorithm that sorts an array of integers. It uses successive digits in the numbers to partially

Radix Sort• The radix sort is a linear algorithm that

sorts an array of integers. It uses successive digits in the numbers to partially sort the list.

• Successive elements in the array are passed into an array of ten queues (index 0 to 9) using a digit as the index. Elements are copied from the array back to the array, creating a partially sorted list.

Page 3: Podcast Ch15b - Appalachian State University · Radix Sort • The radix sort is a linear algorithm that sorts an array of integers. It uses successive digits in the numbers to partially

Radix Sort Example• List: [91, 6, 85, 15, 92, 35, 30, 22, 39]

• After Pass 0: [30, 91, 92, 22, 85, 15, 35, 6, 39]

• After Pass 2: [6, 15, 22, 30, 35, 39, 85, 91, 92]

Page 4: Podcast Ch15b - Appalachian State University · Radix Sort • The radix sort is a linear algorithm that sorts an array of integers. It uses successive digits in the numbers to partially

Radix Sort (continued)• Any two numbers with different ten's digits,

such as 35 and 92 will be in the proper relative order since the algorithm collects numbers from the 3-bin before numbers from the 9-bin.

• Pass 0 assures the ordering of numbers with the same tens digit. Such numbers will appear in the tens queue in order of their ones digit.

Page 5: Podcast Ch15b - Appalachian State University · Radix Sort • The radix sort is a linear algorithm that sorts an array of integers. It uses successive digits in the numbers to partially

Radix Sort (continued)Initial sequence: {91, 6, 85, 15, 92, 35, 30, 22, 39}

Same tens digit. In order by ones digit

Page 6: Podcast Ch15b - Appalachian State University · Radix Sort • The radix sort is a linear algorithm that sorts an array of integers. It uses successive digits in the numbers to partially

Practice Problem{321, 456, 123, 876, 348, 874, 923, 756, 693, 329, 234}

Page 7: Podcast Ch15b - Appalachian State University · Radix Sort • The radix sort is a linear algorithm that sorts an array of integers. It uses successive digits in the numbers to partially

Radix Sort Method• The radixSort() method uses the expanded

base-10 representation of a number. value = xd-110d-1 + xd-210d-2 + ... + x2102 + x1101 + x0100

• The implementation uses 10 queues// allocate 10 null references to a LinkedQueueLinkedQueue[] digitQueue = new LinkedQueue[10];// initialize each element of digitQueue to be

// an empty queuefor (i=0;i < digitQueue.length;i++)

digitQueue[i] = new LinkedQueue();

Page 8: Podcast Ch15b - Appalachian State University · Radix Sort • The radix sort is a linear algorithm that sorts an array of integers. It uses successive digits in the numbers to partially

Radix Sort Method (continued)• The radixSort() method must execute d

iterations, one for each digit. The i-thiteration distributes the numbers into the array of queues, digitQueue, by using the digit corresponding to the power 10i. To determine the value of the digit at any position i, divide a number by the power = 10i and take the remainder after division by 10.digitQueue[(arr[i] / power) % 10].

push(new Integer(arr[i]));

Page 9: Podcast Ch15b - Appalachian State University · Radix Sort • The radix sort is a linear algorithm that sorts an array of integers. It uses successive digits in the numbers to partially

Radix Sort distribute()// support method for radixSort()// distribute array elements into one of 10 queues// using the digit corresponding to power// power = 1 ==> 1's digit// power = 10 ==> 10's digit// power = 100 ==> 100's digit// ...private static void distribute(int[] arr,LinkedQueue[] digitQueue, int power){

int i;

// loop through the array, inserting each// element into the queue (arr[i] / power) % 10for (i = 0; i < arr.length; i++)

digitQueue[(arr[i] / power) % 10].push(arr[i]);}

Page 10: Podcast Ch15b - Appalachian State University · Radix Sort • The radix sort is a linear algorithm that sorts an array of integers. It uses successive digits in the numbers to partially

Radix Sort collect()// support method for radixSort()// gather elements from the queues and copy// back to the arrayprivate static void collect(LinkedQueue[] digitQueue, int[] arr) {

int i = 0, digit;

// scan the array of queues using// indices 0, 1, 2, etc.for (digit = 0; digit < 10; digit++)

// collect items until queue empty// and copy items back to the arraywhile (!digitQueue[digit].isEmpty()) {

arr[i] = digitQueue[digit].pop());i++;

}}

Page 11: Podcast Ch15b - Appalachian State University · Radix Sort • The radix sort is a linear algorithm that sorts an array of integers. It uses successive digits in the numbers to partially

radixSort() Methodpublic static void radixSort(int[] arr, int d) {

int i;// current digit found by dividing by 10^powerint power = 1;// allocate 10 null references to a LinkedQueueLinkedQueue[] digitQueue = new LinkedQueue[10];// initialize each element of digitQueue to be// an empty queuefor (i=0; i < digitQueue.length; i++)

digitQueue[i] = new LinkedQueue();for (i=0; i < d; i++){

distribute(arr, digitQueue, power);collect(digitQueue, arr);power *= 10;

}}

Page 12: Podcast Ch15b - Appalachian State University · Radix Sort • The radix sort is a linear algorithm that sorts an array of integers. It uses successive digits in the numbers to partially

Program 15.2 (Run)

RUN

2554 3097 5231 6876 8539 1244616483 20040 23202 24353 24758 2599628922 29730 30672 32032 32198 3226136705 36867 47340 47688 51547 5361754797 55577 56055 59553 61588 6528965465 68416 68935 71586 73017 7711980185 80659 81371 83443 87678 8813890076 90717 93637 94948 95470 9698497332 98616

• The program illustrates radixSort() for an array of 50 integers in the range 0-99,999.

Page 13: Podcast Ch15b - Appalachian State University · Radix Sort • The radix sort is a linear algorithm that sorts an array of integers. It uses successive digits in the numbers to partially

Program 15.2 (continued)import java.util.Random;import ds.util.Arrays;public class Program15_2 {

public static void main(String[] args) {// array to hold the data that is sortedint[] arr = new int[50];Random rnd = new Random();int i;// initialize array with 50 random numbers in// range 0 - 99999for (i = 0; i < 50; i++)

arr[i] = rnd.nextInt(100000);// apply the radix sort and output// the sorted arrayArrays.radixSort(arr, 5);displayArray(arr);

}

Page 14: Podcast Ch15b - Appalachian State University · Radix Sort • The radix sort is a linear algorithm that sorts an array of integers. It uses successive digits in the numbers to partially

Program 15.2 (continued)

private static void displayArray(int[] arr){

int i, j, strnLength;String s, strn;for (i=0; i < arr.length; i++) {

// represent value of arr[i] as a stringstrn = String.valueOf(arr[i]);// capture the length of strnstrnLength = strn.length();s = "";// justify strn in a field// of 8 print positionsfor (j=0; j < 8-strnLength; j++)

s += " ";s += strn;

Page 15: Podcast Ch15b - Appalachian State University · Radix Sort • The radix sort is a linear algorithm that sorts an array of integers. It uses successive digits in the numbers to partially

Program 15.2 (concluded)

// output the justified integer valueSystem.out.print(s);// newline every 6 numbersif ((i+1) % 6 == 0)

System.out.println();}System.out.println();

}}

• Runtime efficiency of radixSort() is O(dn) where the array has n elements and the maximum integer has d digits.