Results 1 to 4 of 4
  1. #1
    buzz1500 is offline Member
    Join Date
    Nov 2008
    Posts
    2
    Rep Power
    0

    Thumbs up Need help. Array sort

    i am writing a program with array. the program will generate ten random numbers
    i am trying put those number into empty array. the list has to be in numerically ascending as numbers are entered
    e.g 12 87 8 15 16 1 77 99 10 40.
    the output should be
    12 12 8 8 8 1 1 1 1 1
    87 12 12 12 8 8 8 8 8
    87 15 15 12 12 12 10 10
    87 16 15 15 15 12 12
    87 16 16 16 15 15
    87 77 77 16 16
    87 87 77 40
    99 87 77
    99 87
    99
    Each time the number is entered the list grow as represented.

    i tried to use selection sort but i can't get the required output. I got this output.
    init:
    deps-jar:
    compile-single:
    run-single:
    78
    78 30
    78 30 12
    78 30 12 81
    78 30 12 81 43
    78 30 12 81 43 42
    78 30 12 81 43 42 56
    78 30 12 81 43 42 56 45
    78 30 12 81 43 42 56 45 83
    78 30 12 81 43 42 56 45 83 41
    BUILD SUCCESSFUL (total time: 0 seconds)
    but i don't know how to sort as necessary. Need help.
    Last edited by buzz1500; 11-07-2008 at 12:41 AM.

  2. #2
    buzz1500 is offline Member
    Join Date
    Nov 2008
    Posts
    2
    Rep Power
    0

    Default this is my program

    public class test2 {
    public static void main(String[] args) {

    int i,j, min,p=0, c , N,t,minindex;
    int[] MyList=new int[10];
    int[] MyArray=new int[10];
    int smallest, largest,index;
    int count=0;
    N=10;

    for (i=0;i<N;i++){
    MyArray[i]=(int)(Math.random()*100);

    count++;
    p=0;

    while(p<count)

    {
    MyList[p]=MyArray[p];
    p++;

    }

    for (i=0;i<count;i++){
    min=MyList[i];
    minindex=i;
    for(j=i+1;j<=N-1;j++){
    if (MyList[j]<min){
    min=MyList[j];
    minindex=j;
    }
    }
    t=MyList[i];
    MyList[i]=MyList[minindex];
    MyList[minindex]=t;
    }

    for (i=0;i<count;i++)
    {
    System.out.print(MyArray[i]+" ");
    }


    System.out.println();

    }






    }
    }

  3. #3
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,418
    Rep Power
    25

    Default

    WHere in your code do you do the sort?
    Can you add some comments to your code to describe what it is doing?
    What is MyList and MyArray used for?
    Why do you do this: MyList[p]=MyArray[p];

    Looking at your output, it looks like no values are being swapped?
    What are the following two statements supposed to do?
    min=MyList[j];
    minindex=j;

  4. #4
    Eranga's Avatar
    Eranga is offline Moderator
    Join Date
    Jul 2007
    Location
    Colombo, Sri Lanka
    Posts
    11,372
    Blog Entries
    1
    Rep Power
    20

    Default

    Quote Originally Posted by Norm View Post
    Looking at your output, it looks like no values are being swapped?
    What are the following two statements supposed to do?
    min=MyList[j];
    minindex=j;

    Yes, I cannot see any value sorting too. Can you explain much clearly what you trying to do. As Norm says, it's good practice that when you ask a question put some comments.

    By the way, did you read about Arrays class on Java doc. you can simple use sort() method there, which is ordered element in natural order.

Similar Threads

  1. Array sort
    By Jeremy720 in forum New To Java
    Replies: 2
    Last Post: 10-07-2008, 11:41 PM
  2. How to sort a list using Bubble sort algorithm
    By Java Tip in forum Algorithms
    Replies: 3
    Last Post: 04-29-2008, 08:04 PM
  3. How to sort an array
    By Java Tip in forum java.lang
    Replies: 0
    Last Post: 04-14-2008, 08:48 PM
  4. sort
    By Camden in forum New To Java
    Replies: 7
    Last Post: 11-28-2007, 01:11 AM
  5. how to sort
    By Feng in forum New To Java
    Replies: 1
    Last Post: 11-20-2007, 06:56 AM

Posting Permissions

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