sort相关

  1. /** 
  2.  *  
  3.  * Heap sort 
  4.  * @author ljs 
  5.  * 2011-06-03 
  6.  *  
  7.  *  
  8.  */  
  9. public class HeapSort {  
  10.     public static void solve(int[] data){  
  11.         heapify(data);  
  12.           
  13.         int len = data.length;  
  14.         //sort: remove the top element and put it at the right side  
  15.         int size = len;  
  16.           
  17.         while(size>0){  
  18.             //swap   
  19.             int tmp = data[size-1];               
  20.             data[size-1] = data[0];  
  21.             data[0] = tmp;  
  22.               
  23.             size--;  //only consider 0...size-1  
  24.             moveDown(data,0,size);  
  25.         }  
  26.     }  
  27.       
  28.     /** 
  29.      * 用Floyd算法转换任何一颗完全二叉树为最大值堆(maxheap) 
  30.      *  
  31.      * Build a heap from any non-heap array 
  32.      * 转换数组为堆结构,无需开辟额外的空间(直接在数组中交换in-place) 
  33.      * Floyd Algorithm: O(n) 
  34.      * 
  35.      */  
  36.     //input: a randomly generated integer array   
  37.     //output: convert it into a maxheap   
  38.     private static void heapify(int[] data){  
  39.         if(data.length<2) return;  
  40.           
  41.         //the last nonleaf node's index  
  42.         int nonLeafIndex = data.length/2 - 1;  
  43.           
  44.         for(int i=nonLeafIndex;i>=0;i--){  
  45.             moveDown(data,i,data.length);  
  46.         }         
  47.     }  
  48.     //index: the nonleaf node's index  
  49.     //size: the size of array (from 0 to size-1) that is to be heapified  
  50.     private static void moveDown(int[] data,int index,int size){          
  51.         int target = data[index];  //save the target value to be moved down  
  52.           
  53.         while(true){  
  54.             int leftChildIndex = 2*index + 1;  
  55.             int rightChildIndex = leftChildIndex+1;  
  56.               
  57.             if(rightChildIndex>size-1){  
  58.                 //if right child is empty, left child must be empty or leaf  
  59.                 if(leftChildIndex<size){  
  60.                     //left child is leaf  
  61.                     if(target<data[leftChildIndex]){  
  62.                         data[index] = data[leftChildIndex];  
  63.                         index = leftChildIndex;  
  64.                     }  
  65.                 }  
  66.                 break;                                
  67.             }  
  68.               
  69.             if(data[leftChildIndex]>data[rightChildIndex]){  
  70.                 //left child is larger  
  71.                 if(target<data[leftChildIndex]){  
  72.                     //exchange the max with index node  
  73.                     data[index] = data[leftChildIndex];  
  74.                     index = leftChildIndex;  
  75.                 }else{  
  76.                     break;  
  77.                 }  
  78.             }else{  
  79.                 //right child is larger  
  80.                 if(target<data[rightChildIndex]){  
  81.                     data[index] = data[rightChildIndex];  
  82.                     index = rightChildIndex;  
  83.                 }else{  
  84.                     break;  
  85.                 }  
  86.             }  
  87.                   
  88.         }  
  89.         data[index] = target;             
  90.     }  
  91.       
  92.     public static void main(String[] args) {  
  93.         int[] data = {2,6,12,17,9,1,0,3,4,8,7,10,5};  
  94.         HeapSort.solve(data);  
  95.         for(int i=0;i<data.length;i++){  
  96.             System.out.format(" %d",data[i]);  
  97.         }  
  98.     }  
  99. }  
 
 
#include "stdio.h"
#include "string.h"
void bubble_sort(int a[],int len)
{
    int temp = 0;
    for(int i = len-1;i>0;i--)
   for ( int j = 0; j<i; j++)
{
     if(a[j] >a[j+1])
   {
   temp = a[j];
a[j] = a[j+1];
a[j+1] = temp;
   }
   }
}
void select_sort(int a[],int len)
{
    int temp = 0;
int loc = 0;
    for(int i = len-1;i>0;i--)
{
   temp = a[i];
   for ( int j = 0; j<i; j++)
{
   if ( temp < a[j] )
{
   loc = j;
       temp = a[j];
   }
   }
a[loc] = a[i];
a[i] = temp;
 
    }
}
void merge(int a[],int s,int m,int e)
{
    int l = e-s+1;
    int temp[l];
int i=s,j=m+1,k=0;
while(i<=m&&j<=e)
{
   if ( a[i] < a[j] )
   temp[k++] = a[i++];
   else
   temp[k++] = a[j++];
}
if(i > m)
{
   for (;j<=e;j++)
temp[k++] = a[j];
}
if(j > e)
{
   for(;i<=m;i++)
temp[k++] = a[i];
}
memcpy(a+s,temp,l*sizeof(int));
}
void mergesort(int a[],int s, int e)
{
    if(s!=e)
{
   mergesort(a,s,(s+e)/2);
mergesort(a,(s+e)/2+1,e);
merge(a,s,(s+e)/2,e);
}
}
void QuickSort(int a[],int numsize)/*a是整形数组,numsize是元素个数*/
{
int i=0,j=numsize-1;
int val=a[0];/*指定参考值val大小*/
if(numsize>1)/*确保数组长度至少为2,否则无需排序*/
{
while(i<j)/*循环结束条件*/
{
/*从后向前搜索比val小的元素,找到后填到a[i]中并跳出循环*/
for(;j>i;j--)
if(a[j]>val)
{
a[i]=a[j];
break;
}
/*从前往后搜索比val大的元素,找到后填到a[j]中并跳出循环*/
for(;i<j;i++)
if(a[i]<val)
{
a[j]=a[i];
break;
}
}
a[i]=val;/*将保存在val中的数放到a[i]中*/
QuickSort(a,i);/*递归,对前i个数排序*/
QuickSort(a+i+1,numsize-1-i);/*对i+1到numsize-1这numsize-1-i个数排序*/
}
}
int main()
{
    int a[] = {10,9,8,7,6,5,4,3,2,1};
//    for ( int i = 0; i<10; i++)
        //printf("%d\n",a[i]);
   // bubble_sort(a,10);
   // for ( int i = 0; i<10; i++)
   //     printf("%d\n",a[i]);
   mergesort(a,0,9);
    for ( int i = 0; i<10; i++)
        printf("%d\n",a[i]);
   QuickSort(a,10);
    for ( int i = 0; i<10; i++)
        printf("%d\n",a[i]);
   select_sort(a,10);
    for ( int i = 0; i<10; i++)
        printf("%d\n",a[i]);
return 0;
}
posted @ 2016-05-26 09:08  bshaozi  阅读(120)  评论(0编辑  收藏  举报