诛仙3法宝融合化多少钱:[Python]python和C语言分别实现快速排序
来源:百度文库 编辑:九乡新闻网 时间:2024/07/14 22:32:04
def quick_sort(arr):
if len(arr) > 1:
qsort(arr,0,len(arr)-1)
def qsort(arr,start,end):
base = arr[start]
pl = start + 1
pr = end
while True:
while arr[pl] > base:
pl += 1
while arr[pr] < base:
pr -= 1
if pl >= pr:
break;
arr[pl],arr[pr] = arr[pr],arr[pl]
arr[start] = arr[pr]
arr[pr] = base
if(pr - 1 > start):
qsort(arr,start,pr - 1)
if(pl + 1 < end):
qsort(arr,pr+1,end)
r = Random()
a = []
for i in range(20):
a.append(r.randint(0,100))
print a
quick_sort(a)
print a
最后结果:
[20, 84, 4, 12, 48, 91, 71, 84, 44, 43, 78, 46, 26, 50, 51, 90, 40, 7, 93, 62]
[93, 91, 90, 84, 84, 78, 71, 62, 51, 50, 48, 46, 44, 43, 40, 26, 20, 12, 7, 4]
C部分:
* =====================================================================================
*
* Filename: quickSort.cpp
*
* Description: quick Sort method
*
* Version: 1.0
* Created: 11/25/2010 08:52:33 PM
* Revision: none
* Compiler: gcc
*
* Author: Archy Yu
* Company:
*
* =====================================================================================
*/
#include
void swap(int &i,int &j)
{
int k = i;
i = j;
j = k;
}
int Partition(int a[3],int left,int right)
{
int i = left;
int j = right + 1;
int tem = a[i];
while(true)
{
while(a[++i] < tem);
while(a[--j] > tem);
if(i >= j)
break;
swap(a[i],a[j]);
}
a[left] = a[j];
a[j] = tem;
return j;
}
void QuickSort(int a[3],int left,int right)
{
if(left < right)
{
int part = Partition(a,left,right);
QuickSort(a,left,part-1);
QuickSort(a,part+1,right);
}
}
int main()
{
int b[3] = {9,7,8};
QuickSort(b,0,3);
for(int i=0;i<=2;i++)
{
printf("%d ",b[i]);
}
return 0;
}
[Python]python和C语言分别实现快速排序
Python
python
使用 Python 实现多进程
python中调用c++(Boost
C++ 扩展和嵌入 Python
Python input和raw_input的区别 [Python俱乐部]
[Python]Python网页编程 - 解析和生成 Last-Modified-Time
[Python]python随笔
[Python]Python笔记-
[Python]python编程 FAQ
编程中国?>>?Python成为2010年度语言
[Python]的效率实在是比不上c!
Python编程语言入门学习-编程语言-开发语言
标准悬链线方程和C语言实现
python 数列
[Python]C#程序员初学Python
[Python]Python连接MySQL (例子)
[Python]python for ARM/LINUX
[Python]MySQLdb for Python使用指南
[Python]用Python操作Mysql
·C语言排序算法
[Python]WebPy学习笔记一 搭建环境,实现简单示例
动态语言ruby、groovy、python基本语法复习1