Jump to content
xisto Community
Sign in to follow this  
iGuest

Array Sorting Does anyone hava a decent JAVA method

Recommended Posts

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.

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

Share this post


Link to post
Share on other sites

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.

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

Share this post


Link to post
Share on other sites

If you want to do some more reading on sorting alogrithms, go to this site:

 

http://forums.xisto.com/no_longer_exists/

 

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

Share this post


Link to post
Share on other sites

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

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

Share this post


Link to post
Share on other sites

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.

Share this post


Link to post
Share on other sites

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


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

Share this post


Link to post
Share on other sites

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

Share this post


Link to post
Share on other sites

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


well i guess coz we need some more extra while we are searching..

 

like doing something if this certain condition was meet etc etc...

Share this post


Link to post
Share on other sites

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


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.

Share this post


Link to post
Share on other sites

btw ... you can use simple method sort an array of int .. :D
java.util.Arrays.sort(int [] array);



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

Share this post


Link to post
Share on other sites

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.

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
Sign in to follow this  

×
×
  • Create New...

Important Information

Terms of Use | Privacy Policy | Guidelines | We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.