
#include <stdio.h>
#define MAXN 8
#define MOD 1024
void QuickSort(int *arr, int low, int high)
{
if (low >= high) return
//保存排序区间的 起始位置和终点位置
int left = low, right = high
//默认 左边第一个元素 为标志
int key = arr[low]
while (low < high)
{
while (low < high && arr[high] <= key) --high
arr[low] = arr[high]
while (low < high && arr[low] >= key) ++low
arr[high] = arr[low]
}
arr[low] = key
//每次排序后都分成两部分[left, low) (low, right]
//arr[low]的位置是一定是有序的
QuickSort(arr, left, low - 1)
QuickSort(arr, low + 1, right)
return
}
int main(void)
{
int n
scanf("%d", &n)
int arr[MAXN] = {0}
旅嫌int i
for (i = 0 i < n ++i)
scanf("%d", &arr[i])
//输入是默兆乎认为生活中习惯的数族镇悉组左边第一个为:编号1
int s, m
scanf("%d %d", &s, &m)
//转成计算机数组第一个为:编号0
s-- m--
//快排
QuickSort(arr, s, m)
//输出
for (i = s i <= m ++i)
{
printf("%d ", arr[i])
}
return 0
}
//测试数据
//8
//1 2 3 4 5 6 7 8
//2 6
输出 6 5 4 3 2
#include<stdio.h>虚清丛void quickSort(int *p,int head,int tail){
int low=head,high=tail
int s=p[head]
if(head>=tail)return
while(low<差樱high){
while(low<high&&p[high]>s)high--
if(low<high)p[low++]=p[high]
while(low<high&&p[low]<=s)low++
if(low<high)p[high--]=p[low]
}p[low]=s
quickSort(p,head,low-1)quickSort(p,low+1,tail)
}void main()
{
int i=0
int array[10]={3,7,2,8,6,8,4,3,2,9}
quickSort(array,0,9)
for(i=0i<10i++)
{
printf("正盯%d ",array[i])
}
printf("\n")
}
采用快速排序,用递归实现#include <stdio.h>和银
#define N 10 //定义排序数组元素个数
int Qsort(int start,int length,int a[])//start排序的起始,length是要排序序列长度
{
int x = a[start]
int i,j
i = start
j = length -1
while(i <轮棚埋 j)
{
if(x <a[j])
j--
else if(x >a[j])
{
a[i] = a[j]
a[j] = x
i++
}
else if(x <a[i])
{
a[j] = a[i]
a[i] = x
j--
}
else
i++
}
if(start <length-1)
{
Qsort(start,i,a)
Qsort(i+1,length,a)
}
}
void main()
{
int a[N] = {0}
int i
for(i = 0i <Ni++)
scanf("%d",&a[i])
Qsort(0,N,a)
for(i = 0i <Ni++)
printf("%d ",a[i])
}
程序执行时输入N个数,对这N个数进行排序,腊蚂可以预设N的长度
欢迎分享,转载请注明来源:内存溢出
微信扫一扫
支付宝扫一扫
评论列表(0条)