插入排序 - 无效的方法声明

时间:2016-02-25 14:14:30

标签: java methods insertion-sort

我正在制作一个接受(Int,Double,String)的插入排序程序..但是我不能称之为方法,它说的方法声明无效,我无法弄清楚什么是真正的问题.....................................

import java.util.*;
import java.util.Scanner;


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

  insertionSort(input);

  private static void printNumbers(int[] input) 
  { 
    for (int i = 0; i < input.length; i++) 
    { 
      System.out.print(input[i] + ", "); 
    } 
    System.out.println("\n"); 
  } 

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

2 个答案:

答案 0 :(得分:1)

您需要在主要方法中调用insertionSort(input);。只需将方法调用移至1行即可。

答案 1 :(得分:-3)

您将方法声明为

hdfs dfs -put

而不是

public static void insertionSort(int array[])