1. Member
Join Date
Aug 2011
Posts
10
Rep Power
0

## 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?

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

2. 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.

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

3. Member
Join Date
Aug 2011
Posts
10
Rep Power
0
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 10:49 AM. Reason: Code tags added, [code]...[/code]

4. 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

5. 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]

6. 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

7. Member
Join Date
Aug 2011
Posts
10
Rep Power
0
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. Member
Join Date
Aug 2011
Posts
47
Rep Power
0
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. Member
Join Date
Aug 2011
Posts
10
Rep Power
0
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 10:31 PM. Reason: added [code] ... [/code] tags

10. Yup, that's correct.

kind regards,

Jos

11. Member
Join Date
Aug 2011
Posts
10
Rep Power
0
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)
{

}

}

}```

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. 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.

#### Posting Permissions

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