首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >插入排序与外壳排序程序。Shell排序只在某些情况下有效

插入排序与外壳排序程序。Shell排序只在某些情况下有效
EN

Stack Overflow用户
提问于 2011-12-10 00:53:54
回答 1查看 1.1K关注 0票数 1

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

当我编译和运行其中一个shell排序时,尽管它已经部分排序,但它通常不能完全工作。没有完全排序的shell排序可以是这两种排序中的任何一种,所以我假设程序以特定的间隔工作,但不能以其他间隔工作,这是不好的:)。有人能帮上忙吗

代码语言:javascript
复制
/**
 * @(#)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;
   }
}
}


}
EN

回答 1

Stack Overflow用户

发布于 2011-12-10 02:01:54

我认为问题在于"inc“(如inc1)向量可能并不总是以1结束。这一步是算法所必需的,因为最后一次迭代应该类似于普通的插入排序(但速度要快得多,因为一些元素已经排序)。

如果是这种情况,您可以在代码中添加额外的检查。如果没有,请举例说明。

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/8448898

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档