您的位置:首页 > 编程语言 > Go语言

algorithm_of_sort(交换排序)

2010-09-09 20:04 561 查看
冒泡排序:bubble sort

开始选择一个element like x[i],if x[i]biger than x[i+1],then exchange

them,untile every element is sort;

because after every time ,there has one element is sort,so ,at the begi

-ning ,we have to compare n-1,second ,we can copmare n-2,because one el

-ement has sorted.

now there is the code to realize the bubble sort.

void bubble(int x[],int n)

{

int j,temp;

int switched=TRUE;

int i;

for(i=0;i<n-1;i++)

{

for(j=0;j<n-1-i;j++)

{

if(x[j]>x[j+1])

{

switch=FALSE;

temp=x[j];

x[j]=x[j+1];

x[j+1]=temp;

}

}

}

}

Two:i will study the quick sort;

the theory is :if we algorithm the base dada like one of x[0],x[n-1],

x[n/2],or other data .the left of data base is smaller and right is big

-ger.ok ,repet in the left and right of the data base until every eleme

-nt is sort.

now there is code to realize the quick sort;

void quick_sort(int lb,int ub,int x[],int *pj)

{

int a,down,up,temp;

a=x[lb];

up=ub;

down=lb;

while(down<up)

{

while(x[down]<=a&&down<ub)

down++;

while(x[up]>a&&up<lb)

up--;

if(down<up)

{

temp=x[down];

x[down]=x[up];

x[up]=temp;

}

}/*end while*/

x[lb]=x[up];

x[up]=a;

*pj=up;

}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: