- #include <stdio.h>
- #include <stdlib.h>
- /*
- 算法的分类
- 排序算法
- 插入排序(直接插入排序、希尔排序)
- 选择排序(简单选择排序、堆排序)
- 归并排序
- 基数排序
- */
- ///////////////////////////////////////////////////////////
- /*堆排序*/
- /**
- *@param A[] 完全二叉树
- *@param i 看中的那个父结点
- *@param size 结点的总数
- **/
- void heapify ( int a [] , int i , int size ){
- int left = i*2+1 ;
- int right = i*2 +2 ;
- int largest = i ;
- int temp = 0 ;
- if ( i <0 )
- return ;
- if ( left >=size )
- {
- heapify(a, i-1 , size);
- return 0;
- }
- else if ( a[left] > a[largest])
- {
- temp = a[largest] ;
- a[largest] = a[left] ;
- a[left] = temp ;
- }
- if (right >= size)
- ;
- else if ( a[right]> a[largest])
- {
- temp = a[largest] ;
- a[largest] = a[right] ;
- a[right] = temp ;
- }
- heapify(a,i-1,size);
- }
- //int b []={0,2,6,2,34,8,64,52,32,21,6};
- void heap_sort( int a [] , int size ){
- int i ;
- for ( i = size-1 ; i>=0 ; i-- ){
- heapify(a,i,i+1) ;
- printf("size =%d max = %d ",i+1,a[0]);
- int temp = a [0] ;
- a[0] = a[i] ;
- a[i] = temp;
- }
- }
- /////////////////////////////////////////////////////////////
- /*归并排序*/
- void mergearray(int a[], int first, int mid, int last, int temp[])
- {
- int i = first, j = mid + 1;
- int m = mid, n = last;
- int k = 0;
- while (i <= m && j <= n)
- {
- if (a[i] < a[j])
- temp[k++] = a[i++];
- else
- temp[k++] = a[j++];
- }
- while (i <= m)
- temp[k++] = a[i++];
- while (j <= n)
- temp[k++] = a[j++];
- for (i = 0; i < k; i++)
- a[first + i] = temp[i];
- }
- void merge_sort( int a[] , int first , int last , int *temp){
- if ( first < last){
- int mid = (first + last) /2 ;
- merge_sort(a ,first,mid , temp) ;
- merge_sort(a,mid+1,last , temp) ;
- mergearray(a,first,mid,last , temp);
- }
- }
- int MergeSort( int a [] , int n ){
- int p [n+1];
- if ( p == NULL)
- return 0 ;
- else
- printf("开闭空间:%d ",n );
- merge_sort(a, 0,n-1,p) ;
- //free(p) ;
- return 1 ;
- }
- //////////////////////////////////////////////
- /*选择排序*/
- selectionSort( int a[], long len ){
- int j = 0 , i = 0 ;
- int tmp ;
- long max_pos ;
- for ( i = len-1;i>=1;i--){
- max_pos = i ;
- for( j = 0 ; j<i ; j++ )
- if( a[max_pos] <a[j])
- max_pos = j ;
- if( max_pos !=i ) {
- tmp = a[i] ;
- a[i] = a[max_pos] ;
- a[max_pos] = tmp;
- }
- }
- }
- /*冒泡排序*/
- void doubleSort(int a [] , long len){
- int i = len -1 ;
- int j = len -1 ;
- int temp ;
- for ( i =len;i >0; i--){
- for ( j = i; j >0 ; j-- ){
- if( a[j]<a[j-1]){
- temp = a[j] ;
- a[j] = a[j-1] ;
- a[j-1] = temp ;
- }
- }
- }
- }
- /*快速排序*/
- int partiion( int a [], int left , int right){
- a[0] = a[left] ;
- while(left<right){
- while(left<right && a[0]<a[right])
- right -- ;
- if( left<right){
- // update benchmark
- a[left] = a[right];
- left ++ ;
- }
- while (left<right&&a[0]>a[left])
- left++;
- if( left < right ){
- a[right] = a[left];
- right -- ;
- }
- }
- a[left] = a[0] ;
- return left;
- }
- void quickSort( int a [], int left, int right){
- int i ;
- if ( left<right){
- i = partiion(a,left,right);
- quickSort(a,left,i-1);
- quickSort(a,i+1,right);
- }
- }
- /////////////////////////////////////////////////////////
- /*简单插入排序*/
- void insertSort( int a[] , long len ){
- int i , j;
- int temp ;
- for ( i =0; i <=len -1 ; i++) {
- j = i+ 1;
- if ( a[j] <a[i])
- {
- temp = a[j] ;
- while(temp <a[i]){
- a[i+1] = a[i] ;
- i -- ;
- }
- // insert data
- a[i+1] = temp ;
- }
- i = j-1 ;
- }
- }
- /*希尔排序*/
- void shellSort ( int a [] , int len ){
- int temp ,gap;
- int i , j ;
- for( gap = len/2; gap>0 ;gap/=2 ){
- for ( i =0 ; i <gap ; i ++ ){
- for ( j = i+gap ; j <len;j++){
- /*最起码要小于 已经排好序里面的最大值*/
- if(a[j]<a[j-gap]){
- temp = a[j] ;
- int k = j-gap ;
- while(k >=0 && a[k]>temp ){
- a [k+gap ] = a[k] ;
- k-= gap ;
- }
- a[k+gap ] = temp;
- }
- }
- }
- }
- }
- void swap(int a [] ){
- a[1] = a[2];
- }
- int main ( void ) {
- int a [6]={34,8,64,52,32,21};
- int b []={0,2,6,2,34,8,64,52,32,21,6};
- // insertSort(a,6);
- //shellSort( a , 6) ;
- // selectionSort(a,6);
- //doubleSort(a,11);
- // quickSort(b,1,10);
- // MergeSort(b,sizeof(b)/sizeof(b[0]));
- heap_sort(b,sizeof(b)/sizeof(b[0]));
- int i ;
- //swap(b);
- // for( i =0 ; i<6 ;i++)
- // printf("%d ",a[i]);
- for( i =0; i<sizeof(b)/sizeof(b[0]) ;i++)
- printf("%d ",b[i]);
- printf(" ");
- return 0;
- }