Log in

View Full Version : Selection sort help



maxquattromani
12-13-2010, 01:09 AM
Hi,

I'm working on this assignment for my CS1 class and I'm stuck on the proper use of selection sort (assignment specifies selection, I can't use bubble or any other type of sort).

I have to take integers from a data file (file contents below) and read them into an array of length 15.

After the array is filled use a method to print the list of numbers including their original index/position. I have the code working up to this point.

Here's what my question is: I have to re-sort them in descending order and keep the original index/position. How do I? and more specifically where do use this code?

I have the code provided by the instructor to do this but I'm not sure how to initiate inside the code as I have it. Can it be called in main? should I run main and complete and then use it as a separate method? Can it be its own method inside of main?

Thanks for taking a look and pointing me in the right direction.

Here's my code up to this point (it compiles and runs - output below):



import java.io.*;
import java.util.*;

public class prgrm10
{ public static void main(String [] args) throws Exception
{ int Max = 15;
double []A = new double[Max];
double []S = new double[Max];
int ACtr = 0;
int i = 0;
int left = 0;
String filename = null;
Scanner console = new Scanner("prgrm10.dat");
String line = null;
StringTokenizer st;

//create the header
PrintWriter outFile = new PrintWriter("prgrm10.out");
outFile.println("Original Unsorted List");
outFile.println("*******************");
outFile.println(String.format("%5s %10s", "INDEX", "NUMBER"));

//input of the array
try
{ filename = console.nextLine();
Scanner fileIn = new Scanner(new FileReader(filename));
while(i < 15 && fileIn.hasNext())
{ line = fileIn.nextLine();
st = new StringTokenizer(line);
A[i] = Double.parseDouble(st.nextToken());
S[i] = i;
i++;
}
//counting the number of lines left in the file
while(fileIn.hasNext())
{ left++;
line = fileIn.nextLine();
}
//processing of the array
//first determine the size of the partially filled array
ACtr = i;

//output of the array to the file
for(int j = 0; j < ACtr; j++)
outFile.println(String.format("%5s %10s", j,A[j]));

//output of the number of elements in the array and the number
//of values from the input file which are not stored in the array

outFile.println("Number of elements in A is " + ACtr);
outFile.println("Number of values not in A is " + left);
} //end try

catch(FileNotFoundException e)
{ System.out.println(filename + " -- File Not Found"); }
outFile.close();

} //end of main
}


Here is the selection sort code as it was provided:




public static void SelectionSort(int [] A)

{ int k, i, minI, temp;
for(k = 0; k<A.length - 1; k++)
{
minI = k;
for (i = k + 1; i<A.length; i++)
{ if(A[i] < A[minI])
minI = i;
}

temp = A[minI];
A[minI] = A[k];
A[k] = temp;
}
}



Here is the data from the input file that I am working with:



88
90
56
99
11
78
82
100
27
39
100
99
46
31
73
98
21
7
100


Here is my output currently:



Original Unsorted List
*******************
INDEX NUMBER
0 88.0
1 90.0
2 56.0
3 99.0
4 11.0
5 78.0
6 82.0
7 100.0
8 27.0
9 39.0
10 100.0
11 99.0
12 46.0
13 31.0
14 73.0
Number of elements in A is 15
Number of values not in A is 4