C語言中使用快速排序算法對元素排序的實例詳解
調(diào)用C語言的快速排序算法qsort();
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#define SIZE 100
//從小到大排序
int comp1(const void *x,const void *y)
{
return *(int *)x - *(int *)y;
}
//從大到小排序
int comp2(const void *x,const void *y)
{
return *(int *)y - *(int *)x;
}
void main()
{
int arr[SIZE];
int n = 0; //數(shù)組的有效長度
int t = 0;
int i;
printf("input the arr(Q to quit).\n");
while( (t = scanf("%d",&arr[n])) != 0)
{
n++;
}
printf("arr before qsort.\n");
for(i = 0; i < n; i++)
{
printf("arr[%d]=%d\t",i,arr[i]);
if((i+1) % 5 == 0)
{
printf("\n");
}
}
qsort(arr,n,sizeof(int),comp1);
printf("\narr after qsort.\n");
for(i = 0; i < n; i++)
{
printf("arr[%d]=%d\t",i,arr[i]);
if((i+1) % 5 == 0)
{
printf("\n");
}
}
qsort(arr,n,sizeof(int),comp2);
printf("\nRecover All.\n");
for(i = 0; i < n; i++)
{
printf("arr[%d]=%d\t",i,arr[i]);
if((i+1) % 5 == 0)
{
printf("\n");
}
}
printf("\n");
}
只帶兩個參數(shù)的快速排序:
編寫快速排序,函數(shù)qsort(),函數(shù)只帶兩個參數(shù)
#include<stdio.h>
#include<time.h>
#include<stdlib.h>
#define LEN 10
typedef int dataType;
//初始化數(shù)組,數(shù)組元素為小于100的整數(shù)
void intiArr(dataType A[], int len);
//打印數(shù)組元素
void print(dataType A[], int len);
//帶兩個參數(shù)的快排
void qsort(dataType A[], int len);
int main()
{
dataType data[LEN];
intiArr(data,LEN);
printf("排序前數(shù)組元素:");
print(data,LEN);
qsort(data,LEN);
printf("排序后數(shù)組元素:");
print(data,LEN);
return 0;
}
初始化數(shù)組,數(shù)組元素為小于100的整數(shù)
void intiArr(dataType A[], int len)
{
int i;
srand((unsigned)time(NULL));
for(i = 0; i < len; i++)
{
A[i] = rand() % 100;
}
}
打印數(shù)組元素
void print(dataType A[], int len)
{
int i;
for(i = 0; i < len; i++)
{
if(i % 5 == 0)
printf("\n");
printf("%d\t",A[i]);
}
printf("\n");
}
帶兩個參數(shù)的快排
void qsort(dataType A[], int len)
{
dataType *p = A;
dataType *q = A + len - 1;
dataType temp = *p;;
if(len <= 0)
{
return ;
}
while(p < q)
{
while((p < q) && (*q >= temp))
{
q--;
}
*p = *q;
while((p < q) && (*p <= temp))
{
p++;
}
*q = *p;
}
*p = temp;
qsort(A,p - A);
qsort(p + 1,len - (p - A) - 1);
}
上一篇:C語言單鏈表的實現(xiàn)
欄 目:C語言
本文標(biāo)題:C語言中使用快速排序算法對元素排序的實例詳解
本文地址:http://www.jygsgssxh.com/a1/Cyuyan/2379.html
您可能感興趣的文章
- 04-02c語言函數(shù)調(diào)用后清空內(nèi)存 c語言調(diào)用函數(shù)刪除字符
- 04-02c語言的正則匹配函數(shù) c語言正則表達(dá)式函數(shù)庫
- 04-02func函數(shù)+在C語言 func函數(shù)在c語言中
- 04-02c語言中對數(shù)函數(shù)的表達(dá)式 c語言中對數(shù)怎么表達(dá)
- 04-02c語言用函數(shù)寫分段 用c語言表示分段函數(shù)
- 04-02c語言編寫函數(shù)冒泡排序 c語言冒泡排序法函數(shù)
- 04-02c語言沒有round函數(shù) round c語言
- 04-02c語言分段函數(shù)怎么求 用c語言求分段函數(shù)
- 04-02C語言中怎么打出三角函數(shù) c語言中怎么打出三角函數(shù)的值
- 04-02c語言調(diào)用函數(shù)求fibo C語言調(diào)用函數(shù)求階乘


閱讀排行
本欄相關(guān)
- 04-02c語言函數(shù)調(diào)用后清空內(nèi)存 c語言調(diào)用
- 04-02func函數(shù)+在C語言 func函數(shù)在c語言中
- 04-02c語言的正則匹配函數(shù) c語言正則表達(dá)
- 04-02c語言用函數(shù)寫分段 用c語言表示分段
- 04-02c語言中對數(shù)函數(shù)的表達(dá)式 c語言中對
- 04-02c語言編寫函數(shù)冒泡排序 c語言冒泡排
- 04-02c語言沒有round函數(shù) round c語言
- 04-02c語言分段函數(shù)怎么求 用c語言求分段
- 04-02C語言中怎么打出三角函數(shù) c語言中怎
- 04-02c語言調(diào)用函數(shù)求fibo C語言調(diào)用函數(shù)求
隨機(jī)閱讀
- 04-02jquery與jsp,用jquery
- 08-05DEDE織夢data目錄下的sessions文件夾有什
- 01-10C#中split用法實例總結(jié)
- 08-05織夢dedecms什么時候用欄目交叉功能?
- 01-11Mac OSX 打開原生自帶讀寫NTFS功能(圖文
- 01-10使用C語言求解撲克牌的順子及n個骰子
- 01-11ajax實現(xiàn)頁面的局部加載
- 01-10SublimeText編譯C開發(fā)環(huán)境設(shè)置
- 08-05dedecms(織夢)副欄目數(shù)量限制代碼修改
- 01-10delphi制作wav文件的方法


