Results 1 to 12 of 12
Thread: Selection sort question
- 08-20-2011, 04:24 PM #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?
Ok i knows its a selection sort and the code find the smallest element and swaps it with the first.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; } } }
Im just wondering what the two for statements do? Also is the sort order ascending?
Thanks in advance
Moderator Edit: Code tags addedLast edited by Fubarable; 08-20-2011 at 04:25 PM. Reason: Moderator Edit: Code tags added
- 08-20-2011, 04:31 PM #2
- Join Date
- Sep 2008
- Location
- Voorschoten, the Netherlands
- Posts
- 11,405
- Blog Entries
- 7
- Rep Power
- 17
Your implementation finds the largest element and swaps it with the last element.
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,
JosWhen people rob a bank they get a penalty; when banks rob people they get a bonus.
- 08-21-2011, 12:14 AM #3
Member
- Join Date
- Aug 2011
- Posts
- 10
- Rep Power
- 0
just wondering how you would output the sorted array for this code?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( } } }Last edited by sunde887; 08-21-2011 at 09:49 AM. Reason: Code tags added, [code]...[/code]
- 08-21-2011, 09:46 AM #4
- Join Date
- Sep 2008
- Location
- Voorschoten, the Netherlands
- Posts
- 11,405
- Blog Entries
- 7
- Rep Power
- 17
- 08-21-2011, 09:50 AM #5
- Join Date
- Jan 2011
- Location
- Richmond, Virginia
- Posts
- 3,069
- Blog Entries
- 3
- Rep Power
- 7
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]
- 08-21-2011, 10:01 AM #6
- Join Date
- Sep 2008
- Location
- Voorschoten, the Netherlands
- Posts
- 11,405
- Blog Entries
- 7
- Rep Power
- 17
- 08-22-2011, 09:40 PM #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?
- 08-23-2011, 12:04 AM #8
Member
- Join Date
- Aug 2011
- Posts
- 40
- 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.
- 08-23-2011, 09:22 PM #9
Member
- Join Date
- Aug 2011
- Posts
- 10
- Rep Power
- 0
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 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) ; } }
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
- 08-23-2011, 09:27 PM #10
- Join Date
- Sep 2008
- Location
- Voorschoten, the Netherlands
- Posts
- 11,405
- Blog Entries
- 7
- Rep Power
- 17
Yup, that's correct.
kind regards,
JosWhen people rob a bank they get a penalty; when banks rob people they get a bonus.
- 08-24-2011, 04:38 PM #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) { 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 #12
Similar Threads
-
Selection Sort. please help!
By cassato in forum New To JavaReplies: 4Last Post: 03-14-2011, 10:26 PM -
Is this a Selection Sort?
By Metastar in forum New To JavaReplies: 2Last Post: 10-22-2010, 05:00 AM -
Problem with selection sort
By Metastar in forum New To JavaReplies: 6Last Post: 10-21-2010, 02:18 AM -
selection sort
By mayhewj7 in forum New To JavaReplies: 1Last Post: 04-29-2009, 12:40 AM -
Selection sort in Java
By Java Tip in forum AlgorithmsReplies: 0Last Post: 04-15-2008, 07:41 PM


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks