|
|
|
|
![]() ![]() |
May 14 2005, 10:10 AM
Post
#11
|
|
|
Guilty Until Proven Innocent 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.~~ well i guess coz we need some more extra while we are searching.. like doing something if this certain condition was meet etc etc... |
|
|
|
May 14 2005, 04:21 PM
Post
#12
|
|
|
Newbie [ Level 2 ] 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. |
|
|
|
May 26 2006, 05:45 AM
Post
#13
|
|
|
Advanced Member Group: Members Posts: 153 Joined: 8-May 06 From: Houston, TX Member No.: 13,291 |
btw ... you can use simple method sort an array of int .. 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 |
|
|
|
May 26 2006, 06:17 AM
Post
#14
|
|
|
Advanced Member 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.
|
|
|
|
May 27 2006, 05:41 PM
Post
#15
|
|
|
Advanced Member Group: Members Posts: 105 Joined: 22-December 05 Member No.: 10,229 |
|
|
|
|
Mar 7 2008, 10:19 PM
Post
#16
|
|
|
Newbie [ Level 2 ] Group: Members Posts: 14 Joined: 7-March 08 Member No.: 28,957 |
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.~~ 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 |
|
|
|
Mar 28 2008, 10:19 PM
Post
#17
|
|
|
Newbie [ Level 2 ] 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 |
|
|
|
Mar 28 2008, 10:25 PM
Post
#18
|
|
|
Newbie [ Level 2 ] 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 |
|
|
|
Mar 28 2008, 10:37 PM
Post
#19
|
|
|
Newbie [ Level 2 ] 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. |
|
|
|
Mar 28 2008, 10:39 PM
Post
#20
|
|
|
Colonel Panic 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 |
|
|
|
![]() ![]() |
Similar Topics
|
Lo-Fi Version | Time is now: 22nd August 2008 - 01:57 AM |