package BinarySearchSort;
public class Program
{
public static class Student
{
public int ID;
public String name;
public int age;
public double GPA;
// constructor
public Student(int ID, String name, int age, double GPA)
{
this.ID = ID;
this.name = name;
this.age = age;
this.GPA = GPA;
}
} // class Student
public static int BinarySearch(int A[],
int k,
int p, int q)
{
int m;
if (p>q)
{
System.out.print("Error in BinarySearch: the routine was called with p > q");
Runtime r = Runtime.getRuntime();
r.exit(0);
}
while (p != q)
{
m = (p + q) / 2;
if (A[m] > A[k])
q = m ;
else if (A[m] < A[k])
p = m + 1;
else
return m;
}
return p;
}
public static void InsertionSort(int A[])
{
int k,temp;
int N = A.length;
System.out.print(" ");
for (int j = 0; j < N; j++) System.out.print("[" + j + "]");
System.out.println();
// values
System.out.print(" ");
for (int j = 0; j < N; j++) System.out.print(" " + A[j] + " ");
System.out.println();
for (int i = 1; i < N; i++)
{
k = BinarySearch(A, i, 0, i);
System.out.print("Place "+ A[i]+ " on position " + k + ":");
for (int j = i; j - 1 >= k; j--)
{
temp = A[j];
A[j] = A[j - 1];
A[j - 1] = temp;
}
for (int j = 0; j < N; j++) System.out.print(" " + A[j] + " ");
System.out.println();
try
{
System.in.read();
}
catch (java.io.IOException e)
{
e.printStackTrace();
}
}
}
public static int BinarySearchStructure(Student A[],
int k,
int p, int q)
{
int m;
if (p > q)
{
System.out.print("Error in BinarySearch: the routine was called with p > q");
Runtime r = Runtime.getRuntime();
r.exit(0);
}
while (p != q)
{
m = (p + q) / 2;
if (A[m].ID > A[k].ID)
q = m;
else if (A[m].ID < A[k].ID)
p = m + 1;
else
return m;
}
return p;
}
public static void InsertionSortStructure(Student A[])
{
int k;
int N = A.length;
Student temp = new Student(0,"",0,0.0);
String spacesString = " ";
int distances[] = new int[N];
System.out.print(spacesString);
for (int j = 0; j < N; j++)
{
System.out.print("[" + j + "]");
distances[j] = A[j].name.length() - "[0]".length() + 1;
for (int n = 0; n < distances[j]; n++) System.out.print(" ");
}
System.out.println();
System.out.print(spacesString+" ");
for (int j = 0; j < N; j++)
{
System.out.print(A[j].ID+" ");
for (int n = 0; n < distances[j]; n++) System.out.print(" ");
}
System.out.println();
System.out.print(spacesString + " ");
for (int j = 0; j < N; j++) System.out.print(A[j].name + " ");
System.out.println();
System.out.print(spacesString+" ");
for (int j = 0; j < N; j++)
{
System.out.print(A[j].age + " ");
for (int n = 0; n < distances[j]; n++) System.out.print(" ");
}
System.out.println();
System.out.print(spacesString+" ");
for (int j = 0; j < N; j++)
{
System.out.print(A[j].GPA + " ");
for (int n = 0; n < distances[j]-1; n++) System.out.print(" ");
}
System.out.print(spacesString);
System.out.println();
for (int i = 1; i < N; i++)
{
k = BinarySearchStructure(A, i, 0, i);
System.out.println("Place " + A[i].name + " on position " + k + ":");
for (int j = i; j - 1 >= k; j--)
{
temp = A[j];
A[j] = A[j - 1];
A[j - 1] = temp;
}
System.out.print(spacesString + " ");
for (int j = 0; j < N; j++)
{
System.out.print(A[j].ID + " ");
for (int n = 0; n < distances[j]; n++) System.out.print(" ");
}
System.out.println();
System.out.print(spacesString + " ");
for (int j = 0; j < N; j++) System.out.print(A[j].name + " ");
System.out.println();
System.out.print(spacesString + " ");
for (int j = 0; j < N; j++)
{
System.out.print(A[j].age + " ");
for (int n = 0; n < distances[j]; n++) System.out.print(" ");
}
System.out.println();
System.out.print(spacesString + " ");
for (int j = 0; j < N; j++)
{
System.out.print(A[j].GPA + " ");
for (int n = 0; n < distances[j]-1; n++) System.out.print(" ");
}
System.out.print(spacesString);
System.out.println();
try
{
System.in.read();
}
catch (java.io.IOException e)
{
e.printStackTrace();
}
}
}
public static void main(String[] args)
{
int maxNumStudents = 10;
Student[] A = new Student[maxNumStudents];
java.io.BufferedReader file;
String line;
String[] studentTextData;
java.util.StringTokenizer tokenizer;
String fileName = "Students.txt";
System.out.println("List(unsorted) of students from comma delimited file "+ fileName );
System.out.println();
int studentNum = -1;
try {
file = new java.io.BufferedReader(new java.io.FileReader(fileName));
while (studentNum < maxNumStudents-1)
{
studentNum++;
if ((line = file.readLine()) == null) {
System.out.println("The file has less than "+maxNumStudents+" records.");
Runtime r = Runtime.getRuntime();
r.exit(0);
}
tokenizer = new java.util.StringTokenizer(line, ",");
A[studentNum] = new Student(
Integer.parseInt(tokenizer.nextToken().trim()), // ID
tokenizer.nextToken().trim(), // name
Integer.parseInt(tokenizer.nextToken().trim()), // age
Double.parseDouble(tokenizer.nextToken().trim())); // GPA
System.out.println(A[studentNum].ID + " "+ // ID
A[studentNum].name + " "+ //name
A[studentNum].age + " "+ //age
A[studentNum].GPA); // GPA
}
file.close();
} catch (java.io.FileNotFoundException e) {
e.printStackTrace();
}
catch (java.io.IOException e){
e.printStackTrace();
}
System.out.println();
System.out.println();
System.out.println("Sort by Binary Insertion Algorithm :");
System.out.println();
InsertionSortStructure(A);
String sortedFile = "Students_sorted.txt";
java.io.BufferedWriter outFile;
System.out.println("Sorted list of students from comma delimited file " + fileName);
System.out.println("was output to file " +sortedFile);
try {
outFile = new java.io.BufferedWriter(new java.io.FileWriter(sortedFile));
for (studentNum = 0; studentNum < maxNumStudents; studentNum++)
{
System.out.println(A[studentNum].ID + " " + // ID
A[studentNum].name + " " + //name
A[studentNum].age + " " + //age
A[studentNum].GPA); // GPA
outFile.write(A[studentNum].ID + " " + // ID
A[studentNum].name + " " + //name
A[studentNum].age + " " + //age
A[studentNum].GPA); // GPA
outFile.newLine();
}
outFile.close();
}
catch (java.io.IOException e)
{
e.printStackTrace();
}
}
}
我收到编译错误.....
Exception in thread "main" java.lang.Error: Unresolved compilation problem:
No enclosing instance of type Program is accessible. Must qualify the allocation with an enclosing instance of type Program (e.g. x.new A() where x is an instance of Program).
at BinarySearchSort.Program.main(Program.java:279)