|
|
|
|
![]() ![]() |
| malgainis |
Feb 11 2005, 12:36 PM
Post
#1
|
|
Guests |
does anyone have a decent JAVA method that will sort an array from smallest-to highest?
This should take an array and then calculate how many of a number is in it, add it to the first slots of another array, and then repeat until the 2nd array is full to the length of the array that is sent to it. After which it will get the median and return it. CODE public static int getMedian(int[] list, int countt) { int median; int lowestNum = 1; boolean newNum = false; int[] orderedArray = new int[countt + 1]; int count = 0; int index = 0; while(index < countt) { if(count == 0) { int minNum = lowestNum; lowestNum = 100; for(int i = 0; i < list.length; i++) { if(list[i] > minNum && list[i] < lowestNum) { lowestNum = list[i]; } } } for(int i = 0; i < list.length; i++) { if(list[i] == lowestNum) { count++; list[i] = -1; } } for(int x = 0; x < count; x++) { orderedArray[index] = lowestNum; index++; } count = 0; } median = (index / 2) + 1; return median; } |
|
|
|
| malgainis |
Feb 11 2005, 12:39 PM
Post
#2
|
|
Guests |
egads... forgot to insert the <PRE> tag:( sry if its a little hard to read for anyone who looks at it:(
|
|
|
|
Feb 11 2005, 01:22 PM
Post
#3
|
|
|
Pretty please? Group: Members Posts: 733 Joined: 28-November 04 From: Holland Member No.: 1,552 |
Euh... Have you heard of the bubblesort technique?
http://en.wikipedia.org/wiki/Bubble_sort I know the Visual Basic version of it... If you want it, ill post it. Good luck |
|
|
|
Feb 11 2005, 02:38 PM
Post
#4
|
|
|
Premium Member Group: [HOSTED] Posts: 336 Joined: 22-September 04 Member No.: 798 |
aight
let's say you have the array with the beautiful name "array" that has some elements in it. this is a method to sort it. CODE INT tmp; int lenght = array.length; for(int i = 1; i < length; i++){ tmp = array[i]; int j=i; while (tmp <array[j-1]){ array[j]=array[j-1]; j--; } array[j]=tmp; } this should work. haven't tested it though. if you want to use double's instead of int's , just change the INT (in capitals) into double. that should do the trick it's not the fasted method but it works. i've had 2 years of algoritms and can provide you with extreme fast algoritms if you need a way to sort thousands of numbers one more thing, about your code: try to post it in betweend code-tags next time, so it's a bit structured and: it's very C++. you're not using the benefits of java. like the fact that the lenght of an array is stored inside it (array.lenght) i bet you learned C++ first |
|
|
|
Feb 11 2005, 03:09 PM
Post
#5
|
|
|
PsYcheDeLiC dR3aMeR Group: Admin Posts: 2,242 Joined: 29-January 05 From: Nakorn Chaisri, Thailand Member No.: 2,411 |
If you want to do some more reading on sorting alogrithms, go to this site:
http://linux.wku.edu/~lamonml/algor/sort/sort.html They've got discussions on a pretty decent list of commonly used sorting algorithms, namely:
You'll find detailed discussions on the pros and cons of each method as well as the efficiency of each sorting algorithm. Working code samples in C/C++ are also provided, and judging from your coding style you shouldn't have much difficulty porting them to java. Have you grabbed a copy of JavaDocs yet - I think you need one (it's the Java comtemporary of the MSDN libraries), coz as marjinn pointed out, you should use the benefits that the java framework provides you to the fullest extent. Makes life easier as well as in certain cases it actually optimizes and speeds up your code All the best... |
|
|
|
| malgainis |
Feb 16 2005, 06:10 PM
Post
#6
|
|
Guests |
A much delayed thank you for posting that:)
and another note: this version of it will work if you have a array that has a certain length, but number of numbers in it is controlled by a sentinel: where counta is a number count of the elements in the array CODE public static int[] sort(int[] array, int counta) { int tmp; int length = counta + 1; for(int i = 1; i < counta; i++) { tmp = array[i]; int j=i; while (tmp < array[j-1]) { array[j]=array[j-1]; j--; } array[j]=tmp; } return array; } |
|
|
|
Feb 22 2005, 06:20 AM
Post
#7
|
|
|
Newbie [ Level 1 ] Group: Members Posts: 2 Joined: 22-February 05 Member No.: 2,746 |
btw ... you can use simple method sort an array of int ..
java.util.Arrays.sort(int [] array); |
|
|
|
May 12 2005, 03:00 AM
Post
#8
|
|
|
Member [ Level 1 ] Group: Members Posts: 34 Joined: 25-April 05 From: San Diego, California Member No.: 4,345 |
DON"T USE BUBBLE SORT!!!! it is just realy, realy, rreally, slow. Think of bubble sort as a bottle of some substanvce of low viscosity with bubbles speead throughout it, it takes a long time for the bubbles to slowly climb their way to the top, as with the bubble sort, it takes a long time for teh elements to slowly reach tehir appropriate position.
the fastest is Quick Sort, but, dependign on teh order and value of the elemnts alreadsy in the array, it is somettimes faster and sometimes slower, but on average, it is faster, at least, than bubble sort (Thoughthat isn't to asay very much). Insertion is also slow, it loops throught he vaues and inserts the values in order at the beginning (and then it ahs to move all tehj other elements over one to maek room for it whch takes a lot of time). Well, you should basically read about it yourself and decide whch one is most applicable, just as long as you don't use bubble sort. |
|
|
|
May 13 2005, 11:31 PM
Post
#9
|
|
|
Guilty Until Proven Innocent Group: Members Posts: 372 Joined: 13-April 05 Member No.: 3,937 |
QUOTE(eyvind @ May 12 2005, 11:00 AM) DON"T USE BUBBLE SORT!!!! *** Well, you should basically read about it yourself and decide whch one is most applicable, just as long as you don't use bubble sort. Actually I do have this really bad habit of mixing stuffs.. I do use bubble sort but not in the native why that it is laid out.. I combine it with some of the other algorithym.. once you get the idea on how those sorting stuffs work, there will be a point that you will discover the loop holes or bottle necks that they present.. A good thing for you to know is to learn all the principles of all the sorting algorithym, study the pit fall and the advantages and combine those into a newer algo.. Be sure also to have lotsa time testing and benchmarking.. |
|
|
|
May 14 2005, 07:08 AM
Post
#10
|
|
|
Newbie [ Level 2 ] Group: Members Posts: 27 Joined: 14-May 05 From: Hong Kong Member No.: 5,054 |
Why don't you just use the builtin java sorting function in java.util package, Arrays.sort(int[] a), to do that?
The builtin function use quick sort, which is much more effective than the method you described. Also because it's well published (it's here since Java 2), it's already heavily tested and sure will not have any bugs, rather than coded by yourself.~~ |
|
|
|
![]() ![]() |
Similar Topics
|
Lo-Fi Version | Time is now: 12th October 2008 - 05:13 PM |