记录自己理解实现快速排序
1、可运行的实现
public class Quicksort1 {
public static void main(String[] args) {
Random random = new Random();
Entity[] entities = new Entity[60000000];
for (int i = 0;i < 60000000;++i){
Entity entity = new Entity(random.nextInt(600000000));
entities[i] = entity;
}
long start = System.currentTimeMillis();
Quicksort(entities,0,entities.length - 1);
long end = System.currentTimeMillis();
System.out.println("Quicksort :"+(end - start) / 1000);
assert SortUtil.isSorted(entities);
}
private static int partition(Entity[] entities,int left,int right){
boolean right2left = true;
boolean left2right = false;
Entity temp = entities[left];
while (left != right){
if (right2left){//右到左扫描
if (entities[right].compareTo(temp) >= 0){
right--;
}else {
entities[left] = entities[right];
left2right = true;
right2left = false;
continue;
}
}
if (left2right){//左到右扫描
if (entities[left].compareTo(temp) <= 0){
left++;
}else {
entities[right] = entities[left];
right2left = true;
left2right = false;
continue;
}
}
}
//这边得到了temp的正确坑位
entities[left]= temp;
return left;
}
public static void Quicksort(Entity[] entities,int left,int right){
if (left >= right){
return;
}
if (entities == null || entities.length== 0){
return;
}
int partition = partition(entities,left,right);
Quicksort(entities,left,partition-1);//递归的对左边子数组进行 partition 和 左右子数组排序操作
Quicksort(entities,partition + 1,right);//递归的对右边子数组进行 partition 和 左右子数组排序操作
}
}
2、有问题实现会出现栈深度溢出问题但是程序逻辑没问题在此记录(重要事情mark)
public class Quicksort {
public static void main(String[] args) {
Random random = new Random();
Entity[] entities = new Entity[60000000];
for (int i = 0;i < 60000000;++i){
Entity entity = new Entity(random.nextInt(600000000));
entities[i] = entity;
}
long start = System.currentTimeMillis();
Quicksort(entities,0,entities.length - 1);
long end = System.currentTimeMillis();
System.out.println("Quicksort :"+(end - start) / 1000);
assert SortUtil.isSorted(entities);
}
private static int partition(Entity[] entities,int left,int right){
return right2leftScan(entities,left,right,entities[left]);
}
public static void Quicksort(Entity[] entities,int left,int right){
if (left >= right){
return;
}
if (entities == null || entities.length== 0){
return;
}
int partition = partition(entities,left,right);
Quicksort(entities,left,partition-1);
Quicksort(entities,partition + 1,right);
}
private static int right2leftScan(Entity[] entities,int left,int right,Entity temp){
// Entity temp = entities[left];
while (left != right){
Entity righte = entities[right];
if (righte.compareTo(temp) < 0){
entities[left] = entities[right];
return left2rightScan(entities,left,right,temp);
}else if(righte.compareTo(temp) >= 0){
right--;
}
}
entities[left] = temp;
return left;
}
private static int left2rightScan(Entity[] entities,int left,int right,Entity temp){
//Entity temp = entities[left];
while (left != right){
Entity lefte = entities[left];
if (lefte.compareTo(temp) <= 0){
left++;
}else if(lefte.compareTo(temp) >0){
entities[right] = entities[left];
return right2leftScan(entities,left,right,temp);
}
}
entities[left] = temp;
return left;
}
}
3、实体辅助类
public class Entity implements Comparable<Entity> {
private int order;
Entity(int order){
this.order = order;
}
@Override
public int compareTo(Entity o) {
if (o == null){
return 0;
}
return this.order - o.order;
}
public int getOrder() {
return order;
}
public void setOrder(int order) {
this.order = order;
}
}
排序断言代码参考
4、原理参考