java sort function ?
let's say i have an array of integer.
is there any java sort function so that i can call something like this ?
java.xxx.sort(myIntegerArray)
and it returns me an sorted integer array ??
thanks
Signature (up to 100 characters) You may use Markup in your signature
[310 byte] By [
Zerg] at [2007-11-15 20:12:27]

# 2 Re: java sort function ?
I couldn't find that class, but the following should work:
int[] x = {5, 2, 7, 9, 3};
java.util.Arrays.sort(x);
for (int i = 0; i < x.length; i++) {
System.out.println("element no. " + (i + 1) + " = " + x);
}
--------------
weaver
icq# 64665116
Please rate this post.
http://weaver.x7.htmlplanet.com
weaver at 2007-11-10 2:54:39 >

# 6 Re: java sort function ?
I have copied someof the sourcecode from java.util.Arrays class and have created a class called "Arrays". Give a look at it. You can use this "Arrays" class in your code.
public class sortEx{
public static void main( String[] str ){
int[] intArray = { 4, 2 , 12 , 5 , 3 , 4 , 10 };
Arrays.sort( intArray );
for( int index = 0 ; index < intArray.length ; index++ ){
System.out.println( intArray[index] );
}
}
}
class Arrays{
public static void sort(int[] a) {
sort1(a, 0, a.length);
}
private static void sort1(int x[], int off, int len) {
// Insertion sort on smallest arrays
if (len < 7) {
for (int i=off; i<len+off; i++)
for (int j=i; j>off && x[j-1]>x[j]; j--)
swap(x, j, j-1);
return;
}
// Choose a partition element, v
int m = off + len/2; // Small arrays, middle element
if (len > 7) {
int l = off;
int n = off + len - 1;
if (len > 40) { // Big arrays, pseudomedian of 9
int s = len/8;
l = med3(x, l, l+s, l+2*s);
m = med3(x, m-s, m, m+s);
n = med3(x, n-2*s, n-s, n);
}
m = med3(x, l, m, n); // Mid-size, med of 3
}
int v = x[m];
// Establish Invariant: v* (<v)* (>v)* v*
int a = off, bb = a, c = off + len - 1, d = c;
while(true) {
while (bb <= c && x[bb] <= v) {
if (x[bb] == v)
swap(x, a++, bb);
bb++;
}
while (c >= bb && x[c] >= v) {
if (x[c] == v)
swap(x, c, d--);
c--;
}
if (bb > c)
break;
swap(x, bb++, c--);
}
// Swap partition elements back to middle
int s, n = off + len;
s = Math.min(a-off, bb-a ); vecswap(x, off, bb-s, s);
s = Math.min(d-c, n-d-1); vecswap(x, bb, n-s, s);
// Recursively sort non-partition-elements
if ((s = bb-a) > 1)
sort1(x, off, s);
if ((s = d-c) > 1)
sort1(x, n-s, s);
}
private static void swap(int x[], int a, int bb) {
int t = x[a];
x[a] = x[bb];
x[bb] = t;
}
private static int med3(int x[], int a, int bb, int c) {
return (x[a] < x[bb] ?
(x[bb] < x[c] ? bb : x[a] < x[c] ? c : a) :
(x[bb] > x[c] ? bb : x[a] > x[c] ? c : a));
}
private static void vecswap(int x[], int a, int b, int n) {
for (int i=0; i<n; i++, a++, b++)
swap(x, a, b);
}
}
poochi at 2007-11-10 2:58:44 >
