C语言快速排序代码

C\u8bed\u8a00\u5feb\u901f\u6392\u5e8f\u4ee3\u7801

\u91c7\u7528\u5feb\u901f\u6392\u5e8f\uff0c\u7528\u9012\u5f52\u5b9e\u73b0
#include
#define N 10 //\u5b9a\u4e49\u6392\u5e8f\u6570\u7ec4\u5143\u7d20\u4e2a\u6570
int Qsort(int start,int length,int a[])//start\u6392\u5e8f\u7684\u8d77\u59cb\uff0clength\u662f\u8981\u6392\u5e8f\u5e8f\u5217\u957f\u5ea6
{
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 = 0;i < N;i++)
scanf("%d",&a[i]);
Qsort(0,N,a);
for(i = 0;i < N;i++)
printf("%d ",a[i]);
}
\u7a0b\u5e8f\u6267\u884c\u65f6\u8f93\u5165N\u4e2a\u6570\uff0c\u5bf9\u8fd9N\u4e2a\u6570\u8fdb\u884c\u6392\u5e8f\uff0c\u53ef\u4ee5\u9884\u8bbeN\u7684\u957f\u5ea6

\u5feb\u901f\u6392\u5e8f\u6cd5\u201d\u4f7f\u7528\u7684\u662f\u9012\u5f52\u539f\u7406\uff0c\u4e0b\u9762\u6211\u7ed3\u5408\u4e00\u4e2a\u4f8b\u5b50\u6765\u8bf4\u660e\u201c\u5feb\u901f\u6392\u5e8f\u6cd5\u201d\u7684\u539f\u7406\u3002\u9996\u5148\u7ed9\u51fa\u4e00\u4e2a\u6570\u7ec4{53\uff0c12\uff0c98\uff0c63\uff0c18\uff0c72\uff0c80\uff0c46\uff0c 32\uff0c21}\uff0c\u5148\u627e\u5230\u7b2c\u4e00\u4e2a\u6570--53\uff0c\u628a\u5b83\u4f5c\u4e3a\u4e2d\u95f4\u503c\uff0c\u4e5f\u5c31\u662f\u8bf4\uff0c\u8981\u628a53\u653e\u5728\u4e00\u4e2a\u4f4d\u7f6e\uff0c\u4f7f\u5f97\u5b83\u5de6\u8fb9\u7684\u503c\u6bd4\u5b83\u5c0f\uff0c\u53f3\u8fb9\u7684\u503c\u6bd4\u5b83\u5927\u3002{21\uff0c12\uff0c32\uff0c 46\uff0c18\uff0c53\uff0c80\uff0c72\uff0c63\uff0c98}\uff0c\u8fd9\u6837\u4e00\u4e2a\u6570\u7ec4\u7684\u6392\u5e8f\u5c31\u53d8\u6210\u4e86\u4e24\u4e2a\u5c0f\u6570\u7ec4\u7684\u6392\u5e8f--53\u5de6\u8fb9\u7684\u6570\u7ec4\u548c53\u53f3\u8fb9\u7684\u6570\u7ec4\uff0c\u800c\u8fd9\u4e24\u4e2a\u6570\u7ec4\u7ee7\u7eed\u7528\u540c\u6837\u7684\u65b9\u5f0f\u7ee7\u7eed\u4e0b\u53bb\uff0c\u4e00\u76f4\u5230\u987a\u5e8f\u5b8c\u5168\u6b63\u786e\u3002
\u3000\u3000\u4e00\u822c\u6765\u8bf4\uff0c\u5192\u6ce1\u6cd5\u662f\u7a0b\u5e8f\u5458\u6700\u5148\u63a5\u89e6\u7684\u6392\u5e8f\u65b9\u6cd5\uff0c\u5b83\u7684\u4f18\u70b9\u662f\u539f\u7406\u7b80\u5355\uff0c\u7f16\u7a0b\u5b9e\u73b0\u5bb9\u6613\uff0c\u4f46\u5b83\u7684\u7f3a\u70b9\u5c31\u662f--\u7a0b\u5e8f\u7684\u5927\u5fcc--\u901f\u5ea6\u592a\u6162\u3002
\u9644\u4e0a\u5feb\u901f\u6392\u5e8f\u4ee3\u7801\uff1a

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34

#include
void quicksort(int a[],int left,int right)
{
int i,j,temp;
i=left;
j=right;
temp=a[left];
if(left>right)
return;
while(i!=j)
{
while(a[j]>=temp&&j>i)
j--;
if(j>i)
a[i++]=a[j];
while(a[i]i)
i++;
if(j>i)
a[j--]=a[i];

}
a[i]=temp;
quicksort(a,left,i-1);
quicksort(a,i+1,right);
}
void main()
{
int a[]={53,12,98,63,18,72,80,46,32,21};
int i;
quicksort(a,0,9);
/*\u6392\u597d\u5e8f\u7684\u7ed3\u679c*/
for(i=0;i<10;i++)
printf("%4d\n",a[i]);
}

#include <stdio.h>

int partions(int l[],int low,int high)
{
int prvotkey=l[low];
l[0]=l[low];
while (low<high)
{
while (low<high&&l[high]>=prvotkey)
--high;
l[low]=l[high];
while (low<high&&l[low]<=prvotkey)
++low;
l[high]=l[low];
}

l[low]=l[0];
return low;
}

void qsort(int l[],int low,int high)
{
int prvotloc;
if(low<high)
{
prvotloc=partions(l,low,high); //将第一次排序的结果作为枢轴
qsort(l,low,prvotloc-1); //递归调用排序 由low 到prvotloc-1
qsort(l,prvotloc+1,high); //递归调用排序 由 prvotloc+1到 high

}
}

void quicksort(int l[],int n)
{
qsort(l,1,n); //第一个作为枢轴 ,从第一个排到第n个
}

void main()
{
int a[11]={0,2,32,43,23,45,36,57,14,27,39};

for (int b=1;b<11;b++)
printf("%3d",a[b]);

printf("\n");
quicksort(a,11);

for(int c=1;c<11;c++)
printf("%3d",a[c]);

}

  • 濡備綍灏c璇█瀹炵幇鎸変粠灏忓埌澶х殑椤哄簭杈撳嚭?
    绛旓細C璇█瀹炵幇灏嗘暟缁勭殑鍏釜鍏冪礌鎸変粠灏忓埌澶х殑椤哄簭杈撳嚭锛屽彲浠ラ噰鐢ㄥ唴閮ㄦ帓搴忕畻娉曞鏁扮粍鐨勫厓绱犺繘琛屾帓搴忥紝鐒跺悗杈撳嚭鎺掑簭鍚庣殑鏁扮粍锛屽氨鍙互寰楀埌鎸変粠灏忓埌澶х殑椤哄簭杈撳嚭銆備互蹇熸帓搴涓轰緥鐨鎺掑簭浠g爜锛歷oid quickSort(int a[],int l,int r) { if(l>=r)return;int i = l;int j = r;int key = a[l];//閫夋嫨...
  • c璇█鎬庢牱瀹炵幇蹇熸帓搴
    绛旓細c = arr_num[i];arr_num[i] = arr_num[j];arr_num[j] = c;} } //left涓鸿捣濮嬪硷紙鍙傜収鍊硷級姝ゆ椂鐨処涓虹涓娆鎺掑簭缁撴潫鐨勬渶鍚庡硷紝涓庡弬鐓у间氦鎹綅缃 arr_num[left] = arr_num[i];arr_num[i] = temp;//缁х画閫掑綊鐩村埌鎺掑簭瀹屾垚 quick_sort(left, i-1);quick_sort(i+1, right);} i...
  • C璇█蹇熸帓搴
    绛旓細/蹇熸帓搴绠楁硶/ int Partition(int D[], int l, int r){ D[0]=D[l];while (l<r) { while (l<r && D[0]<D[r]) r--;D[l]=D[r];while (l<r && D[0]>=D[l]) l++;D[r]=D[l]; } D[r]=D[0];return r;} void Qsort(int D[], int l, int r){ int p...
  • 蹇熸帓搴缁熻姣旇緝娆℃暟鍜岃褰曠Щ鍔ㄦ鏁,鐢c璇█瀹炵幇,鏁版嵁鑷繁杈撳叆銆俖鐧 ...
    绛旓細typedefstruct{ int key;intother_data;}RecordType;typedef struct{ RecordType r[LIST_SIZE+1];int length;}RecordList;int compare_number=0,move_number=0;int QKPass_one(RecordType r[], int low, int high)//浣庡埌楂 鍗曟帓 { int x=r[low].key;move_number++;while(low<high){ whil...
  • C璇█蹇熸帓搴忎唬鐮
    绛旓細閲囩敤蹇熸帓搴锛岀敤閫掑綊瀹炵幇 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...
  • 濡備綍鐢c璇█杈撳叆10涓暟骞惰繘琛鎺掑簭?
    绛旓細鐒跺悗鐢╯canf鎺ュ彈鏁扮粍锛岀敤鎴疯緭鍏10涓暟浠ュ悗锛屼細灏嗘暟瀛樺湪鏁扮粍array涓紝鐒跺悗璋冪敤涓婇潰澶勭悊鎺掑簭鐨勫嚱鏁帮紝鍑芥暟鐨勮緭鍏ュ氨鏄垰鎵嶈緭鍏ョ殑鏁帮紝鏈鍚庡湪鎶婃帓搴忕殑缁撴灉杈撳嚭鍗冲彲锛4銆佹渶鍚庣紪璇戣繍琛岋紝杈撳叆10涓暟锛屾渶鍚庢帶鍒跺彴杈撳嚭浜嗘帓搴忕殑缁撴灉锛岃瘉鏄庣▼搴忕殑閫昏緫鏄病鏈夐棶棰樼殑銆備互涓婂氨鏄C璇█杈撳叆10涓暟鎺掑簭鐨勬紨绀猴細
  • C璇█澶х墰鎺ㄨ崘涓冨ぇ鎺掑簭绠楁硶瀛︾敓鏉ョ湅
    绛旓細C璇█7绉鎺掑簭绠楁硶闄浠g爜 1.鍐掓场鎺掑簭 姣旇緝鐩搁偦鐨勫厓绱犮傚鏋滅涓涓瘮绗簩涓ぇ锛屽氨浜ゆ崲瀹冧滑涓や釜瀵规瘡涓瀵圭浉閭诲厓绱犱綔鍚屾牱鐨勫伐浣滐紝浠庡紑濮嬬涓瀵瑰埌缁撳熬鐨勬渶鍚庝竴瀵癸紝杩欐牱鍦ㄦ渶鍚庣殑鍏冪礌搴旇浼氭槸鏈澶х殑鏁:閽堝鎵鏈夌殑鍏冪礌閲嶅浠ヤ笂鐨勬楠わ紝闄や簡鏈鍚庝竴涓;閲嶅姝ラ1~3锛岀洿鍒版帓搴忓畬鎴愩2.閫夋嫨鎺掑簭 鍦ㄦ湭鎺掑腑搴忓垪涓...
  • 鐢C璇█,闅忔満杈撳叆10涓暣鏁,鐢ㄥ啋娉鎺掑簭娉曞杩欎簺鏁存暟杩涜浠庡皬鍒板ぇ鎺掑簭...
    绛旓細C璇█闅忔満杈撳叆10涓暣鏁扮殑婧愪唬鐮濡備笅锛歩nclude"stdio.h"void fun(int a[]){ int i,j,t;for(i=0;i<9;i++)for(j=i+1;j<10;j++)if(a[i]>a[j]){t=a[i];a[i]=a[j];a[j]=t;} } void main(){ FILE *wf;int a[10];int b[10]={9,10,11,12,1,2,3,4,0,1};...
  • 鐢C璇█鍐欎釜瀹屾暣绋嬪簭,鍖呮嫭甯屽皵鎺掑簭鍜蹇熸帓搴
    绛旓細杩欐槸绗簩涓:蹇熸帓搴绠楁硶C绋嬪簭:/*code by jgao,閫掑綊蹇熸帓搴忕畻娉,杈撳叆涓哄瓧绗︽暟缁*/#include<stdio.h>void main(){ int quickSort(char vert[], int n, int begin, int end); char vert[] = "qwertyuiopasdfghjklzxcvbnm"; int n = 26; quickSort(vert, n, 0, n-1);}int quickSort(char vert...
  • 鐢C璇█缂栧啓鍑芥暟瀹炵幇蹇熸帓搴(鍗囧簭),鍦ㄤ富鍑芥暟涓緭鍏ユ暟缁勬暟鎹,骞惰皟鐢ㄨ...
    绛旓細//鎺掑簭鐨勭畻娉曟槸浜屽垎娉曪紝N鐨勫鏁版椂闂村鏉傚害銆傘傘//濡傛灉鏈夌枒闂紝鎴戜滑鍙互鍐嶆帰璁ㄣ傘傘俰nclude<stdlib.h> include<string.h> include<stdio.h> bool merge(int * array,int p,int q,int r){ if(!(p<<q<r)&&p>=0&&r<=sizeof(array)/sizeof(array[0])-1){ return false;} int * ...
  • 扩展阅读:c++入门程序代码 ... c++必背入门代码 ... c++编程爱心代码 ... 快速排序完整代码c ... c++编程代码大全 ... c++编程必背50个代码 ... c++入门源代码大全 ... c++最浪漫的编程代码 ... 冒泡排序c语言代码简单 ...

    本站交流只代表网友个人观点,与本站立场无关
    欢迎反馈与建议,请联系电邮
    2024© 车视网