void swap1(int *a,int *b)
{
int temp = *a ;
*a = *b;
*b = temp;
}
//冒泡排序
void BubbleSort(int *a,int size)
{
int i,j;
for(i=0;i<size;i++)
for(j=size - 1; j > i; j--)
if(a[j] < a[j-1])
swap1(&a[i],&a[j]);
}
//选择排序
void SelectSort(int *a,int size)
{
int i,j,min;
for(i=0;i<size-1;i++)
{
min = i;
for(j=i+1;j<size;j++)
if(a[min]>a[j])
min = j;
if(min != i)
swap1(&a[i],&a[min]);
}
}
//插入排序
void InsertSort(int *a,int size)
{
int i,j,temp;
for(i=1;i <size;i++)
{
temp = a[i];
j = i -1 ;
while( j >=0 && temp < a[j])
{
a[j +1] = a[j];
j--;
}
a[j+1] = temp;
}
}
//快速排序
int Partition(int *a,int low,int high)
{
int temp;
temp = a[low];
while(low < high)
{
while(low < high && a[high] >= temp)
high--;
a[low] = a[high];
while(low < high && a[low] <= temp)
low++;
a[high]= a[low];
}
a[low] = temp;
return low;
}
void RecSort(int *a,int low,int high){
int p;
if(low < high) {
p = Partition(a,low,high);
RecSort(a,low,p-1);
RecSort(a,p+1,high);
}
}
void QuickSort(int *a,int size)
{
RecSort(a,0,size-1);
}
//计数排序
void CountSort(int *a,int size)
{
int b[10] = {0};
int i,j;
for(i=0;i<size;i++)
{
b[a[i]]++;
}
j = 0;
for(i=0;i<10;i++)
{
while(b[i] != 0)
{
a[j] = i;
j++;
b[i]--;
}
}
}
//归并排序
void Merge(int*a,int start,int middle,int end)
{
int temp1[10];
int temp2[10];
int i,j,k;
int n1,n2;
n1 = middle - start + 1;
n2 = end - middle;
for(i=0 ; i< n1 ;i++)
{
temp1[i]=a[start + i];
}
for(i=0; i< n2 ;i++)
{
temp2[i]=a[middle + 1 + i];
}
temp1[n1]=100;
temp2[n2]=100;
i = j = 0;
for(k=start; k<= end; k++)
{
if(temp1[i]<temp2[j])
{
a[k] = temp1[i];
i++;
}else
{
a[k] = temp2[j];
j++;
}
}
}
void RecMerge(int *a,int start,int end)
{
if(start<end)
{
int middle = (start + end) / 2;
RecMerge(a,start,middle);
RecMerge(a,middle+1,end);
Merge(a,start,middle,end);
}
}
void MergeSort(int *a,int size)
{
RecMerge(a,0,size-1);
}
//希尔排序
void ShellSort(int *a,int size)
{
int i,j,k;
int temp;
for(i=size/2;i>0;i/=2)
{
for(j=i;j<size;j+=i)
{
temp = a[j];
k = j;
while(k-i >= 0 && temp < a[k-i])
{
a[k] = a[k-i];
k = k - i;
}
a[k] = temp;
}
}
}
//堆排序
void Heapify(int*arr, int low, int high)
{
int large;
int temp = arr[low];
large =2* low +1;
while(large <= high)
{
if(large<high && arr[large]<arr[large+1])
large = large +1;
if(temp > arr[large])
break;
else
{
arr[low] = arr[large];
low = large;
large =2* low +1;
}
}
arr[low] = temp;
}
void BuildHeap(int*arr, int size)
{
int i;
for(i=size/2-1;i>=0;i--)
Heapify(arr, i, size-1);
}
void HeapSort(int*arr, int size)
{
int i; //lastOfOrder
BuildHeap(arr, size);
for(i=size-1;i>=0;i--)
{
swap1(&arr[0], &arr[i]);
Heapify(arr, 0, i-1);
}
}
int main()
{
int a[9]={10,1,3,5,4,6,8,4,3};
int size = 9;
int i = 0;
//BubbleSort(a,size);
//SelectSort(a,size);
//InsertSort(a,size);
QuickSort(a,size);
//CountSort(a,size);
//MergeSort(a,size);
//ShellSort(a,size);
//HeapSort(a,size);
for(i=0;i<size;i++)
printf("%d ",a[i]);
}