排序
## Bubble_sort ##
void bublesort(int *a,int n)
{
int i,j,t;
for(i = 0;i<n-1;i++)
{
for(j = i+1;j<n;j++)
{
if(a[i]>a[j])
{
t = a[i];
a[i] = a[j];
a[j] = t;
}
}
}
return;
}
## Select_sort ##
void selectsort(int *a,int n)
{
int i,j,k,t;
for(i = 0;i<n - 1;i++)
{
k = i;
for(j = i+1;j<n;j++)
{
if(a[k]>a[j])
k = j;
}
if(k!=i)
{
t = a[k];
a[k] = a[i];
a[i] = t;
}
}
return;
}
## Quick_sort ##
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];
}
a[low] = val;
return low;
}
void sort(int a[],int low,int high)
{
int pos = findpos(a,low,high);
if(low >= high)
return;
else
{
sort(a,low,pos - 1);
sort(a,pos+1,high);
}
return;
}
## Insert_sort ##
void Insert_sort (int a[],int n)
{
int i,j,t;
for(i = 1;i< n;i++){
t = a[i];
for(j = i - 1;j>=0 && a[j]>t;j--)
a[j+1] = a[j];
a[j+1] = t;
}
return;
}
## Shell_sort ##
void sort (int a[],int n)
{
int k = n,i,j,t;
while(k>0)
{
for(i = k;i<n;i++)
{
t = a[i];
for(j = i - k;j>=0 && a[j] > t;j -= k)
a[j+k] = a[j];
a[j+k] = t;
}
k /= 2;
}
return;
}
## Merge_sort ##
void mergesort(int a[],int low,int high)
{
int m = (low+high)/2;
if(low == high)
return;
mergesort(a,low,m);
mergesort(a,m+1,high);
merge(a,low,m+1,high);
}
void merge(int a[],int low,int mid ,int high)
{
int LEFT_SIZE = mid - low;
int RIGHT_SIZE = high-mid+1;
int left[LEFT_SIZE];
int right[RIGHT_SIZE];
int i = 0,j = 0,k =low;
for(i = 0;i<mid;i++)
left[i-low] = a[i];
for(i =mid;i<=high;i++)
right[i-mid] = a[i];
i = 0;
while(i<LEFT_SIZE && j<RIGHT_SIZE)
{
if(left[i] > right[j])
{
a[k] = right[j];
j++;
k++;
}
else
{
a[k] = left[i];
i++;
k++;
}
}
while(i < LEFT_SIZE)
{
a[k] = left[i];
i++;
k++;
}
while(j < RIGHT_SIZE)
{
a[k] = right[j];
j++;
k++;
}
return;
}