java的快速排序怎么写
时间: 2023-09-19 16:06:31 浏览: 57
### 回答1:
Java的快速排序代码如下:
```
import java.util.Arrays;
public class QuickSort {
public static void sort(int[] array, int low, int high) {
if (array == null || array.length == 0) {
return;
}
if (low >= high) {
return;
}
int middle = low + (high - low) / 2;
int pivot = array[middle];
int i = low, j = high;
while (i <= j) {
while (array[i] < pivot) {
i++;
}
while (array[j] > pivot) {
j--;
}
if (i <= j) {
int temp = array[i];
array[i] = array[j];
array[j] = temp;
i++;
j--;
}
}
if (low < j) {
sort(array, low, j);
}
if (high > i) {
sort(array, i, high);
}
}
public static void main(String[] args) {
int[] array = new int[]{5, 2, 9, 1, 5, 6};
sort(array, 0, array.length - 1);
System.out.println(Arrays.toString(array));
}
}
```
上面的代码是Java的快速排序的一种实现方式。
### 回答2:
快速排序是一种常见的排序算法,它的基本思想是选取一个基准元素,将数组划分为两个子数组,使得左边的元素都小于等于基准元素,右边的元素都大于等于基准元素,然后递归地对子数组进行快速排序。
在Java中,可以通过以下代码实现快速排序:
```java
public class QuickSort {
public static void quickSort(int[] arr, int low, int high) {
if (low < high) {
int partitionIndex = partition(arr, low, high);
quickSort(arr, low, partitionIndex - 1);
quickSort(arr, partitionIndex + 1, high);
}
}
private static int partition(int[] arr, int low, int high) {
// 选取最右边的元素作为基准元素
int pivot = arr[high];
int i = low - 1;
for (int j = low; j < high; j++) {
if (arr[j] <= pivot) {
i++;
swap(arr, i, j);
}
}
swap(arr, i + 1, high);
return i + 1;
}
private static void swap(int[] arr, int i, int j) {
int temp = arr[i];
arr[i] = arr[j];
arr[j] = temp;
}
public static void main(String[] args) {
int[] arr = { 9, 5, 2, 8, 3, 7, 1, 6, 4 };
int n = arr.length;
quickSort(arr, 0, n - 1);
System.out.println("排序后的数组:");
for (int i = 0; i < n; i++) {
System.out.print(arr[i] + " ");
}
}
}
```
上述代码通过递归地调用`quickSort`函数实现快速排序,`partition`函数用于进行划分操作,具体步骤如下:
1. 选取最右边的元素作为基准元素`pivot`;
2. 使用指针`i`指向划分的位置,初始为`low - 1`;
3. 遍历数组,若当前元素小于等于`pivot`,则将其与`i`指向的元素交换,并将`i`向右移动一位;
4. 最后将`pivot`与`i + 1`指向的元素交换,返回`i + 1`作为划分的位置。
在示例中,给定数组`arr`为`{ 9, 5, 2, 8, 3, 7, 1, 6, 4 }`,输出的排序结果为`1 2 3 4 5 6 7 8 9`。
### 回答3:
Java的快速排序算法可以通过以下步骤实现:
1. 选取一个基准元素(pivot),可以选择数组的第一个或最后一个元素,也可以随机选择。这里我们选择数组的第一个元素作为基准。
2. 将数组分为两部分,将小于基准的元素放在基准的左边,大于基准的元素放在基准的右边,基准元素被放在了正确的位置。这个过程被称为分区(partition)。
3. 递归地对基准元素左边的子数组和右边的子数组进行步骤1和2,直到每个子数组只包含一个元素或为空。
下面是一个示例代码:
```
// 快速排序的入口函数
public void quickSort(int[] array) {
if (array == null || array.length == 0) {
return;
}
quickSort(array, 0, array.length - 1);
}
// 快速排序的具体实现
private void quickSort(int[] array, int start, int end) {
if (start >= end) {
return;
}
int pivotIndex = partition(array, start, end); // 对数组进行分区,获取基准元素的索引
quickSort(array, start, pivotIndex - 1); // 递归对基准元素左边的子数组进行排序
quickSort(array, pivotIndex + 1, end); // 递归对基准元素右边的子数组进行排序
}
// 数组分区函数
private int partition(int[] array, int start, int end) {
int pivot = array[start]; // 取第一个元素作为基准元素
int left = start + 1; // 左指针
int right = end; // 右指针
while (true) {
// 从左往右找到第一个大于等于基准的值
while (left <= right && array[left] < pivot) {
left++;
}
// 从右往左找到第一个小于等于基准的值
while (right >= left && array[right] > pivot) {
right--;
}
// 如果左指针和右指针相遇,则结束循环
if (left >= right) {
break;
}
// 交换左指针和右指针指向的元素
int temp = array[left];
array[left] = array[right];
array[right] = temp;
left++;
right--;
}
// 将基准元素放在正确的位置
int temp = array[start];
array[start] = array[right];
array[right] = temp;
return right;
}
```
这段代码实现了一个基础的快速排序算法。在实际应用中,还可以进行一些优化,例如选择更合适的基准元素、处理数组中存在重复元素的情况等。
相关推荐
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.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)
![doc](https://img-home.csdnimg.cn/images/20210720083327.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![java](https://img-home.csdnimg.cn/images/20210720083646.png)
![docx](https://img-home.csdnimg.cn/images/20210720083331.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![rar](https://img-home.csdnimg.cn/images/20210720083606.png)