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;
}
开始选择一个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;
}
相关文章推荐
- Five kinds of sort algorithm
- Algorithm of the Week: Merge Sort
- implementation of General Sort Algorithm - mark
- [the summarization of algorithm]Heap and Heapsort
- algorithm——sort(eg.The Dragon of Loowater)
- An Algorithm Summary of Programming Collective Intelligence (2)
- HyperLogLog in Practice: Algorithmic Engineering of a State of The Art Cardinality Estimation Algorithm
- complexity of algorithm
- Implementation Of TDOA Using Chan Algorithm And Taylor Algorithm
- Coursera Algorithms, Part 1 Week 1: Analysis of Algorithm
- c++-algorithm 头文件排序sort
- 各位,我做了一个flash的Sort Algorithm Visualization
- Effective STL 46 Consider function objects instead of functions as algorithm parameters
- An universal algorithm design of fixed length substring locating
- algorithm to generate unique code of 64 bit
- Optimization of BackTracking algorithm for n queen problem
- Illustrating the generic search algorithm: Search for first occurrence of deque's contents as a subs
- Data Structures(Chapter 3 of The Algorithm Design Manual)
- A Neural Algorithm of Artistic Style
- A Neural Algorithm of Artistic Style 论文理解