快速排序算法
时间: 2023-06-21 22:21:42 浏览: 76
快速排序是一种经典的排序算法,它是基于分治思想的。该算法的基本思想是:选取一个基准元素,将序列中小于基准元素的数放在左边,大于基准元素的数放在右边,然后对左右两个子序列递归地进行快速排序,直到整个序列有序为止。
快速排序的具体步骤如下:
1. 选择一个基准元素,一般选择第一个元素作为基准元素。
2. 从序列的右端开始向左扫描,找到第一个比基准元素小的元素,将其与基准元素交换。
3. 从序列的左端开始向右扫描,找到第一个比基准元素大的元素,将其与基准元素交换。
4. 重复步骤2和3,直到左右指针相遇。
5. 将基准元素与相遇位置的元素交换。
6. 对左右两个子序列递归地进行快速排序。
以下是快速排序的 Python 实现代码:
```python
def quick_sort(arr):
if len(arr) <= 1:
return arr
pivot = arr[0]
left = []
right = []
for i in range(1, len(arr)):
if arr[i] < pivot:
left.append(arr[i])
else:
right.append(arr[i])
return quick_sort(left) + [pivot] + quick_sort(right)
```
快速排序的时间复杂度为 O(nlogn),空间复杂度为 O(logn)。
相关问题
JAVA快速排序算法
快速排序是一种常用的排序算法,它的基本思想是通过一趟排序将待排序的数据分割成独立的两部分,其中一部分的所有数据都比另一部分的所有数据小,然后再按此方法对这两部分数据分别进行快速排序,整个排序过程可以递归进行,以此达到整个数据变成有序序列。
下面是JAVA实现快速排序算法的示例代码:
```java
public class QuickSort {
public static void quickSort(int[] arr, int low, int high) {
if (low < high) {
int pivot = partition(arr, low, high); // 划分数组
quickSort(arr, low, pivot - 1); // 对左子数组进行快速排序
quickSort(arr, pivot + 1, high); // 对右子数组进行快速排序
}
}
public static int partition(int[] arr, int low, int high) {
int pivot = arr[low]; // 选取第一个元素作为基准值
while (low < high) {
while (low < high && arr[high] >= pivot) {
high--;
}
arr[low] = arr[high]; // 将比基准值小的元素移到低端
while (low < high && arr[low] <= pivot) {
low++;
}
arr[high] = arr[low]; // 将比基准值大的元素移到高端
}
arr[low] = pivot; // 基准值归位
return low; // 返回基准值的位置
}
public static void main(String[] args) {
int[] arr = {5, 2, 8, 9, 1, 3};
quickSort(arr, 0, arr.length - 1);
for (int num : arr) {
System.out.print(num + " ");
}
}
}
```
以上代码实现了快速排序算法,通过递归调用`quickSort`函数对数组进行划分和排序,`partition`函数用于划分数组并返回基准值的位置。最后在`main`函数中调用`quickSort`函数对数组进行排序,并输出排序结果。
快速排序算法java
快速排序是一种采用分治法解决问题的典型应用,也是冒泡排序的一种改进。它的基本思想是通过一轮排序将待排记录分割成独立的两部分,其中一部分均比另一部分小,则可分别对这两部分继续进行排序,已达到整个序列有序。下面是一个使用Java实现的快速排序算法:
```java
public static int partition(int[] array, int left, int right) {
int pivot = array[left];
while (left < right) {
while (left < right && array[right] >= pivot) {
right--;
}
array[left] = array[right];
while (left < right && array[left] <= pivot) {
left++;
}
array[right] = array[left];
}
array[left] = pivot;
return left;
}
public static void quickSort(int[] array) {
quick(array, 0, array.length - 1);
}
public static void quick(int[] array, int start, int end) {
if (start >= end) {
return;
}
int pivot = partition(array, start, end);
quick(array, start, pivot - 1);
quick(array, pivot + 1, end);
}
```
这是一种基于Hoare划分方式的快速排序算法。它首先选择一个枢轴元素,然后将数组分割为两个部分,一部分包含所有小于枢轴元素的值,另一部分包含所有大于枢轴元素的值。然后,递归地对这两部分进行排序,最终得到正确的排序结果。
相关推荐
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![doc](https://img-home.csdnimg.cn/images/20210720083327.png)
![docx](https://img-home.csdnimg.cn/images/20210720083331.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)
![](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)