0

I am new to java and we are task to make an insertion sort but I have a little problem with my codes I can't print my Sorted values I can't figure out where are my faults ...please help me. thank you

import java.util.Scanner;
public class Sorting {

public static void main(String[] args){
System.out.println("Enter 10 numbers to be sorted separated with spaces:");
String values = new Scanner(System.in).nextLine();
 String [] Vstring = values.split(" ");
 int [] num = new int[Vstring.length];

 for(int i = 0; i<num.length; i++){
    num[i]=Integer.parseInt(Vstring[i]);

     }

     }
     public static void insertionSort(int[] list, int n){
     for(int i = 0; i<n; i++)
      {
        int key = list[i];
        int j = i-1;
        while(j >=0 && list[j]>key){
            list[j+1] = list[j];
            j=j-1;
        }

        list[j+1] = key;
        }


        }

       public static void printValues(int [] list){
       for (int i = 0; i<list.length; i++){
           System.out.println(list[i]+"");
       }
      System.out.println();
     }
     }
Kara
  • 6,115
  • 16
  • 50
  • 57
JulzBlues
  • 1
  • 1

4 Answers4

0

To start, you nested a function within main. I don't think you can do that. Please separate them (I'll show an example) and see if that helps you. Also, be careful with user input. Assume the user is going to make mistakes, and program defensively. What if I were to type "Hello" into this program? It would throw a hard exception and crash. Try to prevent this kind of thing.

Also, I don't know if arrays can be passed by reference or not in Java, so I've modified your sort method to return the new "sorted" array.

public static void main(String[] args){

System.out.println("Enter 10 numbers to be sorted separated with spaces:");

String values = new Scanner(System.in).nextLine();

 String [] Vstring = values.split(" ");
 int [] num = new int[Vstring.length];

 for(int i = 0; i< num.length; i++){
      num[i] = Integer.parseInt(Vstring[i]);
 }

 num = insertionSort(num, num.length);
 printValues(num);

}

 public static int[] insertionSort(int[] list, int n){
     for(int i = 0; i<n; i++)
      {
        int key = list[i];
        int j = i-1;
        while(j >=0 && list[j]>key){
            list[j+1] = list[j];
            j=j-1;
        }

        list[j+1] = key;
        }

      return list;

  }


  public static void printValues(int [] list){
       for (int i = 0; i<list.length; i++){
           System.out.println(list[i]+"");
       }

       System.out.println();

  }
Community
  • 1
  • 1
Ricky Mutschlechner
  • 4,291
  • 2
  • 31
  • 36
0

Try this code, You should call insertionSort(num, num.length) method and printValues(int[] list) inside main method. I modified the return type of insertionSort method as well.

public static void main(String[] args) {
    System.out.println("Enter 10 numbers to be sorted separated with spaces:");
    String values = new Scanner(System.in).nextLine();
    String[] Vstring = values.split(" ");
    int[] num = new int[Vstring.length];

    for (int i = 0; i < num.length; i++) {
        num[i] = Integer.parseInt(Vstring[i]);

    }

    int[] result=insertionSort(num, num.length);
    printValues(result);
}

public static int[] insertionSort(int[] list, int n) {
    for (int i = 0; i < n; i++) {
        int key = list[i];
        int j = i - 1;
        while (j >= 0 && list[j] > key) {
            list[j + 1] = list[j];
            j = j - 1;
        }

        list[j + 1] = key;
    }
    return list;
}

public static void printValues(int[] list) {
    for (int i = 0; i < list.length; i++) {
        System.out.println(list[i] + "");
    }
    System.out.println();
}
user3717646
  • 436
  • 3
  • 10
0

The problem is that you aren't calling your functions in main. Your code will compile, (although you might want to have some indents/spaces for readability... Ctrl+A then Ctrl+I will help somewhat) but you will not get any results.

Here is what you have to do:

import java.util.Scanner;
public class Sorting {

    public static void main(String[] args){

        System.out.println("Enter 10 numbers to be sorted separated with spaces:");
        String values = new Scanner(System.in).nextLine();
        String [] Vstring = values.split(" ");
        int [] num = new int[Vstring.length];

        for(int i = 0; i<num.length; i++){
            num[i]=Integer.parseInt(Vstring[i]);
        }

        //HERE!!!!! Call the functions!
        insertionSort(num, num.length); 
        printValues(num);

    }

    public static void insertionSort(int[] list, int n){
        for(int i = 0; i<n; i++)
        {
            int key = list[i];
            int j = i-1;
            while(j >=0 && list[j]>key){
                list[j+1] = list[j];
                j=j-1;
            }

            list[j+1] = key;
        }
    }

    public static void printValues(int [] list){

        for (int i = 0; i<list.length; i++){
            System.out.println(list[i]+"");
        }
        System.out.println();
    }
}

I hope it helped! :)

-1

You do not call the printValues function. You also do not call the sorting function. The code inside a function is only executed once you call it. Also I would suggest indenting the code so that things inside two braces are indented twice etc.

BenMorel
  • 34,448
  • 50
  • 182
  • 322
Joonazan
  • 1,408
  • 10
  • 18