排序sort

public class alg{


public static int count=0;
public static int t=0;
//====================quicksort======================
public static void  quicksort(int []a,int left,int right){
int l = left;
int r = right;
int pivot = a[(l+r)/2];
int temp = 0;
while (l < r) {
while(a[l] < pivot) l++;
while(a[r] > pivot) r--;


if (l >= r) break;
temp = a[l];
a[l] = a[r];
a[r] = temp;


if(a[l] == pivot) --r;
if(a[r] == pivot) ++l;
}
if (l == r) {
l++;
r--;
}
if(left < r) quicksort(a,left,r);
if(right > l) quicksort(a,l,right);
}




//=======================qsort=======================
public static void qsort(int a[],int left,int right){
int index = partition(a,left,right);
display(a);
if (left < index-1) {
qsort(a,left,index-1);
}
if (index < right) {
qsort(a,index,right);
}

}


public static int partition(int a[],int left,int right){
int pivot = a[(left+right)/2];
while(left <= right){
while(a[left] < pivot) left++;
while(a[right] > pivot) right--;
if (left <= right) {
//swap(a[left],a[right])
int temp = a[left];
a[left] = a[right];
a[right] = temp;
left++;
right--;

}
}
return left;
}


//====================mergesort======================
public static void mergesort(int a[],int temp[],int low,int high){
if (low < high) {
int middle = (low + high)/2;
mergesort(a,temp,low,middle);
mergesort(a,temp,middle+1,high);
merge(a,temp,low,middle,high);
}
}


public static void merge(int a[],int temp[],int low,int middle,int high){
int l=low;
int r=middle+1;
int cur = 0;
while(l <= middle && r <= high){
if(a[l] > a[r]){
temp[cur++] = a[r++];
}else{
temp[cur++] = a[l++];
}
}
while(l <= middle){
temp[cur++] = a[l++];
}
while(r <= high){
temp[cur++] = a[r++];
}
for (int i=0; i < cur; i++) {
a[low+i] = temp[i];
}
}


public static void Mergesort(int a[]){
if (a !=null ) {
int []temp = new int[a.length];
mergesort(a,temp,0,a.length-1);
}
}


//=====================mergeArray=====================
public static void mergeArray(int a[],int b[],int c[]){
int i,j,k;
i=j=k=0;
while(i < a.length && j < b.length){
if(a[i] < b[j]){
c[k++] = a[i++];
}
else{
c[k++] = b[j++];
}
}
while(i < a.length){
c[k++]=a[i++];
}
while(j < b.length){
c[k++]=b[j++];
}


}



//======================BinarySearch====================
public static int BinarySearch(int[] a, int x){
int low=0;
int high=a.length-1;
int mid;
while(low <= high){
mid = (low+high)/2;
if (a[mid] < x) {
low = mid+1;
}else if (a[mid] > x) {
high = mid;
}else{
return mid;
}
}
return -1;
}


public static void display(int a[]){
count++;
System.out.print("NO . "+ count+"\t");
for(int i = 0;i < a.length ; i++){
if (i%10 == 0 && i > 0) {
System.out.println();
}
System.out.print(a[i]+ "\t");
}
System.out.println("\n");
}


public static void main(String[] args) {
int a[] = new int[8];
for (int i = 0; i < a.length; i++){
a[i] = (int)(Math.random()*100);
}
display(a);


//Mergesort(a);
/*for(int i = 0;i < a.length ; i++){
System.out.print(a[i]+ "\t"); 
if (i/10 == 0) {
System.out.println();
}
}
System.out.println();*/
qsort(a,0,a.length-1);

//System.out.println(BinarySearch(a,5));
}
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值