# Selection sort question

• 08-20-2011, 04:24 PM
eoins2345
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?

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?

• 08-20-2011, 04:31 PM
JosAH
Quote:

Originally Posted by eoins2345
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
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
• 08-21-2011, 12:14 AM
eoins2345
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?
• 08-21-2011, 09:46 AM
JosAH
Quote:

Originally Posted by eoins2345
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
• 08-21-2011, 09:50 AM
sunde887
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]
[/code]
• 08-21-2011, 10:01 AM
JosAH
Quote:

Originally Posted by sunde887
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
• 08-22-2011, 09:40 PM
eoins2345
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?
• 08-23-2011, 12:04 AM
Willriker
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.
• 08-23-2011, 09:22 PM
eoins2345
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?
• 08-23-2011, 09:27 PM
JosAH
Yup, that's correct.

kind regards,

Jos
• 08-24-2011, 04:38 PM
eoins2345
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

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
• 08-25-2011, 05:14 AM
Junky
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.