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
vhortex
post May 14 2005, 10:10 AM
Post #11


Guilty Until Proven Innocent
Group Icon

Group: Members
Posts: 372
Joined: 13-April 05
Member No.: 3,937



QUOTE(ykf @ May 14 2005, 03:08 PM)
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
*



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

like doing something if this certain condition was meet etc etc...
Go to the top of the page
 
+Quote Post
ykf
post May 14 2005, 04:21 PM
Post #12


Newbie [ Level 2 ]
Group Icon

Group: Members
Posts: 27
Joined: 14-May 05
From: Hong Kong
Member No.: 5,054



QUOTE(vhortex @ May 14 2005, 06:10 PM)
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.
Go to the top of the page
 
+Quote Post
BitShift
post May 26 2006, 05:45 AM
Post #13


Advanced Member
Group Icon

Group: Members
Posts: 153
Joined: 8-May 06
From: Houston, TX
Member No.: 13,291



QUOTE(bagas199 @ Feb 22 2005, 01:20 AM) *

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

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

Go to the top of the page
 
+Quote Post
yeh
post May 26 2006, 06:17 AM
Post #14


Advanced Member
Group Icon

Group: Members
Posts: 147
Joined: 13-May 06
Member No.: 13,389



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.
Go to the top of the page
 
+Quote Post
snutz411
post May 27 2006, 05:41 PM
Post #15


Advanced Member
Group Icon

Group: Members
Posts: 105
Joined: 22-December 05
Member No.: 10,229



QUOTE(bagas199 @ Feb 22 2005, 02:20 AM) *

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

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


Haha, I was about to post the same thing.
Go to the top of the page
 
+Quote Post
jc804
post Mar 7 2008, 10:19 PM
Post #16


Newbie [ Level 2 ]
Group Icon

Group: Members
Posts: 14
Joined: 7-March 08
Member No.: 28,957



QUOTE(ykf @ May 14 2005, 01:08 AM) *
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

here's an example of the java api Arrays.sort in use

CODE
import java.util.Arrays;
public class MainClass {
  public static void main(String[] a) {
    int array[] = { 2, 5, -2, 6, -3 };
    Arrays.sort(array);
    for (int i : array) {
      System.out.println(i);
    }
  }
}

CONSOLE

-3
-2
2
5
6


This post has been edited by jc804: Mar 7 2008, 11:47 PM
Go to the top of the page
 
+Quote Post
Umar Shah
post Mar 28 2008, 10:19 PM
Post #17


Newbie [ Level 2 ]
Group Icon

Group: Members
Posts: 22
Joined: 25-March 08
Member No.: 29,382



lets start with a bubble sort code in JAVA

CODE

public class BubbleSort
{
public static void main(String args[])
{

int array[] = new int[10];
for (int i=0; i < array.length; i++){
array[i] = (int)(java.lang.Math.random()*100);
}
bubbleSort(array);

for (int i=0; i < array.length; i++){
System.out.println("Val of array[" + i + "] = " +array[i] );
}

}

public static void bubbleSort(int arr[])
{
for (int i=0; i<arr.length; i++)
{
for (int j=0; j<arr.length-i; j++)
{

if(arr[j]>arr[j+1]){
int tmp =arr[j];
arr[j]=arr[j+1];
arr[j+1]=tmp;

}
}

}
}
}

for each iteration of the inner loop the next highest value is bubbled to the last position of the remaining array;



This post has been edited by Umar Shah: Mar 28 2008, 10:20 PM
Go to the top of the page
 
+Quote Post
Umar Shah
post Mar 28 2008, 10:25 PM
Post #18


Newbie [ Level 2 ]
Group Icon

Group: Members
Posts: 22
Joined: 25-March 08
Member No.: 29,382



Next I'll try to demonstrate a relatively simpler sort called Selection sort;

CODE
public class SelectionSort
{
public static void main(String args[])
{

int array[] = new int[10];
for (int i=0; i < array.length; i++){
array[i] = (int)(java.lang.Math.random()*100);
}
selectionSort(array);

for (int i=0; i < array.length; i++){
System.out.println("Val of array[" + i + "] = " +array[i] );
}

}

public static void selectionSort(int arr[])
{
for (int i=0; i<arr.length; i++)
{
for (int j=i+1; j<arr.length-1; j++)
{

if(arr[i]>arr[j]){
int tmp =arr[j];
arr[i]=arr[j];
arr[j]=tmp;

}
}

}
}
}


in this example the inner loop just compares all the elements from position i to the end to find the mininmum one to be placed at position i,

the position determined by each iteration of outer loop.

This post has been edited by Umar Shah: Mar 28 2008, 10:37 PM
Go to the top of the page
 
+Quote Post
Umar Shah
post Mar 28 2008, 10:37 PM
Post #19


Newbie [ Level 2 ]
Group Icon

Group: Members
Posts: 22
Joined: 25-March 08
Member No.: 29,382



Now in this example i'll demonstrate one of my favourite O(n^2) sorts.
This one is called insertion sort.

[/code]
public class InsertionSort
{
public static void main(String args[])
{

int array[] = new int[10];
for (int i=0; i < array.length; i++){
array[i] = (int)(java.lang.Math.random()*100);
}
insertionSort(array);

for (int i=0; i < array.length; i++){
System.out.println("Val of array[" + i + "] = " +array[i] );
}

}

public static void insertionSort(int arr[])
{
int j=0;
for (int i=1; i<arr.length;)
{

if(arr[i]<arr[j]){
int tmp =arr[j];
arr[i]=arr[j];
arr[j]=tmp;
if(j>0){
j--;
}
else {
j=i;
i++;
}
}
else {
j=i;
i++;
}

}
}
}


[/code]


Although we dont have two nested loops in this example , nonetheless it worls like two nested loops.

the outer loop is only iterated if the present array upto position i is completely sorted. Otherwise j is decremented upto 0 so that the new element a[i] is inserted at its proper position in the partial array.
Go to the top of the page
 
+Quote Post
xboxrulz
post Mar 28 2008, 10:39 PM
Post #20


Colonel Panic
Group Icon

Group: [MODERATOR]
Posts: 2,706
Joined: 25-March 05
From: Toronto, Ontario, Canada
Member No.: 3,233



This is my code for an array sorter. It's quite efficient for counting too.

CODE
/**
*
* @author xboxrulz
* Source Code is released under CDDL if requested.
*
*/
import java.util.Arrays;
public class Main {

    /**
     * @param args the command line arguments
     */
    public static void main(String[] args) {
        int intCount;
        String strNames[] = {"Gary", "Adrian", "Corey", "Alex", "Adrien", "Josh", "Zev"};
        for (intCount=0; intCount<=6;intCount++)
        {
            Arrays.sort(strNames);
            System.out.println("Hello\t" + strNames[intCount]);
        }
    }

}


Unlike the code provided by the poster above, this one is very human-readable.

Names are people in my class.

xboxrulz
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. Java Unlimited(14)
  3. What Are The Advantages Of Java Vs C++?(15)
  4. Need Help: Find Lowest Character Using Java(6)
  5. Download Java Ebooks(13)
  6. Video Streaming In Web Browser Through Java Or Jsp(1)
  7. Looking For A Java IDE(24)
  8. Java App To Web App(12)
  9. On Why Java Is 'c'ooler!(10)
  10. How To Configure/intergrate Jboss 4 With Java?(1)
  11. Snake In Java(2)
  12. Other Sound Format Support(3)
  13. Java Phone Book(2)
  14. How To Create Exe File In Java?(12)
  15. How Do I Test A Java Aplication(11)
  1. Mozilla And Java!(2)
  2. Need To Modify Xml Attribute Using Java(4)
  3. Bluetooth And Java(5)
  4. Java Sdk Vs. Java Jdk?(2)
  5. Graphcal User Interfaces In Java(4)
  6. Java Db Help Pls(2)
  7. Loading 3d Models In Java?(2)
  8. Java Applet Loading Error(5)
  9. Setting Up Java Correctly(8)
  10. Java Java.security.accesscontrolexception(6)
  11. Simple Java Question(1)
  12. Java And Sql: Data Mismatch(6)
  13. Java Memory Leak?(0)


 



- Lo-Fi Version Time is now: 22nd August 2008 - 01:57 AM