先贴代码,有什么进一步的问题等大家回复我了再提public class A {
//这个方法最需要注释,很多变量的名字太短,看不懂用来看嘛的
public 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];//v is the pivot //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, off, s);
} } //Postcondition: the elements of x at indices a and b have been
// swapped.
//这个我懂
private static void swap(int[] x, int a, int b) {
int t = x[a];
x[a] = x[b];
x[b] = t;
} //Postcondition: the elements of x, from indices a through a + n - 1 have
//been swapped with the elements of x from indices b
//through b + n - 1
//不知道这个方法应用在sort1中有什么用?
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);
}
} //Postcondition: the index of the median of x[a],x[b] and x[c] has
//been returned.
//这个我懂,按abc排序,返回中间的那个值
private static int med3(int[] x, int a, int b, int c) {
if (x[a] < x[b]) {
if (x[b] < x[c]) {
return b;
} else {
return x[a] < x[c] ? c : a;
}
} else {
if (x[b] > x[c]) {
return b;
} else {
return x[a] > x[c] ? c : a;
}
}
}
}
//这个方法最需要注释,很多变量的名字太短,看不懂用来看嘛的
public 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];//v is the pivot //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, off, s);
} } //Postcondition: the elements of x at indices a and b have been
// swapped.
//这个我懂
private static void swap(int[] x, int a, int b) {
int t = x[a];
x[a] = x[b];
x[b] = t;
} //Postcondition: the elements of x, from indices a through a + n - 1 have
//been swapped with the elements of x from indices b
//through b + n - 1
//不知道这个方法应用在sort1中有什么用?
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);
}
} //Postcondition: the index of the median of x[a],x[b] and x[c] has
//been returned.
//这个我懂,按abc排序,返回中间的那个值
private static int med3(int[] x, int a, int b, int c) {
if (x[a] < x[b]) {
if (x[b] < x[c]) {
return b;
} else {
return x[a] < x[c] ? c : a;
}
} else {
if (x[b] > x[c]) {
return b;
} else {
return x[a] > x[c] ? c : a;
}
}
}
}
不信你试试
public static void main(String[] args) {
int[] i = new int[]{5,4,6,7,1,2,8,9,10,15,3};
sort1(i, 2, 9);
for (int j=0; j < i.length;j++)
{
System.out.println(i[j]);
}
}
int[] i = new int[]{10,9,8,7,6,5,4,3,2,1,0};
然后分别执行
sort1(i, 0, 0);
sort1(i, 0, 1);
sort1(i, 0, 2);
sort1(i, 0, 3);
sort1(i, 0, 4);
sort1(i, 0, 5);
sort1(i, 0, 6);
sort1(i, 0, 7);
sort1(i, 0, 8);
sort1(i, 0, 9);
sort1(i, 0, 10); //输出的数组不符合要求,其他的调用返回的数组都是对的
sort1(i, 0, 11);
static int MAX = 100000;
static int[] _data = new int[MAX];
public static void main(String[] args) {
Random r = new Random(20080623);
for (int i = 0; i < MAX; i++) {
_data[i] = r.nextInt(MAX);
}
long begin = System.currentTimeMillis();
qSort(0,MAX-1);
long end = System.currentTimeMillis();
System.out.println("consume time is: "+(end - begin)); // 以这个时间为标准,越小越好。
}
static void qSort(int begin, int end)
{
if (begin<end)
{
int q = partition(begin,end);
qSort(begin,q-1);
qSort(q+1,end);
}
}
static int partition(int begin, int end)
{
int i = begin;
int j = end+1;
int firstdata = _data[begin];
while (true)
{
while (i<end&&(_data[++i]<firstdata));
while (j>0&&(_data[--j]>firstdata));
if (i>=j)
{
break;
}
int temp;
temp = _data[i];
_data[i] = _data[j];
_data[j] = temp;
}
_data[begin] = _data[j];
_data[j] = firstdata;
return j;
}
static void randomSort(int begin,int end)
{
if (begin<end)
{
int q = randomPartition(begin,end);
randomPartition(begin,q-1);
randomPartition(q+1,end);
}
} static int randomPartition(int begin,int end)
{
Random r = new Random(end+1);
int i = begin + r.nextInt();
System.out.println(i);
int temp;
temp = _data[i];
_data[i] = _data[begin];
_data[begin] = temp;
return partition(begin,end);
}}
不信你换成10试试
public static void main(String[] args) {
int[] i = new int[]{5,4,6,7,1,2,8,9,10,15,3};
sort1(i, 2, 10);
for (int j=0; j < i.length;j++)
{
System.out.println(i[j]);
}
}