快速排序的模板代码

#include<stdio.h>
#define maxn 1000001
int a[maxn];
int n;

void init()
{
	scanf("%d %d", &n);
	for(int i = 0; i < n; i++)
		scanf("%d", &a[i]);
}

void out()
{
	for(int i = 0; i < n; i++)
		printf("%d ", a[i]);
}

void swap(int& a, int& b)
{
	int t = a; a = b; b = t;
}
void quicksort(int a[], int l, int r)
{
	int i = l, j = r;
	swap(a[l], a[(l+r)/2]); //不直接选用a[l],是避免初始数组有序而退化成O(n^2)
	do{
		if(j > i)
		{
			while(a[j] > a[i]) j--;
			swap(a[i], a[j]);
			i++;
		}
		if(i < j)
		{
			while(a[i] < a[j]) i++;
			swap(a[i], a[j]);
			j--;
		}
	}while(i < j);

	//[l,j]中的值都比[i,r]中的值小了
	if(l < j) quicksort(a, l, j);  //排序左子数组
	if(i < r) quicksort(a, i, r); //排序右子数组
}

int main()
{
	init();
	quicksort(a, 0, n-1); //对int类型的a[0]~a[n-1]进行升序排序
	out();
	return 0;
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
### 回答1: 好的,以下是使用C语言编写的快速排序模板: ```c #include <stdio.h> void quicksort(int arr[], int low, int high) { int i = low, j = high; int pivot = arr[(low + high) / 2]; while (i <= j) { while (arr[i] < pivot) { i++; } while (arr[j] > pivot) { j--; } if (i <= j) { int temp = arr[i]; arr[i] = arr[j]; arr[j] = temp; i++; j--; } } if (low < j) { quicksort(arr, low, j); } if (i < high) { quicksort(arr, i, high); } } int main() { int arr[] = { 9, 2, 5, 7, 1, 6, 8, 3, 10, 4 }; int n = sizeof(arr) / sizeof(arr[0]); quicksort(arr, 0, n - 1); printf("Sorted array: "); for (int i = 0; i < n; i++) { printf("%d ", arr[i]); } printf("\n"); return 0; } ``` 以上代码中,使用了快速排序算法来对一个整型数组进行排序。其中,`quicksort` 函数是递归调用的,通过比较数组中的元素与一个主元(即“枢轴元素”或“基准元素”)来实现排序。 ### 回答2: 快速排序是一种常用的排序算法,通过递归地将数组分成较小的子数组,然后分别对子数组进行排序,直到整个数组有序。 以下是用C语言写的快速排序模板: ```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 pivotIndex = partition(arr, low, high); quickSort(arr, low, pivotIndex - 1); quickSort(arr, pivotIndex + 1, high); } } int main() { int arr[] = {64, 34, 25, 12, 22, 11, 90}; int n = sizeof(arr) / sizeof(arr[0]); printf("原始数组:"); for (int i = 0; i < n; i++) { printf("%d ", arr[i]); } quickSort(arr, 0, n - 1); printf("\n排序后的数组:"); for (int i = 0; i < n; i++) { printf("%d ", arr[i]); } return 0; } ``` 这个模板中包含一个`swap`函数,用于交换两个元素的位置。`partition`函数用于选择一个主元(pivot),并按照主元将数组分成两个部分。`quickSort`函数通过递归调用自身,对分割后的子数组进行排序。最后,在`main`函数中,我们定义了一个待排序的数组,并调用`quickSort`函数进行排序。我们可以看到,输出的结果是将数组升序排列。 ### 回答3: 快速排序是一种常用的排序算法,它的基本思想是选择一个元素作为基准值,通过一趟排序将待排序序列分割成独立的两部分,其中一部分的所有元素都比基准值小,另一部分的所有元素都比基准值大。然后对这两部分继续进行快速排序,直到整个序列有序。 以下是用C语言实现快速排序模板: ```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[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; } void quickSort(int arr[], int low, int high) { if (low < high) { int pivot_pos = partition(arr, low, high); quickSort(arr, low, pivot_pos - 1); quickSort(arr, pivot_pos + 1, high); } } int main() { int arr[] = {5, 2, 8, 3, 1, 9, 4, 6, 7}; int n = sizeof(arr) / sizeof(arr[0]); quickSort(arr, 0, n - 1); printf("排序后的数组:"); for (int i = 0; i < n; i++) printf("%d ", arr[i]); return 0; } ``` 上述代码实现了快速排序模板,其中swap函数用于交换两个元素的值,partition函数用于通过一趟排序将待排序序列分割成独立的两部分,quickSort函数用于递归调用实现快速排序。 运行程序后,输出结果为:1 2 3 4 5 6 7 8 9,表示数组已经按照从小到大的顺序排列好了。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值