Insertion Sort - How to accept input and print the sorted array

空扰寡人 提交于 2019-12-31 04:18:07

问题


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

回答1:


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.

Below is working code

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


来源:https://stackoverflow.com/questions/35628712/insertion-sort-how-to-accept-input-and-print-the-sorted-array

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!