插入排序与壳牌排序程序。 Shell排序仅在某些时候有效

时间:2011-12-09 16:53:54

标签: java sorting insertion-sort shellsort

好的,这是一个数据结构类。分配是编写一个程序,从txt文件中获取100个整数的列表,为shell排序采用2个不同的4个区间组,然后按插入排序排序100个数字,1)按shell排序4个数字作为间隔,3)shell按第二个100作为间隔排序,将排序列表输出到txt文件,打印每种排序中发生的赋值操作量(尚未完成此部分)。

当我编译并运行其中一个shell排序时,通常不能完全正常工作,尽管它已经部分排序。没有完全排序的shell排序可以是排序,所以我假设程序使用某些间隔而不是其他,这不是很好:)。任何人都可以帮忙

/**
 * @(#)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;
   }
}
}


}

1 个答案:

答案 0 :(得分:1)

我认为问题在于“inc”(如inc1)向量可能并不总是以1结尾。算法需要此步骤,因为最后一次迭代应该像普通的插入排序一样(但是因为几个元素将已经排序。)

如果是这种情况,您可以在代码中添加额外的检查。如果没有,请提供一个例子。