# Thread: Insertion Sorting array help

1. Member
Join Date
Nov 2015
Posts
1
Rep Power
0

## Insertion Sorting array help

So the problem asks to arrange an array (type int) from lowest to highest order by using insertion.
This is my code:

"
package wtf;
import java.util.Scanner;
public class InserSort {
public static void main(String[] args)
{
int[] list = new int[50];
int i, n;
System.out.println("How many elements do you want to sort? ");
Scanner paul = new Scanner(System.in);
n = paul.nextInt();
for (i=0; i<n; i++)
{
System.out.printf("Enter element %d\n", i+1);
list[i] = paul.nextInt();
}

int[] list2 = sort(list, n);
for (i=0; i<n; i++)
{
System.out.printf("value %d is: %d\n", i+1, list2[i]);
}
}
public static int[] sort(int[] list, int n)
{
int[] list2 = new int[n];
int i, j, temp, temp2, k=0, count = 0;
boolean p = false;
//list2[0] = list[0];
for (i=0; i<n-1; i++)
{
temp2 = list[i];
temp = list[i];
for (j=i; j<list.length; j++)
{
if (temp>list[j])
{
count++;
for (int l = 0; l<list2.length; l++)
{
if (list2[l] == list[j])
{
p = true;
break;
}
}
if (p = true)
break;
list2[k] = list[j];
//System.out.println("value is shshshs: " + list2[k]);
if (count > 1)
k++;
}

}
count = 0;
p = false;
++k;
list2[k] = temp2;
}
return list2;
}
}
"

The output of the code when using array {2, 4, 6, 10, 3, 1} is:
"
value 1 is: 0
value 2 is: 2
value 3 is: 4
value 4 is: 6
value 5 is: 10
value 6 is: 3
"

I dont know how value 1 turns to 0. Its been stressing me out, cant figure out the problem. Help would be much appreciated
Thanks