0
import java.util.Scanner;
import java.util.Arrays;

public class rr {

public static void main(String[] args)  { 
int i =1;
Scanner input = new Scanner(System.in);
System.out.print("Please enter the number of data points: ");
int data = input.nextInt();
double [] userArray = new double[data];
if(data < 0){
    System.out.println("The number should be posotive. Exiting.");
}
else {System.out.println("Enter the data:"); }
while (i <= data) {
 int userInput = input.nextInt();
       i ++;

 } 
insertionSort(userArray);
}

static void insertionSort(double[] arr) {
  int i, j;
  double newValue;
  for (i = 1; i < arr.length; i++) {
        newValue = arr[i];
        j = i;
        while (j > 0 && arr[j - 1] > newValue) {
              arr[j] = arr[j - 1];
              j--;
        }
        arr[j] = newValue;
  }
  System.out.println(Array.toString(arr));
}
}

The program is supposed to take the values inputted by the user, sort them with the insertionSort method, then print it. I think that the values are being sorted but they are not being printed for some reason.

154 guy
  • 43
  • 7

1 Answers1

1

There seems to be a typo in sysout, we need to use System.out.println(Arrays.toString(arr)); to print the contents of array.

Darshan Mehta
  • 30,102
  • 11
  • 68
  • 102