java的快速排序怎么写?
时间: 2023-09-12 18:05:47 浏览: 56
### 回答1:
Java中的快速排序可以通过递归实现。以下是一种常见的实现方式:
```java
public static void quickSort(int[] arr, int left, int right) {
if (left < right) {
int pivotIndex = partition(arr, left, right);
quickSort(arr, left, pivotIndex - 1);
quickSort(arr, pivotIndex + 1, right);
}
}
private static int partition(int[] arr, int left, int right) {
int pivot = arr[left];
int i = left + 1;
int j = right;
while (i <= j) {
while (i <= j && arr[i] < pivot) {
i++;
}
while (i <= j && arr[j] > pivot) {
j--;
}
if (i <= j) {
swap(arr, i, j);
i++;
j--;
}
}
swap(arr, left, j);
return j;
}
private static void swap(int[] arr, int i, int j) {
int temp = arr[i];
arr[i] = arr[j];
arr[j] = temp;
}
```
在该实现中,`quickSort`方法用于对数组进行排序。它接受一个整数数组`arr`,以及数组的左右边界`left`和`right`。该方法使用`partition`方法确定一个主元(pivot),并将数组划分为小于主元和大于主元的两个子数组。然后,递归地对这两个子数组进行排序,直到排序完成。`partition`方法使用双指针法进行划分。它从左右两端开始遍历数组,交换小于主元的元素和大于主元的元素,直到指针相遇。最后,它将主元与指针相遇的位置交换,并返回该位置。
使用示例:
```java
int[] arr = {5, 2, 9, 3, 6, 8, 1, 7};
quickSort(arr, 0, arr.length - 1);
System.out.println(Arrays.toString(arr));
```
输出结果为:
```
[1, 2, 3, 5, 6, 7, 8, 9]
```
### 回答2:
快速排序是一种常用的排序算法,它通过选取一个基准元素,将序列划分为小于基准元素和大于基准元素两个子序列,然后递归地对子序列进行排序。以下是Java中的快速排序代码示例:
```java
public class QuickSort {
public static void quickSort(int[] arr, int low, int high) {
if (low < high) {
int pivotIndex = partition(arr, low, high); // 将数组划分为两个子序列
quickSort(arr, low, pivotIndex - 1); // 对左半部分进行递归排序
quickSort(arr, pivotIndex + 1, high); // 对右半部分进行递归排序
}
}
public static int partition(int[] arr, int low, int high) {
int pivot = arr[low]; // 选择第一个元素作为基准元素
int i = low, j = high;
while (i < j) {
while (i < j && arr[j] >= pivot) {
j--;
}
if (i < j) {
arr[i++] = arr[j];
}
while (i < j && arr[i] <= pivot) {
i++;
}
if (i < j) {
arr[j--] = arr[i];
}
}
arr[i] = pivot; // 基准元素归位
return i;
}
public static void main(String[] args) {
int[] arr = {6, 2, 8, 3, 1, 9};
quickSort(arr, 0, arr.length - 1);
for (int num : arr) {
System.out.print(num + " ");
}
}
}
```
上述代码实现了快速排序算法,首先定义了一个`quickSort`方法,在该方法中通过递归实现了快速排序的过程。在`quickSort`方法中调用了`partition`方法,该方法用于划分数组并返回基准元素的下标。
`partition`方法选取第一个元素作为基准元素,通过双指针`i`和`j`,将小于基准元素的放在左边,大于基准元素的放在右边,最后将基准元素放在正确的位置上。
在示例代码中,通过`main`方法测试了快速排序的功能,输出排序后的数组。
以上就是使用Java实现快速排序的代码示例。
### 回答3:
快速排序(Quick Sort)是一种经典的排序算法,在Java中可以通过递归来实现。
快速排序的思想是选择一个基准元素,然后将数组分为两部分,一部分是所有小于等于基准元素的元素,另一部分是所有大于基准元素的元素。然后对这两部分分别进行快速排序。
具体的实现步骤如下:
1. 首先,选择数组中的一个元素作为基准元素。可以选择第一个元素或者是随机选择一个元素作为基准元素。
2. 创建两个指针,分别指向数组的起始位置和结束位置。
3. 使用分治法的思想,将数组划分为两部分。根据基准元素,将小于等于基准元素的元素放到左边,大于基准元素的元素放到右边。
4. 递归地对左侧部分和右侧部分进行快速排序。即重复步骤2和步骤3,直到排序完成。
以下是用Java代码实现的快速排序算法的示例:
```java
public class QuickSort {
public static void quickSort(int[] array, int low, int high) {
if (low < high) {
int pivotIndex = partition(array, low, high);
quickSort(array, low, pivotIndex - 1);
quickSort(array, pivotIndex + 1, high);
}
}
public static int partition(int[] array, int low, int high) {
int pivot = array[low];
int left = low + 1;
int right = high;
while (left <= right) {
while (left <= right && array[left] <= pivot) {
left++;
}
while (left <= right && array[right] > pivot) {
right--;
}
if (left < right) {
int temp = array[left];
array[left] = array[right];
array[right] = temp;
}
}
int temp = array[low];
array[low] = array[right];
array[right] = temp;
return right;
}
public static void main(String[] args) {
int[] array = {5, 2, 9, 3, 7, 6, 1, 8, 4};
quickSort(array, 0, array.length - 1);
for (int num : array) {
System.out.print(num + " ");
}
}
}
```
以上就是Java中快速排序的实现方法。快速排序是一种高效的排序算法,平均时间复杂度为O(nlogn),最坏情况下的时间复杂度为O(n^2)。
相关推荐
![pdf](https://img-home.csdnimg.cn/images/20210720083512.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_column_c1.png)
![-](https://csdnimg.cn/download_wenku/file_type_column_c1.png)
![-](https://csdnimg.cn/download_wenku/file_type_column_c1.png)
![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_column_c1.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_column_c1.png)
![-](https://csdnimg.cn/download_wenku/file_type_column_c1.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![doc](https://img-home.csdnimg.cn/images/20210720083327.png)