插入排序 - 如何接受输入并打印排序的数组

时间:2016-02-25 13:30:48

标签: java algorithm insertion-sort

我正在尝试执行一个插入排序程序,它接受任何数据类型(Int,Double,String),然后打印已排序的数组。我知道我的代码工作正常,但我无法弄清楚真正的问题。

import java.util.*;
public class MyInsertionSort 

{
    public static void main(String[] args) 
    { 
        Scanner in = new Scanner(System.in);
        System.out.print("Enter anything you want");
        String insertionSort = in.nextLine(); 
        int num=Integer.parseInt(insertionSort);
        String array[] = new String [num];
        for (int i = 0; i < array.length; i++)
        {
            System.out.print("Input the Number at array index "+i+": ");
            array[i] = in.nextLine();
        }

    public static void insertionSort(int array[]) 
    { 
        int n = array.length; 
        for (int j = 1; j < n; j++) 
        { 
            int key = array[j]; 
            int i = j-1; 
            while ( (i > -1) && ( array [i] > key ) ) 
            { 
                array [i+1] = array [i]; i--; 
            } 
            array[i+1] = key; 
            printNumbers(array); 
        }
    }
} 

1 个答案:

答案 0 :(得分:0)

通用类型插入排序

我做了快速检查并修复了所有错误。只需在比较工具中比较您的代码和此代码。这样你就可以了解到你错过了什么。此代码能够使用String,double,int等多种数据类型进行排序。它可以针对任何实现可比较的对象进行更改。

以下是工作代码

import java.util.Scanner;
public class MyInsertionSort 
{
    public static void main(String[] args) 
    { 
        Scanner in = new Scanner(System.in);

        System.out.print("Enter data type to sort : ");
        String type = in.nextLine(); 

        System.out.print("Enter number of elements : ");
        String insertionSort = in.nextLine(); 
        int num=Integer.parseInt(insertionSort);
        String array[] = new String[num];
        for (int i = 0; i < array.length; i++)
        {
            System.out.print("Input the Number at array index "+i+": ");
            array[i] = in.nextLine();
        }
        MyInsertionSort.insertionSortByType(array,type);
        in.close();
    }


    public static void insertionSortByType(String array[], String type)
    {
        switch (type) {
            case "double":
                Double[] ConvertedArrayDouble = new Double[array.length];
                for (int i = 0; i<array.length; i++) ConvertedArrayDouble[i] = Double.parseDouble(array[i]);
                MyInsertionSort.insertionSort(ConvertedArrayDouble);
                break;
            case "int":
                Integer[] ConvertedArrayInt = new Integer[array.length];
                for (int i = 0; i<array.length; i++) ConvertedArrayInt[i] = Integer.parseInt(array[i]);
                MyInsertionSort.insertionSort(ConvertedArrayInt);
                break;
            default:
                MyInsertionSort.insertionSort(array);   
        }
    }

    public static <E extends Comparable<? super E>> void insertionSort(E array[]) 
    { 
        int n = array.length; 
        for (int j = 1; j < n; j++) 
        { 
            E key = array[j]; 
            int i = j-1; 
            while ( (i > -1) && ( array[i].compareTo(key) > 0 ) ) 
            { 
                array [i+1] = array [i]; i--; 
            } 
            array[i+1] = key; 
        }

        printNumbers(array); 
    }

    public static <E> void printNumbers(E array[]) {
        for (E i : array) {
            System.out.println(i);
        }
    }
}