转【算法之常用排序算法(一)】八大常用内部排序算法(快排、冒泡、希尔、堆排序等)


概述

排序有内部排序和外部排序,内部排序是数据记录在内存中进行排序,而外部排序是因排序的数据很大,一次不能容纳全部的排序记录,在排序过程中需要访问外存。

我们这里说说八大排序就是内部排序。

    当n较大,则应采用时间复杂度为O(nlog2n)的排序方法:快速排序、堆排序或归并排序序。

   快速排序:是目前基于比较的内部排序中被认为是最好的方法,当待排序的关键字是随机分布时,快速排序的平均时间最短;

算法的实现:

  1. void print(int a[], int n ,int i){  
  2.     cout<":";  
  3.     for(int j= 0; j<8; j++){  
  4.         cout<" ";  
  5.     }  
  6.     cout<
  7. }  
  8.   
  9.   
  10. void InsertSort(int a[], int n)  
  11. {  
  12.     for(int i= 1; i
  13.         if(a[i] < a[i-1]){               //若第i个元素大于i-1元素,直接插入。小于的话,移动有序表后插入  
  14.             int j= i-1;   
  15.             int x = a[i];        //复制为哨兵,即存储待排序元素  
  16.             a[i] = a[i-1];           //先后移一个元素  
  17.             while(x < a[j]){  //查找在有序表的插入位置  
  18.                 a[j+1] = a[j];  
  19.                 j--;         //元素后移  
  20.             }  
  21.             a[j+1] = x;      //插入到正确位置  
  22.         }  
  23.         print(a,n,i);           //打印每趟排序的结果  
  24.     }  
  25.       
  26. }  
  27.   
  28. int main(){  
  29.     int a[8] = {3,1,5,7,2,4,9,6};  
  30.     InsertSort(a,8);  
  31.     print(a,8,8);  
  32. }  


效率:

时间复杂度:O(n^2).

其他的插入排序有二分插入排序,2-路插入排序。

Java实现:

view plaincopy      
  1. package com.weijiang.demo;    
  2.   
  3. public class InsertSort {    
  4.   
  5.     public InsertSort(){    
  6.         int a[]={49,38,65,97,76,13,27,49,78,34,12,64,5,4,62,99,98,54,56,17,18,23,34,15,35,25,53,51};    
  7.         int temp=0;    
  8.         for(int i=1;i
  9.             int j=i-1;    
  10.             temp=a[i];    
  11.             for(;j>=0 && temp
  12.                 a[j+1]=a[j];//将大于temp的值整体后移一个单位    
  13.             }    
  14.             a[j+1]=temp;    
  15.         }    
  16.   
  17.         for(int i=0;i
  18.             System.out.println(a[i]);    
  19.     }    
  20.   
  21. }    

4. 特点:每次循环一边之后,最前面的一部分一定是有序序列,但是位置不是最终的

view plaincopy      
  1. package com.weijiang.demo;  
  2.   
  3. public class ShellSort {    
  4.     public ShellSort(){    
  5.         int a[]={1,54,6,3,78,34,12,45,56,100};    
  6.         double d1=a.length;    
  7.         int temp=0;    
  8.         while(true){    
  9.             d1= Math.ceil(d1/2);    
  10.             int d=(int) d1;    
  11.             for(int x=0;x
  12.                 for(int i=x+d;i
  13.                     int j=i-d;    
  14.                     temp=a[i];    
  15.                     for(;j>=0 && temp
  16.                         a[j+d]=a[j];    
  17.                     }    
  18.                     a[j+d]=temp;    
  19.                 }    
  20.             }    
  21.             if(d==1)    
  22.                 break;    
  23.         }    
  24.         for(int i=0;i
  25.             System.out.println(a[i]);    
  26.   
  27.     }    
  28.   
  29. }    

view plaincopy      
  1. package com.weijiang.demo;  
  2.   
  3. public class SelectSort {    
  4.   
  5.     public SelectSort(){    
  6.         int a[]={1,54,6,3,78,34,12,45};    
  7.         int position=0;    
  8.         for(int i=0;i
  9.             int j=i+1;    
  10.             position=i;    
  11.             int temp=a[i];    
  12.             for(;j
  13.                 if(a[j]
  14.                     temp=a[j];    
  15.                     position=j;    
  16.                 }    
  17.             }    
  18.             a[position]=a[i];    
  19.             a[i]=temp;    
  20.         }    
  21.         for(int i=0;i
  22.             System.out.println(a[i]);    
  23.   
  24.     }    
  25.   
  26. }    

4. 特点:每次循环一边之后,最前面的一部分一定是有序的,而且这个顺序不会再改变。这个和前面的插入排序有点不一样。

view plaincopy      
  1. package com.weijiang.demo;  
  2.   
  3. import java.util.Arrays;    
  4.   
  5. public class HeapSort {    
  6.   
  7.     int a[]={49,38,65,97,76,13,27,49,78,34,12,64,5,4,62,99,98,54,56,17,18,23,34,15,35,25,53,51};    
  8.   
  9.     public HeapSort(){    
  10.         heapSort(a);    
  11.   
  12.     }    
  13.   
  14.     public  void heapSort(int[] a){    
  15.         System.out.println("开始排序");    
  16.         int arrayLength=a.length;    
  17.         //循环建堆    
  18.         for(int i=0;i1;i++){    
  19.             //建堆    
  20.             buildMaxHeap(a,arrayLength-1-i);    
  21.             //交换堆顶和最后一个元素    
  22.             swap(a,0,arrayLength-1-i);    
  23.             System.out.println(Arrays.toString(a));    
  24.         }    
  25.     }    
  26.   
  27.     private  void swap(int[] data, int i, int j) {    
  28.         int tmp=data[i];    
  29.         data[i]=data[j];    
  30.         data[j]=tmp;    
  31.     }    
  32.   
  33.     //对data数组从0到lastIndex建大顶堆    
  34.     private void buildMaxHeap(int[] data, int lastIndex) {    
  35.         //从lastIndex处节点(最后一个节点)的父节点开始    
  36.         for(int i=(lastIndex-1)/2;i>=0;i--){    
  37.             //k保存正在判断的节点    
  38.             int k=i;    
  39.             //如果当前k节点的子节点存在    
  40.             while(k*2+1<=lastIndex){    
  41.                 //k节点的左子节点的索引    
  42.                 int biggerIndex=2*k+1;    
  43.                 //如果biggerIndex小于lastIndex,即biggerIndex+1代表的k节点的右子节点存在    
  44.                 if(biggerIndex
  45.                     //若果右子节点的值较大    
  46.                     if(data[biggerIndex]1]){    
  47.                         //biggerIndex总是记录较大子节点的索引    
  48.                         biggerIndex++;    
  49.                     }    
  50.                 }    
  51.   
  52.                 //如果k节点的值小于其较大的子节点的值    
  53.                 if(data[k]
  54.                     //交换他们    
  55.                     swap(data,k,biggerIndex);    
  56.                     //将biggerIndex赋予k,开始while循环的下一次循环,重新保证k节点的值大于其左右子节点的值    
  57.                     k=biggerIndex;    
  58.                 }else{    
  59.                     break;    
  60.                 }    
  61.   
  62.             }    
  63.   
  64.         }    
  65.   
  66.     }   
  67. }  

view plaincopy      
  1. package com.weijiang.demo;  
  2.   
  3. public class BubbleSort {    
  4.     public BubbleSort(){    
  5.         int a[]={1,54,6,3,78,34,12,45};    
  6.         int temp=0;    
  7.         for(int i=0;i
  8.             for(int j=i+1;j
  9.                 if(a[i]>a[j]){    
  10.                     temp=a[i];    
  11.                     a[i]=a[j];    
  12.                     a[j]=temp;    
  13.                 }    
  14.             }    
  15.         }    
  16.         for(int i=0;i
  17.             System.out.println(a[i]);       
  18.     }    
  19.   
  20. }    

经过道友的提醒,发现上面的不是正宗的冒泡排序,其实上面的相当去选择排序的变种。所以更正过来:

正宗的冒泡排序:

 

[java] view plaincopy      
  1. package com.weijiang.demo;  
  2.   
  3. public class BubbleSort {    
  4.     public BubbleSort(){    
  5.         int a[]={49,38,65,97,76,13,27,49,78,34,12,64,5,4,62,99,98,54,56,17,18,23,34,15,35,25,53,51};    
  6.         int temp=0;    
  7.         for(int i=0;i1;i++){    
  8.             for(int j=0;j1-i;j++){    
  9.                 if(a[j]>a[j+1]){    
  10.                     temp=a[j];    
  11.                     a[j]=a[j+1];    
  12.                     a[j+1]=temp;    
  13.                 }    
  14.             }    
  15.         }    
  16.         for(int i=0;i
  17.             System.out.println(a[i]);       
  18.     }    
  19. }  

4. 特点:和选择排序的特点一样,每循环一边之后最前面的一部分是有序的,而且位置不会再改变了

注:上面的冒泡排序的过程我们是可以进行一些优化操作的,可以添加一个变量来记录每次有没有交换操作,如果没有的话,说明序列已经有序了,不需要在进行比较了,代码如下:

 

[java] view plaincopy      
  1. package com.weijiang.demo;  
  2.   
  3. public class EnhanceBubbleSort {    
  4.     public EnhanceBubbleSort(){    
  5.         int a[]={49,38,65,97,76,13,27,49,78,34,12,64,5,4,62,99,98,54,56,17,18,23,34,15,35,25,53,51};   
  6.         int temp=0;   
  7.         boolean isChange = false;//记录每次有没有交换值的状态  
  8.         for(int i=0;i1;i++){    
  9.             isChange = false;  
  10.             for(int j=0;j1-i;j++){    
  11.                 if(a[j]>a[j+1]){  
  12.                     isChange = true;  
  13.                     temp=a[j];    
  14.                     a[j]=a[j+1];    
  15.                     a[j+1]=temp;    
  16.                 }    
  17.             }   
  18.             //如果一趟下来之后没有交换操作,说明数组已经有序了,直接跳出循环  
  19.             if(!isChange)  
  20.                 break;  
  21.         }    
  22.         for(int i=0;i
  23.             System.out.println(a[i]);       
  24.     }   
  25. }  

如果原始序列大部分有序了,这个效率比之前的冒泡排序效果高出很多

 

 

view plaincopy      
  1. package com.weijia.demo;  
  2.   
  3. public class QuickSort {    
  4.   
  5.     int a[]={49,38,65,97,76,13,27,49,78,34,12,64,5,4,62,99,98,54,56,17,18,23,34,15,35,25,53,51};    
  6.     public QuickSort(){    
  7.         quick(a);    
  8.         for(int i=0;i
  9.             System.out.println(a[i]);    
  10.   
  11.     }    
  12.   
  13.     public int getMiddle(int[] list, int low, int high) {       
  14.         int tmp = list[low];    //数组的第一个作为中轴       
  15.         while (low < high) {       
  16.             while (low < high && list[high] >= tmp) {       
  17.                 high--;       
  18.             }       
  19.             list[low] = list[high];   //比中轴小的记录移到低端       
  20.             while (low < high && list[low] <= tmp) {       
  21.                 low++;       
  22.             }       
  23.             list[high] = list[low];   //比中轴大的记录移到高端       
  24.         }       
  25.         list[low] = tmp;              //中轴记录到尾       
  26.         return low;                   //返回中轴的位置       
  27.   
  28.     }      
  29.   
  30.     public void _quickSort(int[] list, int low, int high) {       
  31.         if (low < high) {       
  32.             int middle = getMiddle(list, low, high);  //将list数组进行一分为二       
  33.             _quickSort(list, low, middle - 1);        //对低字表进行递归排序       
  34.             _quickSort(list, middle + 1, high);       //对高字表进行递归排序       
  35.         }       
  36.     }     
  37.   
  38.     public void quick(int[] a2) {       
  39.         if (a2.length > 0) {    //查看数组是否为空       
  40.             _quickSort(a2, 0, a2.length - 1);       
  41.         }       
  42.     }     
  43.   
  44. }    

4. 特点:每一趟结束之后,中间的数的位置不会在改变了,而且每次都是以这个中间数为中心轴的话,一部分是比这个数都小的,另外一部分都是比这个数都大的

view plaincopy      
  1. package com.weijia.demo;  
  2.   
  3. import java.util.Arrays;    
  4.   
  5. public class MergingSort {    
  6.       
  7.     int a[]={49,38,65,97,76,13,27,49,78,34,12,64,5,4,62,99,98,54,56,17,18,23,34,15,35,25,53,51};    
  8.       
  9.     public  MergingSort(){    
  10.         sort(a,0,a.length-1);    
  11.         for(int i=0;i
  12.             System.out.println(a[i]);    
  13.     }    
  14.       
  15.     public void sort(int[] data, int left, int right) {    
  16.         if(left
  17.             //找出中间索引    
  18.             int center=(left+right)/2;    
  19.             //对左边数组进行递归    
  20.             sort(data,left,center);    
  21.             //对右边数组进行递归    
  22.             sort(data,center+1,right);    
  23.             //合并    
  24.             merge(data,left,center,right);    
  25.   
  26.         }    
  27.     }    
  28.       
  29.     public void merge(int[] data, int left, int center, int right) {    
  30.         int [] tmpArr=new int[data.length];    
  31.         int mid=center+1;    
  32.         //third记录中间数组的索引    
  33.         int third=left;    
  34.         int tmp=left;    
  35.         while(left<=center&&mid<=right){    
  36.             //从两个数组中取出最小的放入中间数组    
  37.             if(data[left]<=data[mid]){    
  38.                 tmpArr[third++]=data[left++];    
  39.             }else{    
  40.                 tmpArr[third++]=data[mid++];    
  41.             }    
  42.         }    
  43.         //剩余部分依次放入中间数组    
  44.         while(mid<=right){    
  45.             tmpArr[third++]=data[mid++];    
  46.         }    
  47.         while(left<=center){    
  48.             tmpArr[third++]=data[left++];    
  49.         }    
  50.         //将中间数组中的内容复制回原数组    
  51.         while(tmp<=right){    
  52.             data[tmp]=tmpArr[tmp++];    
  53.         }    
  54.         System.out.println(Arrays.toString(data));    
  55.     }    
  56.   
  57. }    


view plaincopy      
  1. package com.weijia.demo;  
  2.   
  3. import java.util.ArrayList;    
  4. import java.util.List;    
  5.   
  6. public class RadixSort {    
  7.   
  8.     int a[]={49,38,65,97,76,13,27,49,78,34,12,64,5,4,62,99,98,54,101,56,17,18,23,34,15,35,25,53,51};   
  9.       
  10.     public RadixSort(){    
  11.         sort(a);    
  12.         for(int i=0;i
  13.             System.out.println(a[i]);    
  14.     }    
  15.   
  16.     public  void sort(int[] array){       
  17.         //首先确定排序的趟数;       
  18.         int max=array[0];       
  19.         for(int i=1;i
  20.             if(array[i]>max){       
  21.                 max=array[i];       
  22.             }       
  23.         }       
  24.   
  25.         int time=0;       
  26.         //判断位数;       
  27.         while(max>0){       
  28.             max/=10;       
  29.             time++;       
  30.         }       
  31.   
  32.         //建立10个队列;       
  33.         List queue=new ArrayList();       
  34.         for(int i=0;i<10;i++){       
  35.             ArrayList queue1=new ArrayList();     
  36.             queue.add(queue1);       
  37.         }       
  38.   
  39.         //进行time次分配和收集;       
  40.         for(int i=0;i
  41.             //分配数组元素;       
  42.             for(int j=0;j
  43.                 //得到数字的第time+1位数;     
  44.                 int x=array[j]%(int)Math.pow(10, i+1)/(int)Math.pow(10, i);    
  45.                 ArrayList queue2=queue.get(x);    
  46.                 queue2.add(array[j]);    
  47.                 queue.set(x, queue2);    
  48.             }       
  49.             int count=0;//元素计数器;       
  50.             //收集队列元素;       
  51.             for(int k=0;k<10;k++){     
  52.                 while(queue.get(k).size()>0){    
  53.                     ArrayList queue3=queue.get(k);    
  54.                     array[count]=queue3.get(0);       
  55.                     queue3.remove(0);    
  56.                     count++;    
  57.                 }       
  58.             }       
  59.         }       
  60.     }      
  61.   
  62. }   


注明:转载请提示出处:http://blog.csdn.net/hguisu/article/details/7776068

总结:

来源:http://blog.csdn.net/cangchen/article/details/44816905