Хорошо, так что это для класса структур данных. Задание состояло в том, чтобы написать программу, которая берет список из 100 целых чисел из txt-файла, берет 2 различных набора по 4 интервала для сортировки оболочки, а затем сортирует 100 чисел, 1) по сортировке вставкой, 2) по сортировке оболочки с первым 4 числа как интервалы, 3) оболочка сортируется по секундам 100 как интервалы, выводит отсортированный список в текстовый файл, печатает количество операций присваивания, которые выполняются в каждой сортировке (еще не выполнили эту часть).
Когда я компилирую и запускаю, один из видов оболочки обычно не работает полностью, хотя он частично отсортирован. Сортировка оболочки, которая не выполняет полную сортировку, может быть любой из них, поэтому я предполагаю, что программа работает с определенными интервалами, но не с другими, что не очень хорошо :). Может ли кто-нибудь помочь
/**
* @(#)Savit5.java
*
* Savit5 application
*
* @author
* @version 1.00 2011/12/8
*/
import java.util.*;
import java.lang.*;
import java.io.*;
public class Savit5 {
public static void main(String[] args) {
try {
Scanner keyboardScanner = new Scanner(System.in);
System.out.println("Please input the input file location:");
String filePath = keyboardScanner.nextLine();
Scanner scanner = new Scanner(new File(filePath));
System.out.println("Please input 4 increment values for the first Shell Sort:");
String shellOne = keyboardScanner.nextLine();
System.out.println("Please input 4 increment values for the Second Shell Sort:");
String shellTwo = keyboardScanner.nextLine();
Scanner scanOne = new Scanner(shellOne);
Scanner scanTwo = new Scanner(shellTwo);
System.out.println("Please input the output file location:");
String out = keyboardScanner.nextLine();
int[] inc1 = new int[4];
int q = 0;
while (scanOne.hasNextInt()){
inc1[q++] = scanOne.nextInt();
}
int[] inc2 = new int[4];
int r = 0;
while (scanTwo.hasNextInt()){
inc2[r++] = scanTwo.nextInt();
}
int [] anArray = new int [100];
int z = 0;
while(scanner.hasNextInt()){
anArray[z++] = scanner.nextInt();
}
int[] anArray2 = (int[])anArray.clone();
int[] anArray3 = (int[])anArray.clone();
int[] count = {0, 0, 0};
int cnt=0;
insertionSort(anArray, count, cnt);
System.out.println("Assignment count:" + count[cnt]);
cnt=1;
shellSort(anArray2, inc1, count, cnt);
System.out.println("Assignment count:" + count[cnt]);
FileWriter output = new FileWriter(out);
PrintWriter out2 = new PrintWriter(output);
out2.println("Insertion Sort:");
for (int i =0; i<anArray.length; i++) {
out2.println(anArray[i]);
}
out2.println("Shell Sort 1:");
for (int i =0; i<anArray2.length; i++) {
out2.println(anArray2[i]);
}
out2.println("Shell Sort 2:");
for (int i =0; i<anArray3.length; i++) {
out2.println(anArray3[i]);
}
out2.close();
}
catch (IOException e)
{
System.out.println (e);
}
}
public static void insertionSort(int[] a, int[] count, int cnt) {
for (int i=1, j; i < a.length; i++) {
int tmp = a[i];
count[cnt] += 1;
for (j = i - 1; j >= 0; j--) {
if (a[j] <= tmp) break;
a[j + 1] = a[j];
count[cnt] += 1;
}
a[j + 1] = tmp;
count[cnt] += 1;
}
}
public static void shellSort(int[] a, int[] inc, int[] count, int cnt) {
for (int k =0; k<inc.length; k++) {
for (int i= inc[k], j; i < a.length; i+=inc[k]) {
int tmp = a[i];
count[cnt] +=1;
for (j = i - inc[k]; j >= 0; j -= inc[k]) {
if (a[j] <= tmp) break;
a[j + inc[k]] = a[j];
count[cnt] +=1;
}
a[j + inc[k]] = tmp;
count[cnt] +=1;
}
}
}
}