bookmark - Array Sorting Does anyone hava a decent JAVA method

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.
bookmark - Array Sorting Does anyone hava a decent JAVA method  
    
free web hosting
 
does anyone have a decent JAVA method that will sort an array from smallest-to highest? :P The one i'm trying to write doesn't seem to work... or if you could look at mine below, and tell me what i did wrong, that'd be nice to.(if it reallllyyyyyyyyy sucks, please dont laugh to loud :P )
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   


egads... forgot to insert the <PRE> tag:( sry if its a little hard to read for anyone who looks at it:(

Fri Feb 11, 2005    Reply    New Discussion   

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

Fri Feb 11, 2005    Reply    New Discussion   

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 :P

one more thing, about your code: try to post it in betweend code-tags next time, so it's a bit structured :P
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 :P

Fri Feb 11, 2005    Reply    New Discussion   


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:

  1. Bubble sort
  2. Heap sort
  3. Insertion sort
  4. Merge sort
  5. Quick sort
  6. Selection sort
  7. 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   

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;
}

Wed Feb 16, 2005    Reply    New Discussion   

btw ... you can use simple method sort an array of int .. :P

java.util.Arrays.sort(int [] array);

Tue Feb 22, 2005    Reply    New Discussion   

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.

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.
[post="33674"]<{POST_SNAPBACK}>[/post]


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   

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.~~ :)

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.~~  :)
[post="33996"]<{POST_SNAPBACK}>[/post]


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...
[post="34006"]<{POST_SNAPBACK}>[/post]


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 .. :D

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   

Actually you can sort any Objects. This means that Integer, Double and your own object can be sorted. Of course, when you use the primitive data type(int, double) you can just use the default, built-in function. For sorting other object, use the Comparator interface as mentioned by ykf. I also think that it uses Merge Sort, if i remember correctly.

Fri May 26, 2006    Reply    New Discussion   

QUOTE (bagas199)


btw ... you can use simple method sort an array of int .. :D

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   

Quickly Post to Array Sorting Does anyone hava a decent JAVA method w/o signup Share Info about Array Sorting Does anyone hava a decent JAVA method using Facebook, Twitter etc. email your friend about Array Sorting Does anyone hava a decent JAVA method Print
Reply / Comment Ask a Question? Share / Bookmark E-Mail a Friend Print


Where Can I Learn To Make Java Applets ? Where can I learn to make java applets ?  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  Eclipse Look N Feel For My Java Application Eclipse Look n Feel for My Java Applicat