关于java数组越界的问题,请问该怎么修改 5
publicclassh{publicstaticvoidmerg(int[]a,intlow,inthigh){intmid=(high+low)/2;if(high-...
public class h {
public static void merg(int[] a,int low,int high){
int mid = (high + low)/2;
if(high-low<=1){
return;
}
else{
merg(a,low,mid);
merg(a,mid,high);
mrg(a,low,mid,high);
}
}
public static void mrg(int[] a,int low,int mid,int high){
int[] tmp=new int[high-low+1];
int i=low;
int j=mid+1;
int k=0;
while(i<=mid&&j<=high){
if(a[i]<a[j]){
tmp[k++]=a[i++];
}
if(a[i]>=a[j]){
tmp[k++]=a[j++];
}
while(i<=mid){
tmp[k++]=a[i++];
}
while(j<=high){
tmp[k++]=a[j++];
}
}
for(int l=0;l<a.length ;l++){
a[l+low]=tmp[l];
}
}
public static void main(String[] args){
int a[]={51,46,20,18,65,97,82,30,77,50};
merg(a,0,a.length-1);
for(int l=0;l<a.length ;l++){
System.out.println("排序结果:" + a[l]);
}
}
} 展开
public static void merg(int[] a,int low,int high){
int mid = (high + low)/2;
if(high-low<=1){
return;
}
else{
merg(a,low,mid);
merg(a,mid,high);
mrg(a,low,mid,high);
}
}
public static void mrg(int[] a,int low,int mid,int high){
int[] tmp=new int[high-low+1];
int i=low;
int j=mid+1;
int k=0;
while(i<=mid&&j<=high){
if(a[i]<a[j]){
tmp[k++]=a[i++];
}
if(a[i]>=a[j]){
tmp[k++]=a[j++];
}
while(i<=mid){
tmp[k++]=a[i++];
}
while(j<=high){
tmp[k++]=a[j++];
}
}
for(int l=0;l<a.length ;l++){
a[l+low]=tmp[l];
}
}
public static void main(String[] args){
int a[]={51,46,20,18,65,97,82,30,77,50};
merg(a,0,a.length-1);
for(int l=0;l<a.length ;l++){
System.out.println("排序结果:" + a[l]);
}
}
} 展开
1个回答
展开全部
//用这个,你这写的好凌乱...
public class H {
public static void quicksort(int[] arr) {
int start=0;
int end=arr.length-1;
quickSort(arr,start,end);
}
// 快速排序
public static void quickSort(int[] arr, int start, int end) {
if (start < end) {
int base = arr[start]; // 选定的基准值(第一个数值作为基准值)
int temp; // 记录临时中间值
int i = start, j = end;
do {
while ((arr[i] < base) && (i < end))
i++;
while ((arr[j] > base) && (j > start))
j--;
if (i <= j) {
temp = arr[i];
arr[i] = arr[j];
arr[j] = temp;
i++;
j--;
}
} while (i <= j);
if (start < j)
quickSort(arr, start, j);
if (end > i)
quickSort(arr, i, end);
}
}
public static void main(String[] args) {
int a[] = { 51, 46, 20, 18, 65, 97, 82, 30, 77, 50 };
quicksort(a);
for (int l = 0; l < a.length; l++) {
System.out.println("排序结果:" + a[l]);
}
}
}
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询