标签:
下面是各个排序算法的实现:
void bobuleSort(int a[], int len)
{
int i,j,flag =1;
for(i = 0; i<len; i++)
{
for(int j =len-1; j>i; j--)
{
if(a[j] <= a[i])
{
int tmp = a[j];
a[j] = a[i];
a[i] = tmp;
flag = 0;
}
}
if(flag) break;
}
}
void selectSort(int a[], int len)
{
int i,j,k;
for(int i = 0; i< len; i++)
{
k = i;
for(j=i; j< len; j++)
{
if(a[k] >= a[j]) k = j;
}
if(k != i)
{
int tmp = a[k];
a[k] =a[i];
a[i] = tmp;
}
}
}
3 . 插入排序.
void insertSort(int a[], int len)
{
int i,j,k;
for(i = 1; i< len; i++)
{
if(a[i] < a[i-1])
{
k = a[i];
for(j=i-1;j<len; j++)
{
if(k<a[j]) a[j+1] = a[j];
}
a[j+1] = k;
}
}
}
4. 快速排序.
int division(int a[], int left, int right)
{
int key = a[left];
while(left <= right && key <= a[right])
right --;
a[left] = a[right];
while(left <= right && key <= a[left])
left ++;
a[right] = a[left];
a[left] = key;
return left;
}
void quickSort(int a[], int left, int right)
{
int mdedium;
if(left<right)
{
medium = division(a,left,right);
quickSort(a,left, medium-1);
quickSort(a,medium + 1,right);
}
}
版权声明:本文为博主原创文章,未经博主允许不得转载。
标签:
原文地址:http://blog.csdn.net/shaohuazuo/article/details/47137737