这个好象是快速排序吧,选取一个值,然后大的放一边 ,小的放另一边。。
不明白PivotIndex是在做什么, 他想得到怎样的值?
一开始我以为是first 和 last的中间值,肯定不对的,那根本没必要这么复杂。public class Sorts { // 交换位置
private void Swrap(int[] v, int index1, int index2) {
int temp = v[index1];
v[index1] = v[index2];
v[index2] = temp;
}  // 将向量V中索引{first,last)划分成两个 左子表和右子表
 // <param name="first">开始位置</param>
 // <param name="last">结束位置</param>
//这一段是在干嘛? 选取轴值吗?
//为什么要这样选取? 
//他最后是要返回一个怎样的值?
private int PivotIndex(int[] v, int first, int last) {
if (last == first) {
return last;
}
  if (last - first == 1) {
return first;
}
int mid = (first + last) / 2;
int midVal = v[mid];

// 交换v[first]和v[mid]
Swrap(v, first, mid);
int scanA = first + 1;// 第2个
int scanB = last - 1;// 倒数第1 个 while (true) {
while (scanA < scanB && v[scanA] < midVal) {
scanA++;
}
while (scanB > first && midVal <= v[scanB]) {
scanB--;
}
if (scanA >= scanB)
break; Swrap(v, scanA, scanB);
scanA++;
scanB--; }
Swrap(v, first, scanB);
return scanB; } void sort(int[] v, int first, int last, int k) {
// 表示分表中值的索引
int index = 0;
index = PivotIndex(v, first, last);
if (index == k) {
output(v);
return;
} if (index > k) {
// 只在左子表中查找
sort(v, first, index, k);
} else if (index <= k) {
// 只在右子表中查找
sort(v, index, last, k);
}
} void sort(int[] v, int first, int last) {
int k = 1;// 选取轴值
sort(v, first, last, k);
} void output(int[] v) {
for (int i : v) {
System.out.print(i + " ");
}
} public static void main(String[] args) {
Sorts find = new Sorts();
int[] v = new int[] {6,5,4,3,2,1 };
find.sort(v, 0, v.length);
for (int i : v) {
System.out.print(i + " ");
}
}
}