Question

In: Computer Science

Consider the following list of names: PEVAC, MARKOV, ZLATAREVA, ABDOLLAHZADEH, CHEN, WILLIAMS, ALBAYRAM, KURKOVSKY, ZABIHIMAVAN. Use...

Consider the following list of names: PEVAC, MARKOV, ZLATAREVA, ABDOLLAHZADEH, CHEN, WILLIAMS, ALBAYRAM, KURKOVSKY, ZABIHIMAVAN. Use Radix sort to sort them in alphabetic order. How many passes would be made through the outer while loop? Trace the contents of the list after each of these passes.

In JAVA!

Solutions

Expert Solution

import java.util.ArrayList;
import java.util.List;
import java.util.Arrays;
import java.util.Random;

public class RadixSort
{
    /*
     * Radix sort an array of Strings
     * Assume all are all ASCII
     * Assume all have same length
     */
    @SuppressWarnings("unchecked")        
    public static void radixSortA( String [ ] arr, int stringLen )
    {
        final int BUCKETS = 256;

        ArrayList<String> [ ] buckets = new ArrayList[ BUCKETS ];
        
        for( int i = 0; i < BUCKETS; i++ )
            buckets[ i ] = new ArrayList<>( );
        
        for( int pos = stringLen - 1; pos >= 0; pos-- )
        {
            for( String s : arr )
                buckets[ s.charAt( pos ) ].add( s );
            
            int idx = 0;
            for( ArrayList<String> thisBucket : buckets )
            {
                for( String s : thisBucket )
                    arr[ idx++ ] = s;
                
                thisBucket.clear( );
            }
        }
    }
       
    /*
     * Counting radix sort an array of Strings
     * Assume all are all ASCII
     * Assume all have same length
     */
    public static void countingRadixSort( String [ ] arr, int stringLen )
    {
        final int BUCKETS = 256;
        
        int N = arr.length;
        String [ ] buffer = new String[ N ];

        String [ ] in = arr;
        String [ ] out = buffer;
        
        for( int pos = stringLen - 1; pos >= 0; pos-- )
        {
            int[ ] count = new int [ BUCKETS + 1 ];
            
            for( int i = 0; i < N; i++ )
                count[ in[ i ].charAt( pos ) + 1 ]++;

            for( int b = 1; b <= BUCKETS; b++ )
                count[ b ] += count[ b - 1 ];

            for( int i = 0; i < N; i++ )
                out[ count[ in[ i ].charAt( pos ) ]++ ] = in[ i ];
            
              // swap in and out roles
            String [ ] tmp = in;
            in = out;
            out = tmp;
        }
        
           // if odd number of passes, in is buffer, out is arr; so copy back
        if( stringLen % 2 == 1 )
            for( int i = 0; i < arr.length; i++ )
                out[ i ] = in[ i ];
    }
    
    /*
     * Radix sort an array of Strings
     * Assume all are all ASCII
     * Assume all have length bounded by maxLen
     */
    @SuppressWarnings("unchecked")
    public static void radixSort( String [ ] arr, int maxLen )
    {
        final int BUCKETS = 256;

        ArrayList<String> [ ] wordsByLength = new ArrayList[ maxLen + 1 ];
        ArrayList<String> [ ] buckets = new ArrayList[ BUCKETS ];
        
        for( int i = 0; i < wordsByLength.length; i++ )
            wordsByLength[ i ] = new ArrayList<>( );
        
        for( int i = 0; i < BUCKETS; i++ )
            buckets[ i ] = new ArrayList<>( );
        
        for( String s : arr )
            wordsByLength[ s.length( ) ].add( s );
       
        int idx = 0;
        for( ArrayList<String> wordList : wordsByLength )
            for( String s : wordList )
                arr[ idx++ ] = s;
        
        int startingIndex = arr.length;    
        for( int pos = maxLen - 1; pos >= 0; pos-- )
        {
            startingIndex -= wordsByLength[ pos + 1 ].size( );
            
            for( int i = startingIndex; i < arr.length; i++ )
                buckets[ arr[ i ].charAt( pos ) ].add( arr[ i ] );
            
            idx = startingIndex;
            for( ArrayList<String> thisBucket : buckets )
            {
                for( String s : thisBucket )
                    arr[ idx++ ] = s;
                
                thisBucket.clear( );
            }
        }
    }

    public static void main( String [ ] args )
    {
        List<String> lst = new ArrayList<>( );
        Random r = new Random( );

        final int LEN = 5;
        
        for( int i = 0; i < 100000; i++ )
        {
            String str = "";
            int len = LEN; // 3 + r.nextInt( 7 ); // between 3 and 9 characters

            for( int j = 0; j < len; j++ )
                str += (char) ( 'a' + r.nextInt( 26 ) );

            lst.add( str );
        }

        String [ ] arr1 = new String[ lst.size( ) ];
        String [ ] arr2 = new String[ lst.size( ) ];

        lst.toArray( arr1 );
        lst.toArray( arr2 );

        long start, end;

        start = System.currentTimeMillis( );
        Arrays.sort( arr1 );
        end = System.currentTimeMillis( );
        System.out.println( "Elapsed: " + ( end - start ) );


        start = System.currentTimeMillis( );
        countingRadixSort( arr2, LEN );
        end = System.currentTimeMillis( );
        System.out.println( "Elapsed: " + ( end - start ) );

        for( int i = 0; i < arr1.length; i++ )
            if( !arr1[ i ].equals( arr2[ i ]  ) )
                System.out.println( "OOPS!!" );
    }
    
}

Related Solutions

Consider the following list of names: PEVAC, MARKOV, ZLATAREVA, ABDOLLAHZADEH, CHEN, WILLIAMS, ALBAYRAM, KURKOVSKY, ZABIHIMAVAN. Use...
Consider the following list of names: PEVAC, MARKOV, ZLATAREVA, ABDOLLAHZADEH, CHEN, WILLIAMS, ALBAYRAM, KURKOVSKY, ZABIHIMAVAN. Use Radix sort to sort them in alphabetic order. How many passes would be made through the outer while loop? Trace the contents of the list after each of these passes
. Consider the following list of names: PEVAC, MARKOV, ZLATAREVA, ABDOLLAHZADEH, CHEN, WILLIAMS, ALBAYRAM, KURKOVSKY, ZABIHIMAVAN....
. Consider the following list of names: PEVAC, MARKOV, ZLATAREVA, ABDOLLAHZADEH, CHEN, WILLIAMS, ALBAYRAM, KURKOVSKY, ZABIHIMAVAN. Use Radix sort to sort them in alphabetic order. How many passes would be made through the outer while loop? Trace the contents of the list after each of these passes. In JAVA please!
The following is a list of the first names of 24 fictional students in a statistics...
The following is a list of the first names of 24 fictional students in a statistics class at Cleveland State University. The students are identified by numbering them 01 through 24. A sample of six students is to be selected by using systematic sampling. The first student to be selected is the one with ID 03. Choose the six students that will be included in the sample. ID Student ID Student 01 Ali 13 Sakiya 02 Pedro 14 Mary 03...
Creating a list/tuple 3.Given a list of tuples containing student first names and last names e.g....
Creating a list/tuple 3.Given a list of tuples containing student first names and last names e.g. (“John”, “Doe”) that represents a class. Ask the user for a students first and last name and check if that student is a member of the class.
Create an array list (names) that will hold the names of several hall of fame soccer...
Create an array list (names) that will hold the names of several hall of fame soccer players. 2. Place your name on the screen as the master recorder:              “Master Hall of Fame Recorder: John Paul Jones” 3. Ask the user how many names they would like to record. (input 5 when the program runs) 4. Create a loop that will ask for the “Hall of fame member #1: “, etc Add in the following names: Pele Rooney Maradona Messi...
Consider the following Markov chain with P{X0 = 2} = 0.6 and P{X0 = 4} =...
Consider the following Markov chain with P{X0 = 2} = 0.6 and P{X0 = 4} = 0.4: 1 2 3 4 5 6 1 0 0 0 0 1 0 2 .2 .05 0 .6 0 .15 3 0 0 .8 0 0 .2 4 0 .6 0 .2 0 .2 5 1 0 0 0 0 0 6 0 0 .7 0 0 .3 a. What is P{X1 = 4, X2 = 6 | X0 = 2}? b. What...
Write SQL queries below for each of the following: List the names and cities of all...
Write SQL queries below for each of the following: List the names and cities of all customers List the different states the vendors come from (unique values only, no duplicates) Find the number of customers in California List product names and category descriptions for all products supplied by vendor Proformance List names of all employees who have sold to customer Rachel Patterson
How many occupations can you list that would need to use scientific names for plants? How...
How many occupations can you list that would need to use scientific names for plants? How would these groups of people be affected by a change in names for plants?
Dr. Williams decides to open a real estate company which he names, Comfort Realty, Inc. The...
Dr. Williams decides to open a real estate company which he names, Comfort Realty, Inc. The following transactions were made over the period: On September 1, 2018, Dr. Godwin invested $17,000 cash in the business On September 3, 2018, Comfort Realty purchases computer equipment for $8,000 cash. On September 4, 2018, Comfort realty purchases for $1,600 from ACME supply company computer paper and other supplies exacted to last several months. ACME agrees to allow Comfort Realty to pay this bill...
The italicized list below includes scientific names (binomial names) for 5 different organisms. To complete this...
The italicized list below includes scientific names (binomial names) for 5 different organisms. To complete this assignment prepare a document using the textbook (Chapter 1, Diversity of Life) and credible websites as sources (refer to Introductory assignments: Website Credibility for guidelines) that (1) explains the concept and importance of a scientific name- 5 pts., (2) briefly describes each of the following organisms, including the common name- 2 pts. each, (3) assigns each organism to the appropriate domain and kingdom- 1...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT