Results 1 to 2 of 2
  1. #1
    homeclock is offline Member
    Join Date
    Nov 2015
    Posts
    1
    Rep Power
    0

    Angry 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

  2. #2
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    20,003
    Rep Power
    33

    Default Re: Insertion Sorting array help

    If you don't understand my response, don't ignore it, ask a question.

Similar Threads

  1. Insertion sorting in Vectors ?
    By nerc in forum New To Java
    Replies: 7
    Last Post: 06-10-2014, 08:48 AM
  2. Fast array for insertion/removal
    By Skiller in forum New To Java
    Replies: 4
    Last Post: 02-20-2011, 03:23 PM
  3. Sorting Array UI
    By Brandon Seale in forum New To Java
    Replies: 6
    Last Post: 02-18-2011, 01:50 AM
  4. Help me with this array element insertion
    By javanew in forum New To Java
    Replies: 4
    Last Post: 09-07-2010, 02:49 PM
  5. Replies: 0
    Last Post: 01-17-2010, 06:09 AM

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •