java编写快速排序的代码
时间: 2023-03-14 13:13:54 浏览: 58
public class QuickSort {
public static void sort(int[] nums){
quickSort(nums, 0, nums.length-1);
}
public static void quickSort(int[] nums, int low, int high){
if(low < high){
int partitionIndex = partition(nums, low, high);
quickSort(nums, low, partitionIndex-1);
quickSort(nums, partitionIndex+1, high);
}
}
public static int partition(int[] nums, int low, int high){
int pivot = nums[high];
int i = low;
for(int j = low; j < high; j++){
if(nums[j] < pivot){
int temp = nums[i];
nums[i] = nums[j];
nums[j] = temp;
i++;
}
}
int temp = nums[i];
nums[i] = nums[high];
nums[high] = temp;
return i;
}
} 使用Java编写快速排序的代码如下:public class QuickSort {
public static void sort(int[] nums){
quickSort(nums, 0, nums.length-1);
}
public static void quickSort(int[] nums, int low, int high){
if(low < high){
int partitionIndex = partition(nums, low, high);
quickSort(nums, low, partitionIndex-1);
quickSort(nums, partitionIndex+1, high);
}
}
public static int partition(int[] nums, int low, int high){
int pivot = nums[high];
int i = low;
for(int j = low; j < high; j++){
if(nums[j] < pivot){
int temp = nums[i];
nums[i] = nums[j];
nums[j] = temp;
i++;
}
}
int temp = nums[i];
nums[i] = nums[high];
nums[high] = temp;
return i;
}
}
相关推荐
![rar](https://img-home.csdnimg.cn/images/20210720083606.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![-](https://csdnimg.cn/download_wenku/file_type_column_c1.png)
![-](https://csdnimg.cn/download_wenku/file_type_column_c1.png)
![-](https://csdnimg.cn/download_wenku/file_type_lunwen.png)
![-](https://csdnimg.cn/download_wenku/file_type_column_c1.png)
![-](https://csdnimg.cn/download_wenku/file_type_column_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)