|
|
Array Sorting - Does anyone hava a decent JAVA method | ||
Discussion by malgainis with 21 Replies.
Last Update: August 24, 2008, 6:10 am (View Latest) | Page 1 of 2 pages. | ||
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){
[tab][/tab][tab][/tab]
[tab][/tab]int median;
[tab][/tab]int lowestNum = 1;
[tab][/tab]boolean newNum = false;
[tab][/tab]int[] orderedArray = new int[countt + 1];
[tab][/tab]int count = 0;
[tab][/tab]int index = 0;
[tab][/tab][tab][/tab]
[tab][/tab][tab][/tab]
[tab][/tab]while(index < countt)
[tab][/tab]{[tab][/tab]
[tab][/tab][tab][/tab]if(count == 0)
[tab][/tab][tab][/tab]{
[tab][/tab][tab][/tab][tab][/tab] int minNum = lowestNum;
[tab][/tab][tab][/tab][tab][/tab] lowestNum = 100;
[tab][/tab][tab][/tab] for(int i = 0; i < list.length; i++)
[tab][/tab][tab][/tab] {
[tab][/tab][tab][/tab][tab][/tab][tab][/tab][tab][/tab][tab][/tab] if(list[i] > minNum && list[i] < lowestNum)
[tab][/tab][tab][/tab][tab][/tab][tab][/tab][tab][/tab][tab][/tab] {[tab][/tab][tab][/tab][tab][/tab][tab][/tab][tab][/tab][tab][/tab][tab][/tab][tab][/tab][tab][/tab][tab][/tab][tab][/tab][tab][/tab] lowestNum = list[i];
[tab][/tab][tab][/tab][tab][/tab] }
[tab][/tab][tab][/tab][tab][/tab][tab][/tab][tab][/tab] }
[tab][/tab][tab][/tab] }[tab][/tab]
[tab][/tab][tab][/tab]for(int i = 0; i < list.length; i++)
[tab][/tab][tab][/tab][tab][/tab]{
[tab][/tab][tab][/tab][tab][/tab][tab][/tab]if(list[i] == lowestNum)
[tab][/tab][tab][/tab][tab][/tab] {
[tab][/tab][tab][/tab][tab][/tab][tab][/tab] count++;
[tab][/tab][tab][/tab][tab][/tab][tab][/tab][tab][/tab][tab][/tab][tab][/tab] list[i] = -1;
[tab][/tab][tab][/tab][tab][/tab][tab][/tab]}
[tab][/tab][tab][/tab][tab][/tab] }[tab][/tab]
[tab][/tab][tab][/tab][tab][/tab][tab][/tab][tab][/tab] for(int x = 0; x < count; x++)
[tab][/tab][tab][/tab]{[tab][/tab][tab][/tab][tab][/tab]
[tab][/tab][tab][/tab][tab][/tab][tab][/tab][tab][/tab][tab][/tab][tab][/tab][tab][/tab][tab][/tab] orderedArray[index] = lowestNum;
[tab][/tab][tab][/tab][tab][/tab] index++;
[tab][/tab][tab][/tab]}[tab][/tab][tab][/tab]
[tab][/tab][tab][/tab]count = 0;[tab][/tab][tab][/tab]
[tab][/tab]}
[tab][/tab][tab][/tab]median = (index / 2) + 1;
[tab][/tab][tab][/tab]return median;[tab][/tab][tab][/tab]
[tab][/tab][tab][/tab][tab][/tab]
}
Fri Feb 11, 2005 Reply New Discussion
Fri Feb 11, 2005 Reply New Discussion
http://en.wikipedia.org/wiki/Bubble_sort
I know the Visual Basic version of it... If you want it, ill post it.
Good luck
Fri Feb 11, 2005 Reply New Discussion
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
Fri Feb 11, 2005 Reply New Discussion
http://linux.wku.edu/~lamonml/algor/sort/sort.html
They've got discussions on a pretty decent list of commonly used sorting algorithms, namely:
- Bubble sort
- Heap sort
- Insertion sort
- Merge sort
- Quick sort
- Selection sort
- Shell sort
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...
Fri Feb 11, 2005 Reply New Discussion
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;
}
Wed Feb 16, 2005 Reply New Discussion
java.util.Arrays.sort(int [] array);
Tue Feb 22, 2005 Reply New Discussion
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.
Thu May 12, 2005 Reply New Discussion
QUOTE (eyvind)
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..
Fri May 13, 2005 Reply New Discussion
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.~~
Sat May 14, 2005 Reply New Discussion
QUOTE (ykf)
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.~~
well i guess coz we need some more extra while we are searching..
like doing something if this certain condition was meet etc etc...
Sat May 14, 2005 Reply New Discussion
QUOTE (vhortex)
well i guess coz we need some more extra while we are searching..like doing something if this certain condition was meet etc etc...
He can always implement Comparator interface to do virtually ANYTHING he want. So I wonder why he wants to reinvent the wheels... after all, libraries are tools which makes you NOT to redo what was already done.
Sat May 14, 2005 Reply New Discussion
QUOTE (bagas199)
btw ... you can use simple method sort an array of int ..
java.util.Arrays.sort(int [] array);
Link: view Post: 19211
Actually Arrays.sort() should work with any primitive data type
int, double, char, byte
as long as its an array of a primitive data types it should be able to sort it correctly
it will not sort objects of primitives ( Integer, Double ) you must use the primitive type
by the way if you were wondering wut sort Arrays.sort() uses i think its the Merge Sort
Fri May 26, 2006 Reply New Discussion
Fri May 26, 2006 Reply New Discussion
QUOTE (bagas199)
btw ... you can use simple method sort an array of int ..
java.util.Arrays.sort(int [] array);
Link: view Post: 19211
Haha, I was about to post the same thing.
Sat May 27, 2006 Reply New Discussion
Where Can I Learn To Make Java Applets ? Where can I learn to make java applets ? (5)
|
(5) Eclipse Look N Feel For My Java Application Eclipse Look n Feel for My Java Applicat
|
Index




