Results 1 to 4 of 4
Thread: Need help. Array sort
- 11-07-2008, 12:37 AM #1
Member
- Join Date
- Nov 2008
- Posts
- 2
- Rep Power
- 0
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
Each time the number is entered the list grow as represented.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
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.
- 11-07-2008, 12:51 AM #2
Member
- Join Date
- Nov 2008
- Posts
- 2
- Rep Power
- 0
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();
}
}
}
- 11-07-2008, 02:24 AM #3
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;
- 11-07-2008, 04:24 AM #4
- Join Date
- Jul 2007
- Location
- Colombo, Sri Lanka
- Posts
- 11,374
- Blog Entries
- 1
- Rep Power
- 18
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
-
Array sort
By Jeremy720 in forum New To JavaReplies: 2Last Post: 10-07-2008, 11:41 PM -
How to sort a list using Bubble sort algorithm
By Java Tip in forum AlgorithmsReplies: 3Last Post: 04-29-2008, 08:04 PM -
How to sort an array
By Java Tip in forum java.langReplies: 0Last Post: 04-14-2008, 08:48 PM -
sort
By Camden in forum New To JavaReplies: 7Last Post: 11-28-2007, 01:11 AM -
how to sort
By Feng in forum New To JavaReplies: 1Last Post: 11-20-2007, 06:56 AM


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks