快速排序
  Q61SiiCoGao8 2023年11月02日 44 0
/* 

2016-11-17 11:30:14 

快速排序 

 */ 

 #include<stdio.h> 

 #include <stdlib.h> 



 void QuickSort(int a[], int low, int high); 

 int FindPos(int* a, int low, int high); 



 int main(void) 

 { 

int a[10] = { 11, -8, 4, 54, 1, -99, 42, 23, 45, 68 }; 

 

QuickSort(a, 0, 9);    //第二个参数是第一个元素的下标,第三个参数是最后一个元素的坐标 

 

for (int i = 0; i < 10; i++) 

{ 

printf("%d ", a[i]); 

} 

printf("\n"); 

system("pause"); 

return 0; 

 } 



 void QuickSort(int a[], int low, int high) 

 { 

int pos; 

if (low < high) 

{ 

pos = FindPos(a, low, high); 

QuickSort(a, low, pos - 1); 

QuickSort(a, pos + 1, high); 

} 

 } 



 int FindPos(int* a, int low, int high) 

 { 

int val = a[low]; 
while (low < high)
 
{ 

while (low < high && a[high] >= val) 

--high; 

a[low] = a[high]; 



while (low < high && a[low] <= val) 

++low; 

a[high] = a[low]; 

}    //终止while循环之后low和high一定是相等的 

a[low] = val; 

return low;    //low可以改为high,但不能改为a[low]和a[high] 

 }
【版权声明】本文内容来自摩杜云社区用户原创、第三方投稿、转载,内容版权归原作者所有。本网站的目的在于传递更多信息,不拥有版权,亦不承担相应法律责任。如果您发现本社区中有涉嫌抄袭的内容,欢迎发送邮件进行举报,并提供相关证据,一经查实,本社区将立刻删除涉嫌侵权内容,举报邮箱: cloudbbs@moduyun.com

  1. 分享:
最后一次编辑于 2023年11月08日 0

暂无评论

推荐阅读
  wD98WYW8hiWJ   2023年11月20日   30   0   0 #include
  v0MZS93bOvwU   2023年11月02日   53   0   0 #include
  PVcilKyJJTzb   2023年11月02日   83   0   0 unix硬件平台c语言
Q61SiiCoGao8