java线程实现排序_【多线程实现快速排序】

快速排序算法实现文件QuickSort.java

package quick.sort;

import java.util.concurrent.Callable;

import java.util.concurrent.locks.Lock;

import java.util.concurrent.locks.ReentrantLock;

public class QuickSort implements Callable{

private int[] array;

private final int start;

private final int end;

private int middle;

Lock lock = new ReentrantLock();

public QuickSort(int start,int end,int[] array){

this.start = start;

this.end = end;

this.array = array;

}

@Override

public Point call() {

/*进行排序*/

quickSort();

return new Point(start,end,middle);

}

public void quickSort(){

int head,tail;

head = start;

tail = end + 1;

while(true){

/*找到一个比head大的*/

do{

head++;

}while(!(array[head] >= array[start] || head == end));

/*找到一个比head小的*/

do{

tail--;

}while(!(array[start] >= array[tail] || tail == start));

if(head < tail)

swap(head,tail);

else

break;

}

swap(start,tail);

middle = tail;

}

public void swap(int a,int b){

int temp = 0;

temp = array[a];

array[a] = array[b];

array[b] = temp;

}

}

class Point{

private int start;

private int end;

private int middle;

public Point(int start,int end,int middle){

this.start = start;

this.end = end;

this.middle = middle;

}

public int getStart(){

return start;

}

public int getEnd(){

return end;

}

public int getMiddle(){

return middle;

}

public boolean compareStartMiddle(){

return start < middle - 1;

}

public boolean compareMiddleEnd(){

return middle + 1 < end;

}

}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值