//left是数组的想分治部分的左端点,right是数组分治部分的总端点,如长度为10的数组,我想对前5个整数进行分治,则传0,4即可
public int signalFenZhi(int left,int right){
if(left<0||left>n-1||right<0||right>n-1){
return -1;
}
int temp = test[left];
int j=right;
int i=left;
while(i<j){
while(test[j]>=test[left]&&i<j){
j--;
}
while(test[i]<=test[left]&&i<j){
i++;
}
if(i<j){
temp = test[i];
test[i]=test[j];
test[j]=temp;
}
}
if(i==j){
temp = test[i];
test[i]=test[left];
test[left]=temp;
}
for(int m=0;m<n;m++){
System.out.print(test[m]+" ");
}
return i;
}
public void quickSort(int left,int right){
if(right-left<1){
return ;
}else{
int point = this.signalFenZhi(left, right);
System.out.println(point);
//if(point!=left&&point!=right){
quickSort(left,point-1);
quickSort(point+1,right);
//}
}
}
package com.jll;
public class FenZhi {
int[] test;
int n=10;
public FenZhi(){
test = new int[10];
for(int i=0;i<n;i++){
test[i]=(int)(Math.random()*100)+1;
System.out.print(test[i]+" ");
}
System.out.println();
}
public FenZhi(int n){
if(n>0){
this.n=n;
test = new int[n];
for(int i=0;i<n;i++){
test[i]=(int)(Math.random()*100)+1;
}
}
}
public int signalFenZhiMajorizationFirst(int left,int right){
if(left<0||left>n-1||right<0||right>n-1||left>=right){
return -1;
}
if(right-left>=2){
int middle = (right+left)/2;
if(test[left]>test[middle]){
int temp = test[middle];
test[middle] = test[left];
test[left] = temp;
}
if(test[left]>test[right]){
int temp = test[left];
test[left] = test[right];
test[right] = temp;
}
if(test[middle]>test[right]){
int temp = test[middle];
test[middle] = test[right];
test[right] = temp;
}
int temp = test[middle];
test[middle] = test[left];
test[left] = temp;
int j=right-1;
int i=left+1;
while(i<j){
while(test[j]>=test[left]&&i<j){
j--;
}
while(test[i]<=test[left]&&i<j){
i++;
}
if(i<j){
temp = test[i];
test[i]=test[j];
test[j]=temp;
}
}
if(i==j){
temp = test[i];
test[i]=test[left];
test[left]=temp;
}
/*if(i==j){
temp = test[middle];
test[middle]=test[i];
test[i]=temp;
}*/
/*for(int m=0;m<n;m++){
System.out.print(test[m]+" ");
}*/
return i;
}else {
if(test[right]<test[left]){
int temp = test[right];
test[right] = test[left];
test[left] = temp;
}
return right;
}
}
public void quickSortMajorizationFirst(int left,int right){
if(right-left<1){
return ;
}else{
int point = this.signalFenZhiMajorizationFirst(left, right);
System.out.println("the point is:"+point);
quickSortMajorizationFirst(left,point-1);
quickSortMajorizationFirst(point+1,right);
}
}
public static void main(String[] args) {
FenZhi f = new FenZhi();
System.out.println(f.signalFenZhiMajorizationFirst(0, 9));
System.out.println();
f.quickSortMajorizationFirst(0,f.n-1);
//f.quickSort(0,f.test.length-1);
for(int i:f.test){
System.out.print(i+" ");
}
}
}
95 40 64 18 78 23 73 84 40 the point is:4 the point is:1 the point is:3 the point is:7 the point is:6 the point is:9 18 23 40 40 64 73 78 84 95
机械节能产品生产企业官网模板...
大气智能家居家具装修装饰类企业通用网站模板...
礼品公司网站模板
宽屏简约大气婚纱摄影影楼模板...
蓝白WAP手机综合医院类整站源码(独立后台)...苏ICP备2024110244号-2 苏公网安备32050702011978号 增值电信业务经营许可证编号:苏B2-20251499 | Copyright 2018 - 2025 源码网商城 (www.ymwmall.com) 版权所有