Welcome Guest ( Log In | Register )



3 Pages V   1 2 3 >  
Reply to this topicStart new topic
> Array Sorting, Does anyone hava a decent JAVA method
malgainis
post 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? mellow.gif 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 biggrin.gif )
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;        
            
}
Go to the top of the page
 
+Quote Post
malgainis
post 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:(
Go to the top of the page
 
+Quote Post
jipman
post Feb 11 2005, 01:22 PM
Post #3


Pretty please?
Group Icon

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
Go to the top of the page
 
+Quote Post
marijnnn
post Feb 11 2005, 02:38 PM
Post #4


Premium Member
Group Icon

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

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


Go to the top of the page
 
+Quote Post
miCRoSCoPiC^eaRt...
post Feb 11 2005, 03:09 PM
Post #5


PsYcheDeLiC dR3aMeR
Group Icon

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:
  • 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...
Go to the top of the page
 
+Quote Post
malgainis
post 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;
}
Go to the top of the page
 
+Quote Post
bagas199
post Feb 22 2005, 06:20 AM
Post #7


Newbie [ Level 1 ]
Group Icon

Group: Members
Posts: 2
Joined: 22-February 05
Member No.: 2,746



btw ... you can use simple method sort an array of int .. tongue.gif

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

Go to the top of the page
 
+Quote Post
eyvind
post May 12 2005, 03:00 AM
Post #8


Member [ Level 1 ]
Group Icon

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.
Go to the top of the page
 
+Quote Post
vhortex
post May 13 2005, 11:31 PM
Post #9


Guilty Until Proven Innocent
Group Icon

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..
Go to the top of the page
 
+Quote Post
ykf
post May 14 2005, 07:08 AM
Post #10


Newbie [ Level 2 ]
Group Icon

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.~~ unsure.gif
Go to the top of the page
 
+Quote Post

3 Pages V   1 2 3 >
Reply to this topicStart new topic

Collapse

> Similar Topics

Topics Topics
  1. java.lang.NullPointException(4)
  2. Using system date in java... How?(5)
  3. Java Unlimited(14)
  4. What Are The Advantages Of Java Vs C++?(15)
  5. Need Help: Find Lowest Character Using Java(7)
  6. Download Java Ebooks(13)
  7. Java By Example(8)
  8. Video Streaming In Web Browser Through Java Or Jsp(1)
  9. Looking For A Java IDE(25)
  10. On Why Java Is 'c'ooler!(10)
  11. Other Sound Format Support(3)
  12. Java Phone Book(2)
  13. How To Create Exe File In Java?(13)
  14. How Do I Test A Java Aplication(11)
  15. Mozilla And Java!(2)
  1. Need To Modify Xml Attribute Using Java(4)
  2. Bluetooth And Java(5)
  3. Java Sdk Vs. Java Jdk?(2)
  4. Graphcal User Interfaces In Java(4)
  5. Java Db Help Pls(2)
  6. Loading 3d Models In Java?(2)
  7. Java Applet Loading Error(5)
  8. Setting Up Java Correctly(8)
  9. Java Java.security.accesscontrolexception(6)
  10. Simple Java Question(3)
  11. Java And Sql: Data Mismatch(6)
  12. Java Memory Leak?(0)
  13. Java Mouse Movement.(2)


 



- Lo-Fi Version Time is now: 12th October 2008 - 02:47 PM