这段代码有什么错误,指出并改正 public class test { public static void main(String[] args) { int[] index = {5,4,3,2,1}; quickSort(index,0,index.length-1); for (int j : index) { System.out.print(j + "\t"); } } public static void quickSort(int[] nums,int low,int high){ int i,j,t,temp; i=low; j=high; temp=nums[low]; if(low>high){ return; } while(i<j){ while(i<j&&nums[j]>=temp){ j--; } while(i<j&&nums[i]<=temp){ i++; } if(i<j){ t=nums[i]; nums[i]=nums[j]; nums[j]=t; } nums[low]=nums[i]; nums[i]=temp; quickSort(nums,0,i-1); quickSort(nums,i,j-1); } } }
时间: 2023-05-28 07:03:16 浏览: 83
public class Test.docx
){ while(i<j && nums[j]>=temp){ j--; } if(i<j){ nums[i]=nums[j]; i++; } while(i<j && nums[i]<=temp){ i++; } if(i<j){ nums[j]=nums[i]; j--; } } nums[i]=temp; quickSort(nums,low,i-1); quickSort(nums,i+1,high); } }
错误:在第28行缺少括号,应该是 while(i<j) {
修改后的代码:
public class test {
public static void main(String[] args) {
int[] index = {5,4,3,2,1};
quickSort(index,0,index.length-1);
for (int j : index) {
System.out.print(j + "\t");
}
}
public static void quickSort(int[] nums,int low,int high){
int i,j,t,temp;
i=low;
j=high;
temp=nums[low];
if(low>high){
return;
}
while(i<j){
while(i<j && nums[j]>=temp){
j--;
}
if(i<j){
nums[i]=nums[j];
i++;
}
while(i<j && nums[i]<=temp){
i++;
}
if(i<j){
nums[j]=nums[i];
j--;
}
}
nums[i]=temp;
quickSort(nums,low,i-1);
quickSort(nums,i+1,high);
}
}
阅读全文