插入排序 - 如何接受输入并打印排序的数组 [英] Insertion Sort - How to accept input and print the sorted array

查看:100
本文介绍了插入排序 - 如何接受输入并打印排序的数组的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I was trying to do a Insertion Sort Program that accepts any Data Type (Int, Double, String) then print's the sorted array. I know that my code work's but i can't figure out the real problem.

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); 
        }
    }
} 


推荐答案

通用类型插入排序



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

Generic type Insertion Sort

I did a quick check and fixed all of the errors. Just compare your code and this one in a comparing tool. So that you can learn what you missed. This code is capable of doing sorting with Multiple data types like String, double, int as of now. It can be altered for any object that implements comparable.

下面是工作代码

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);
        }
    }
} 

这篇关于插入排序 - 如何接受输入并打印排序的数组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆