Results 1 to 7 of 7
Like Tree2Likes
  • 1 Post By jim829
  • 1 Post By jim829

Thread: Coping random generated numbers to selection sort method

  1. #1
    Jeremyss is offline Member
    Join Date
    Mar 2015
    Posts
    3
    Rep Power
    0

    Default Coping random generated numbers to selection sort method

    Java Code:
    import java.util.ArrayList;
    import java.util.Random;
    
    
    public class NumSorting
    {
       public static int numOfComps = 0,
                         numOfSwaps = 0;
    
       public static void main(String[] args)
       {
            System.out.println("\nOriginal order:");
            int size = 5;
    
            ArrayList<Integer> list = new ArrayList<Integer>(size);
    
            for(int i = 1; i <= size; i++)
            {
               list.add(i);
            }
    
            Random rand = new Random();
            while(list.size() > 0)
            {
             int index = rand.nextInt(list.size());
             System.out.print(list.remove(index) + " ");
            }
    
          System.out.println();
    
           int[] test = convertIntegers(list);
           int[] a = new int[5];
    
           // Selection Sort
           System.out.println("\n\nSelection Sort");
    
           // Display copy of random generated number original order
           System.out.println("\nOriginal order:");      
           System.arraycopy(test, 0, a, 0, test.length);
           System.out.println(a + " ");
    
           // Selection Sort method
           selectionSort(a);
    
           System.out.println("\nSorted order: ");
           for(int element : a)
             System.out.print(element + " ");
             
           // Blank line
           System.out.println();
        }  
    
        public static int[] convertIntegers(ArrayList<Integer> integers)
        {
            int[] num = new int[integers.size()];
            for (int i=0; i < num.length; i++)
            {
            num[i] = integers.get(i).intValue();
            }
            return num;
         }
    
       public static void selectionSort(int[] array)
       {
          int startScan;   // Starting position of the scan
          int index;       // To hold a subscript value
          int minIndex;    // Element with smallest value in the scan
          int minValue;    // The smallest value found in the scan
    
          // The outer loop iterates once for each element in the
          // array. The startScan variable marks the position where
          // the scan should begin.
          for (startScan = 0; startScan < (array.length-1); startScan++)
          {
             // Assume the first element in the scannable area
             // is the smallest value.
             minIndex = startScan;
             minValue = array[startScan];
    
             // Scan the array, starting at the 2nd element in
             // the scannable area. We are looking for the smallest
             // value in the scannable area. 
             for(index = startScan + 1; index < array.length; index++)
             {
                if (array[index] < minValue)
                {
                   minValue = array[index];
                   minIndex = index;
                }
    
                // Counts the number of values comparisons
                numOfComps ++;
             }
             // Counts the number of values swaps
             if(minIndex != startScan)
                {
                    numOfSwaps ++;
                }
    
             // Swap the element with the smallest value 
             // with the first element in the scannable area.
             array[minIndex] = array[startScan];
             array[startScan] = minValue;
    
          }
           System.out.println("\nNumber of comps = " + numOfComps);
           System.out.println("Number of swaps = " + numOfSwaps);
        }
    }

    What is wrong with my code in this sorting program? It won't copy the random generated numbers to the sort method. Please show me how to get the random generated numbers to copy to the sort method. Below is what the program is displaying. Thanks for your help.
    --------------------------

    Original order:
    3 2 5 4 1


    Selection Sort

    Original order:
    [I@7a84e4

    Number of comps = 10
    Number of swaps = 0

    Sorted order:
    0 0 0 0 0
    Last edited by Jeremyss; 03-19-2015 at 11:35 PM.

  2. #2
    jim829 is offline Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    6,226
    Rep Power
    15

    Default Re: Coping random generated numbers to selection sort method

    Please place your code between [code][/code] tags for proper formatting. Also show what the program is displaying.

    Regards,
    Jim
    Jeremyss likes this.
    The JavaTM Tutorials | SSCCE | Java Naming Conventions
    Poor planning on your part does not constitute an emergency on my part

  3. #3
    pbrockway2 is offline Moderator
    Join Date
    Feb 2009
    Location
    New Zealand
    Posts
    4,716
    Rep Power
    19

    Default Re: Coping random generated numbers to selection sort method

    I don't see you adding any randomly selected ints to the list. You add some ints, in order, to the list and then remove them in a random order.

  4. #4
    Jeremyss is offline Member
    Join Date
    Mar 2015
    Posts
    3
    Rep Power
    0

    Default Re: Coping random generated numbers to selection sort method

    Quote Originally Posted by pbrockway2 View Post
    I don't see you adding any randomly selected ints to the list. You add some ints, in order, to the list and then remove them in a random order.
    Hi pbrockway2, what should I do to fix this program?

  5. #5
    jim829 is offline Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    6,226
    Rep Power
    15

    Default Re: Coping random generated numbers to selection sort method

    As you remove the numbers randomly from a list you should add them to an array or another list. By the time you call convertIntegers, your list is empty. Also, you should place more print statements thru out your program to aid in debugging. Here are some hints. To print a list you can do the following:

    Java Code:
    System.out.println(list);
    To print an array, you can do the following:

    Java Code:
    System.out.println(Arrays.toString(myArray));
    In either case, if list or myArray contains objects of some homegrown class, then you need to override toString() in the class definition to print out meaningful information.

    Regards,
    Jim
    Jeremyss likes this.
    The JavaTM Tutorials | SSCCE | Java Naming Conventions
    Poor planning on your part does not constitute an emergency on my part

  6. #6
    Jeremyss is offline Member
    Join Date
    Mar 2015
    Posts
    3
    Rep Power
    0

    Default Re: Coping random generated numbers to selection sort method

    Quote Originally Posted by jim829 View Post
    As you remove the numbers randomly from a list you should add them to an array or another list. By the time you call convertIntegers, your list is empty. Also, you should place more print statements thru out your program to aid in debugging. Here are some hints. To print a list you can do the following:

    Java Code:
    System.out.println(list);
    To print an array, you can do the following:

    Java Code:
    System.out.println(Arrays.toString(myArray));
    In either case, if list or myArray contains objects of some homegrown class, then you need to override toString() in the class definition to print out meaningful information.

    Regards,
    Jim
    Where exactly should I put these print statements in the program so that the random generated numbers will be copied to the sort method? Thanks.

  7. #7
    gimbal2 is offline Just a guy
    Join Date
    Jun 2013
    Location
    Netherlands
    Posts
    5,114
    Rep Power
    13

    Default Re: Coping random generated numbers to selection sort method

    Quote Originally Posted by Jeremyss View Post
    Where exactly should I put these print statements in the program so that the random generated numbers will be copied to the sort method? Thanks.
    Wherever you think it is a good idea to put them after thinking about it.
    "Syntactic sugar causes cancer of the semicolon." -- Alan Perlis

Similar Threads

  1. Making selection sort into a recursive method.
    By Mleclerc182 in forum New To Java
    Replies: 5
    Last Post: 10-21-2014, 04:00 PM
  2. Selection sort method?
    By Propinquity in forum New To Java
    Replies: 2
    Last Post: 03-05-2014, 10:46 AM
  3. Selection sort to quick sort conversion without Nodes?
    By VettesRus in forum New To Java
    Replies: 0
    Last Post: 11-03-2013, 02:54 AM
  4. Replies: 2
    Last Post: 11-26-2012, 04:07 PM
  5. Store a random generated int value in an array.
    By kcon90 in forum New To Java
    Replies: 5
    Last Post: 10-26-2012, 09:31 PM

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •