Results 1 to 12 of 12
  1. #1
    eoins2345 is offline Member
    Join Date
    Aug 2011
    Posts
    10
    Rep Power
    0

    Default Selection sort question

    Identify and describe the sort technique in the code extract below using pseudocode,activity diagram or otherwise,Is the sort order ascending or descending?

    Java Code:
    public static void main(int array[], int n) {
            int mIndex;
            int temp;
            int i,j;
    
            for(i=n-1;i>0;i--){
                mIndex=i;
                for(j=0;j<i;j++){
                    if(array[j]>array[mIndex]){
                        mIndex=j;
                    }
                }
                   if(mIndex != i){
                       temp=array[i];
                       array[i]=array[mIndex];
                       array[mIndex] = temp;
         }
     }
    }
    Ok i knows its a selection sort and the code find the smallest element and swaps it with the first.

    Im just wondering what the two for statements do? Also is the sort order ascending?

    Thanks in advance
    Moderator Edit: Code tags added
    Last edited by Fubarable; 08-20-2011 at 04:25 PM. Reason: Moderator Edit: Code tags added

  2. #2
    JosAH's Avatar
    JosAH is online now Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,453
    Blog Entries
    7
    Rep Power
    20

    Default

    Quote Originally Posted by eoins2345 View Post
    Ok i knows its a selection sort and the code find the smallest element and swaps it with the first.
    Your implementation finds the largest element and swaps it with the last element.

    Quote Originally Posted by eoins2345 View Post
    Im just wondering what the two for statements do? Also is the sort order ascending?
    Put in some System.out.println( ... ) statements and print out the values for i and j and the relevant array element(s) and see for yourself.

    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

  3. #3
    eoins2345 is offline Member
    Join Date
    Aug 2011
    Posts
    10
    Rep Power
    0

    Default

    Java Code:
    public static void main(String[] args) {
           int array[]={4,7,2,9,10,3,8,5,1,6};
            int mIndex;
            int n=10;
            int temp;
            int i,j;
    
            for(i=n-1;i>0;i--){
                mIndex=i;
                for(j=0;j<i;j++){
                    if(array[j]>array[mIndex]){
                        mIndex=j;
                    }
                }
                   if(mIndex != i){
                       temp=array[i];
                       array[i]=array[mIndex];
                       array[mIndex] = temp;
         }
                System.out.print(
     }
    }
    }
    just wondering how you would output the sorted array for this code?
    Last edited by sunde887; 08-21-2011 at 09:49 AM. Reason: Code tags added, [code]...[/code]

  4. #4
    JosAH's Avatar
    JosAH is online now Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,453
    Blog Entries
    7
    Rep Power
    20

    Default

    Quote Originally Posted by eoins2345 View Post
    just wondering how you would output the sorted array for this code?
    Print each array element and use a loop for that ... or, if you want to do it fancy, read about the methods in the Arrays utility class.

    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

  5. #5
    sunde887's Avatar
    sunde887 is offline Moderator
    Join Date
    Jan 2011
    Location
    Richmond, Virginia
    Posts
    3,069
    Blog Entries
    3
    Rep Power
    8

    Default

    Please use code tags when posting code, it will make your code much more readable for others. I have edited them into your second post and Jos edited them into the first. They are simple enough to use, first type [code] paste code, then type [/code]

    [code]
    YOUR CODE HERE
    [/code]

  6. #6
    JosAH's Avatar
    JosAH is online now Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,453
    Blog Entries
    7
    Rep Power
    20

    Default

    Quote Originally Posted by sunde887 View Post
    I have edited them into your second post and Jos edited them into the first.
    Fubarable did that; I didn't do anything (as usual ;-)

    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

  7. #7
    eoins2345 is offline Member
    Join Date
    Aug 2011
    Posts
    10
    Rep Power
    0

    Default

    Ok so the sort order is ascending but places the largest element last and second largest second last.I get it now. it pseudocode just explaining it in steps that humans can understand,like plain english?

  8. #8
    Willriker is offline Member
    Join Date
    Aug 2011
    Posts
    47
    Rep Power
    0

    Default

    Yeah, psuedocode is plain english. Just write down the logic used in the code, step by step. My teacher wanted one step per line on the paper.

  9. #9
    eoins2345 is offline Member
    Join Date
    Aug 2011
    Posts
    10
    Rep Power
    0

    Default

    Java Code:
    public static void sort (float array[]) {
                sort(array,array.length);
    
         }
          private static void sort(float[] , int n ) {
          int mIndex; float temp; int j;
      
    
         if(n>1) {
              mIndex=n-1;
              for( j=n-2;j>=0;j--){
              if(array[j] > array[mIndex]) {
              mIndex=j;
               }
            }
    
    
         if(mIndex!=n-1){
        temp=array[n-1]
        array[n-1]=array[mIndex]
        array[mIndex] =  temp 
      }
        sort(array,n-1) ;
       } 
    }
    Identify and describe the sort technique in the code extract below using pseudocode,activity diagram or otherwise.Is the sort order ascending or descending?

    Am I right in saying it picks the largest element and puts it in the last position and second largest in the second last position and so the code order would ascending?
    Last edited by eoins2345; 08-23-2011 at 09:31 PM. Reason: added [code] ... [/code] tags

  10. #10
    JosAH's Avatar
    JosAH is online now Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,453
    Blog Entries
    7
    Rep Power
    20

    Default

    Yup, that's correct.

    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

  11. #11
    eoins2345 is offline Member
    Join Date
    Aug 2011
    Posts
    10
    Rep Power
    0

    Default

    Write a method to store an array of integers to a file.Clearly state any assumptions.Got an exam 2moro so need to learn how to do this

    Java Code:
    public class StoreArray {
    
     int[] array={ 8,3,6,3,7,1};
        public StoreArray(int[] c)
        {
    
    
    array=c;}
    
        public int[]  getArray()
        { return array;
    
        }
    
    
    
        public void toFile()
        {
            File arrayFile = new File("array.txt");
            try
            {
                PrintWriter output = new PrintWriter(arrayFile);
                System.out.println(getArray()+"");
                output.println(getArray()+"");
                output.close();
    
    
            }
            catch(FileNotFoundException exception)
            {
                System.out.println("File not found");
    
            }
    
    
            }
    
    
    
    
    
    
        }

    This isnt compiling so could be a load of errors.netbeans says no main classes found.How could I correct this or make the program work?Thanks Eoin

  12. #12
    Junky's Avatar
    Junky is offline Grand Poobah
    Join Date
    Jan 2011
    Location
    Dystopia
    Posts
    3,788
    Rep Power
    7

    Default

    When you get errors it helps us if you copy and paste the full and exact error message here. "It doesn't work" provides us with zero information.

Similar Threads

  1. Selection Sort. please help!
    By cassato in forum New To Java
    Replies: 4
    Last Post: 03-14-2011, 10:26 PM
  2. Is this a Selection Sort?
    By Metastar in forum New To Java
    Replies: 2
    Last Post: 10-22-2010, 05:00 AM
  3. Problem with selection sort
    By Metastar in forum New To Java
    Replies: 6
    Last Post: 10-21-2010, 02:18 AM
  4. selection sort
    By mayhewj7 in forum New To Java
    Replies: 1
    Last Post: 04-29-2009, 12:40 AM
  5. Selection sort in Java
    By Java Tip in forum Algorithms
    Replies: 0
    Last Post: 04-15-2008, 07:41 PM

Posting Permissions

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