import java.util.Random; import java.util.Arrays;public class Test { public static void main(String[] args) { Random rd = new Random() ; //随机数对象 int[] num = new int[10] ; System.out.println("排序前:"); for(int i=0; i<num.length; i++) { num[i] = rd.nextInt(150) ; System.out.print(num[i]+"\t"); }
System.out.println("\n排序后:"); Arrays.sort(num); int[] num2 = new int[num.length] ; //重新构造一个数组
for (int i = num.length-1,j=0; i>=0; i--,j++) { num2[j] = num[i] ; System.out.print(num[i]+"\t"); } } }
public static void sort(long[] a) { sort1(a, 0, a.length); } private static void sort1(long 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 >> 1); // 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 } long v = x[m]; // Establish Invariant: v* (<v)* (>v)* v* int a = off, b = a, c = off + len - 1, d = c; while(true) { while (b <= c && x[b] <= v) { if (x[b] == v) swap(x, a++, b); b++; } while (c >= b && x[c] >= v) { if (x[c] == v) swap(x, c, d--); c--; } if (b > c) break; swap(x, b++, c--); } // Swap partition elements back to middle int s, n = off + len; s = Math.min(a-off, b-a ); vecswap(x, off, b-s, s); s = Math.min(d-c, n-d-1); vecswap(x, b, n-s, s); // Recursively sort non-partition-elements if ((s = b-a) > 1) sort1(x, off, s); if ((s = d-c) > 1) sort1(x, n-s, s); } /** * Swaps x[a] with x[b]. */ private static void swap(long x[], int a, int b) { long t = x[a]; x[a] = x[b]; x[b] = t; } /** * Swaps x[a .. (a+n-1)] with x[b .. (b+n-1)]. */ private static void vecswap(long x[], int a, int b, int n) { for (int i=0; i<n; i++, a++, b++) swap(x, a, b); } /** * Returns the index of the median of the three indexed longs. */ private static int med3(long x[], int a, int b, int c) { return (x[a] < x[b] ? (x[b] < x[c] ? b : x[a] < x[c] ? c : a) : (x[b] > x[c] ? b : x[a] > x[c] ? c : a)); }
to tobeprofessional() 提示说nosuchmethoderr
to kevinliuu(@。@) 谢谢
不用自己写, Collection 可以调用。
楼主懒,我也是新手,要写这样的程序不像老鸟,我还要查java Api 不过这对你是很有帮助的不过,如果楼主只是为应负考试或考查什么的,不想学java,那还是继续在这找高手帮你吧,哈哈
kevinliuu(@。@)注解有点问题: Random rd = new Random() ; //伪随机数流对象
comparator 或者 comparable具体怎么用,搂住还是自己好好看看api吧,不难的
看下JAVA数据结构和算法这本书!别太懒!
楼主懒,我也是新手,要写这样的程序不像老鸟,我还要查java Api 不过这对你是很有帮助的不过,如果楼主只是为应负考试或考查什么的,不想学java,那还是继续在这找高手帮你吧,哈哈UPUP!
Arrays.sort(i);
for(int k=0; k<i.length; k++)
System.out.print(i[k]) ;
这里的sort()是一种快速排序。(参考 Core Java Volume I --ch3--Array)
import java.util.Arrays;public class Test
{
public static void main(String[] args)
{
Random rd = new Random() ; //随机数对象
int[] num = new int[10] ;
System.out.println("排序前:");
for(int i=0; i<num.length; i++)
{
num[i] = rd.nextInt(150) ;
System.out.print(num[i]+"\t");
}
System.out.println("\n排序后:");
Arrays.sort(num);
int[] num2 = new int[num.length] ; //重新构造一个数组
for (int i = num.length-1,j=0; i>=0; i--,j++)
{
num2[j] = num[i] ;
System.out.print(num[i]+"\t");
}
}
}
sort1(a, 0, a.length);
}
private static void sort1(long 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 >> 1); // 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
}
long v = x[m]; // Establish Invariant: v* (<v)* (>v)* v*
int a = off, b = a, c = off + len - 1, d = c;
while(true) {
while (b <= c && x[b] <= v) {
if (x[b] == v)
swap(x, a++, b);
b++;
}
while (c >= b && x[c] >= v) {
if (x[c] == v)
swap(x, c, d--);
c--;
}
if (b > c)
break;
swap(x, b++, c--);
} // Swap partition elements back to middle
int s, n = off + len;
s = Math.min(a-off, b-a ); vecswap(x, off, b-s, s);
s = Math.min(d-c, n-d-1); vecswap(x, b, n-s, s); // Recursively sort non-partition-elements
if ((s = b-a) > 1)
sort1(x, off, s);
if ((s = d-c) > 1)
sort1(x, n-s, s);
} /**
* Swaps x[a] with x[b].
*/
private static void swap(long x[], int a, int b) {
long t = x[a];
x[a] = x[b];
x[b] = t;
} /**
* Swaps x[a .. (a+n-1)] with x[b .. (b+n-1)].
*/
private static void vecswap(long x[], int a, int b, int n) {
for (int i=0; i<n; i++, a++, b++)
swap(x, a, b);
} /**
* Returns the index of the median of the three indexed longs.
*/
private static int med3(long x[], int a, int b, int c) {
return (x[a] < x[b] ?
(x[b] < x[c] ? b : x[a] < x[c] ? c : a) :
(x[b] > x[c] ? b : x[a] > x[c] ? c : a));
}
谢谢
Collection 可以调用。
不过这对你是很有帮助的不过,如果楼主只是为应负考试或考查什么的,不想学java,那还是继续在这找高手帮你吧,哈哈
不过这对你是很有帮助的不过,如果楼主只是为应负考试或考查什么的,不想学java,那还是继续在这找高手帮你吧,哈哈UPUP!