|
快速排序是一种交换类排序,可以理解成对冒泡排序的一种改进排序,但快速排序的复杂度相对于冒泡排序的提升相当大。
他的思路是,选取一个关键字K,将所有比K小的记录放在K前面,比K大的数放在K后面,一趟快速排序完成,完整的快速排序就是对分出的每个新数组再进行一次快速排序,也就是一趟排序的递归操作。
快速排序,分别用C语言,C++,java,python编写出来
下面是快速排序(Quick Sort)的不同语言实现示例:
1. C语言:
- #include <stdio.h>
- void swap(int* a, int* b) {
- int temp = *a;
- *a = *b;
- *b = temp;
- }
- int partition(int arr[], int low, int high) {
- int pivot = arr[high];
- int i = (low - 1);
- for (int j = low; j <= high - 1; j++) {
- if (arr[j] < pivot) {
- i++;
- swap(&arr[i], &arr[j]);
- }
- }
- swap(&arr[i + 1], &arr[high]);
- return (i + 1);
- }
- void quickSort(int arr[], int low, int high) {
- if (low < high) {
- int pi = partition(arr, low, high);
- quickSort(arr, low, pi - 1);
- quickSort(arr, pi + 1, high);
- }
- }
复制代码 int main() {
int arr[] = {64, 34, 25, 12, 22, 11, 90};
int n = sizeof(arr) / sizeof(arr[0]);
quickSort(arr, 0, n - 1);
printf("Sorted array: \n");
for (int i = 0; i < n; i++) {
printf("%d ", arr);
}
return 0;
}
2. C++:
- #include <iostream>
- using namespace std;
- void swap(int& a, int& b) {
- int temp = a;
- a = b;
- b = temp;
- }
- int partition(int arr[], int low, int high) {
- int pivot = arr[high];
- int i = (low - 1);
- for (int j = low; j <= high - 1; j++) {
- if (arr[j] < pivot) {
- i++;
- swap(arr[i], arr[j]);
- }
- }
- swap(arr[i + 1], arr[high]);
- return (i + 1);
- }
- void quickSort(int arr[], int low, int high) {
- if (low < high) {
- int pi = partition(arr, low, high);
- quickSort(arr, low, pi - 1);
- quickSort(arr, pi + 1, high);
- }
- }
复制代码
int main() {
int arr[] = {64, 34, 25, 12, 22, 11, 90};
int n = sizeof(arr) / sizeof(arr[0]);
quickSort(arr, 0, n - 1);
cout << "Sorted array: \n";
for (int i = 0; i < n; i++) {
cout << arr << " ";
}
return 0;
}
3. Java:
- import java.util.Arrays;
- class QuickSort {
- public static void swap(int[] arr, int i, int j) {
- int temp = arr[i];
- arr[i] = arr[j];
- arr[j] = temp;
- }
- public static int partition(int[] arr, int low, int high) {
- int pivot = arr[high];
- int i = (low - 1);
- for (int j = low; j <= high - 1; j++) {
- if (arr[j] < pivot) {
- i++;
- swap(arr, i, j);
- }
- }
- swap(arr, i + 1, high);
- return (i + 1);
- }
- public static void quickSort(int[] arr, int low, int high) {
- if (low < high) {
- int pi = partition(arr, low, high);
- quickSort(arr, low, pi - 1);
- quickSort(arr, pi + 1, high);
- }
- }
-
- }
复制代码
public static void main(String[] args) {
int[] arr = {64, 34, 25, 12, 22, 11, 90};
int n = arr.length;
quickSort(arr, 0, n - 1);
System.out.println("Sorted array: ");
for (int i = 0; i < n; i++) {
System.out.print(arr + " ");
}
}
4. Python:
- def partition(arr, low, high):
- pivot = arr[high]
- i = low - 1
- for j in range(low, high):
- if arr[j] < pivot:
- i += 1
- arr[i], arr[j] = arr[j], arr[i]
- arr[i + 1], arr[high] = arr[high], arr[i + 1]
- return i + 1
- def quickSort(arr, low, high):
- if low < high:
- pi = partition(arr, low, high)
- quickSort(arr, low, pi - 1)
- quickSort(arr, pi + 1, high)
复制代码 arr = [64, 34, 25, 12, 22, 11, 90]
n = len(arr)
quickSort(arr, 0, n - 1)
print("Sorted array:")
for i in range(n):
print(arr, end=" ")
|
|