转【算法之常用排序算法(一)】八大常用内部排序算法(快排、冒泡、希尔、堆排序等)
概述
排序有内部排序和外部排序,内部排序是数据记录在内存中进行排序,而外部排序是因排序的数据很大,一次不能容纳全部的排序记录,在排序过程中需要访问外存。
我们这里说说八大排序就是内部排序。
当n较大,则应采用时间复杂度为O(nlog2n)的排序方法:快速排序、堆排序或归并排序序。
快速排序:是目前基于比较的内部排序中被认为是最好的方法,当待排序的关键字是随机分布时,快速排序的平均时间最短;
算法的实现:
- void print(int a[], int n ,int i){
- cout<":";
- for(int j= 0; j<8; j++){
- cout<" ";
- }
- cout<
- }
-
-
- void InsertSort(int a[], int n)
- {
- for(int i= 1; i
- if(a[i] < a[i-1]){ //若第i个元素大于i-1元素,直接插入。小于的话,移动有序表后插入
- int j= i-1;
- int x = a[i]; //复制为哨兵,即存储待排序元素
- a[i] = a[i-1]; //先后移一个元素
- while(x < a[j]){ //查找在有序表的插入位置
- a[j+1] = a[j];
- j--; //元素后移
- }
- a[j+1] = x; //插入到正确位置
- }
- print(a,n,i); //打印每趟排序的结果
- }
-
- }
-
- int main(){
- int a[8] = {3,1,5,7,2,4,9,6};
- InsertSort(a,8);
- print(a,8,8);
- }
效率:
时间复杂度:O(n^2).
其他的插入排序有二分插入排序,2-路插入排序。
Java实现:
view plaincopy
- package com.weijiang.demo;
-
- public class InsertSort {
-
- public InsertSort(){
- 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};
- int temp=0;
- for(int i=1;i
- int j=i-1;
- temp=a[i];
- for(;j>=0 && temp
- a[j+1]=a[j];//将大于temp的值整体后移一个单位
- }
- a[j+1]=temp;
- }
-
- for(int i=0;i
- System.out.println(a[i]);
- }
-
- }
- package com.weijiang.demo;
- public class InsertSort {
- public InsertSort(){
- 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};
- int temp=0;
- for(int i=1;i
- int j=i-1;
- temp=a[i];
- for(;j>=0 && temp
- a[j+1]=a[j];//将大于temp的值整体后移一个单位
- }
- a[j+1]=temp;
- }
- for(int i=0;i
- System.out.println(a[i]);
- }
- }
4. 特点:每次循环一边之后,最前面的一部分一定是有序序列,但是位置不是最终的
view plaincopy
- package com.weijiang.demo;
-
- public class ShellSort {
- public ShellSort(){
- int a[]={1,54,6,3,78,34,12,45,56,100};
- double d1=a.length;
- int temp=0;
- while(true){
- d1= Math.ceil(d1/2);
- int d=(int) d1;
- for(int x=0;x
- for(int i=x+d;i
- int j=i-d;
- temp=a[i];
- for(;j>=0 && temp
- a[j+d]=a[j];
- }
- a[j+d]=temp;
- }
- }
- if(d==1)
- break;
- }
- for(int i=0;i
- System.out.println(a[i]);
-
- }
-
- }
view plaincopy
- package com.weijiang.demo;
-
- public class SelectSort {
-
- public SelectSort(){
- int a[]={1,54,6,3,78,34,12,45};
- int position=0;
- for(int i=0;i
- int j=i+1;
- position=i;
- int temp=a[i];
- for(;j
- if(a[j]
- temp=a[j];
- position=j;
- }
- }
- a[position]=a[i];
- a[i]=temp;
- }
- for(int i=0;i
- System.out.println(a[i]);
-
- }
-
- }
- package com.weijiang.demo;
- public class SelectSort {
- public SelectSort(){
- int a[]={1,54,6,3,78,34,12,45};
- int position=0;
- for(int i=0;i
- int j=i+1;
- position=i;
- int temp=a[i];
- for(;j
- if(a[j]
- temp=a[j];
- position=j;
- }
- }
- a[position]=a[i];
- a[i]=temp;
- }
- for(int i=0;i
- System.out.println(a[i]);
- }
- }
4. 特点:每次循环一边之后,最前面的一部分一定是有序的,而且这个顺序不会再改变。这个和前面的插入排序有点不一样。
view plaincopy
- package com.weijiang.demo;
-
- import java.util.Arrays;
-
- public class HeapSort {
-
- 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};
-
- public HeapSort(){
- heapSort(a);
-
- }
-
- public void heapSort(int[] a){
- System.out.println("开始排序");
- int arrayLength=a.length;
- //循环建堆
- for(int i=0;i
1;i++){
- //建堆
- buildMaxHeap(a,arrayLength-1-i);
- //交换堆顶和最后一个元素
- swap(a,0,arrayLength-1-i);
- System.out.println(Arrays.toString(a));
- }
- }
-
- private void swap(int[] data, int i, int j) {
- int tmp=data[i];
- data[i]=data[j];
- data[j]=tmp;
- }
-
- //对data数组从0到lastIndex建大顶堆
- private void buildMaxHeap(int[] data, int lastIndex) {
- //从lastIndex处节点(最后一个节点)的父节点开始
- for(int i=(lastIndex-1)/2;i>=0;i--){
- //k保存正在判断的节点
- int k=i;
- //如果当前k节点的子节点存在
- while(k*2+1<=lastIndex){
- //k节点的左子节点的索引
- int biggerIndex=2*k+1;
- //如果biggerIndex小于lastIndex,即biggerIndex+1代表的k节点的右子节点存在
- if(biggerIndex
- //若果右子节点的值较大
- if(data[biggerIndex]1]){
- //biggerIndex总是记录较大子节点的索引
- biggerIndex++;
- }
- }
-
- //如果k节点的值小于其较大的子节点的值
- if(data[k]
- //交换他们
- swap(data,k,biggerIndex);
- //将biggerIndex赋予k,开始while循环的下一次循环,重新保证k节点的值大于其左右子节点的值
- k=biggerIndex;
- }else{
- break;
- }
-
- }
-
- }
-
- }
- }
view plaincopy
- package com.weijiang.demo;
-
- public class BubbleSort {
- public BubbleSort(){
- int a[]={1,54,6,3,78,34,12,45};
- int temp=0;
- for(int i=0;i
- for(int j=i+1;j
- if(a[i]>a[j]){
- temp=a[i];
- a[i]=a[j];
- a[j]=temp;
- }
- }
- }
- for(int i=0;i
- System.out.println(a[i]);
- }
-
- }
- package com.weijiang.demo;
- public class BubbleSort {
- public BubbleSort(){
- int a[]={1,54,6,3,78,34,12,45};
- int temp=0;
- for(int i=0;i
- for(int j=i+1;j
- if(a[i]>a[j]){
- temp=a[i];
- a[i]=a[j];
- a[j]=temp;
- }
- }
- }
- for(int i=0;i
- System.out.println(a[i]);
- }
- }
经过道友的提醒,发现上面的不是正宗的冒泡排序,其实上面的相当去选择排序的变种。所以更正过来:
正宗的冒泡排序:
[java] view plaincopy
- package com.weijiang.demo;
- public class BubbleSort {
- public BubbleSort(){
- 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};
- int temp=0;
- for(int i=0;i
1;i++){ - for(int j=0;j
1-i;j++){ - if(a[j]>a[j+1]){
- temp=a[j];
- a[j]=a[j+1];
- a[j+1]=temp;
- }
- }
- }
- for(int i=0;i
- System.out.println(a[i]);
- }
- }
4. 特点:和选择排序的特点一样,每循环一边之后最前面的一部分是有序的,而且位置不会再改变了
注:上面的冒泡排序的过程我们是可以进行一些优化操作的,可以添加一个变量来记录每次有没有交换操作,如果没有的话,说明序列已经有序了,不需要在进行比较了,代码如下:
[java] view plaincopy
- package com.weijiang.demo;
- public class EnhanceBubbleSort {
- public EnhanceBubbleSort(){
- 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};
- int temp=0;
- boolean isChange = false;//记录每次有没有交换值的状态
- for(int i=0;i
1;i++){ - isChange = false;
- for(int j=0;j
1-i;j++){ - if(a[j]>a[j+1]){
- isChange = true;
- temp=a[j];
- a[j]=a[j+1];
- a[j+1]=temp;
- }
- }
- //如果一趟下来之后没有交换操作,说明数组已经有序了,直接跳出循环
- if(!isChange)
- break;
- }
- for(int i=0;i
- System.out.println(a[i]);
- }
- }
如果原始序列大部分有序了,这个效率比之前的冒泡排序效果高出很多
view plaincopy
- package com.weijia.demo;
-
- public class QuickSort {
-
- 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};
- public QuickSort(){
- quick(a);
- for(int i=0;i
- System.out.println(a[i]);
-
- }
-
- public int getMiddle(int[] list, int low, int high) {
- int tmp = list[low]; //数组的第一个作为中轴
- while (low < high) {
- while (low < high && list[high] >= tmp) {
- high--;
- }
- list[low] = list[high]; //比中轴小的记录移到低端
- while (low < high && list[low] <= tmp) {
- low++;
- }
- list[high] = list[low]; //比中轴大的记录移到高端
- }
- list[low] = tmp; //中轴记录到尾
- return low; //返回中轴的位置
-
- }
-
- public void _quickSort(int[] list, int low, int high) {
- if (low < high) {
- int middle = getMiddle(list, low, high); //将list数组进行一分为二
- _quickSort(list, low, middle - 1); //对低字表进行递归排序
- _quickSort(list, middle + 1, high); //对高字表进行递归排序
- }
- }
-
- public void quick(int[] a2) {
- if (a2.length > 0) { //查看数组是否为空
- _quickSort(a2, 0, a2.length - 1);
- }
- }
-
- }
4. 特点:每一趟结束之后,中间的数的位置不会在改变了,而且每次都是以这个中间数为中心轴的话,一部分是比这个数都小的,另外一部分都是比这个数都大的
view plaincopy
- package com.weijia.demo;
-
- import java.util.Arrays;
-
- public class MergingSort {
-
- 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};
-
- public MergingSort(){
- sort(a,0,a.length-1);
- for(int i=0;i
- System.out.println(a[i]);
- }
-
- public void sort(int[] data, int left, int right) {
- if(left
- //找出中间索引
- int center=(left+right)/2;
- //对左边数组进行递归
- sort(data,left,center);
- //对右边数组进行递归
- sort(data,center+1,right);
- //合并
- merge(data,left,center,right);
-
- }
- }
-
- public void merge(int[] data, int left, int center, int right) {
- int [] tmpArr=new int[data.length];
- int mid=center+1;
- //third记录中间数组的索引
- int third=left;
- int tmp=left;
- while(left<=center&&mid<=right){
- //从两个数组中取出最小的放入中间数组
- if(data[left]<=data[mid]){
- tmpArr[third++]=data[left++];
- }else{
- tmpArr[third++]=data[mid++];
- }
- }
- //剩余部分依次放入中间数组
- while(mid<=right){
- tmpArr[third++]=data[mid++];
- }
- while(left<=center){
- tmpArr[third++]=data[left++];
- }
- //将中间数组中的内容复制回原数组
- while(tmp<=right){
- data[tmp]=tmpArr[tmp++];
- }
- System.out.println(Arrays.toString(data));
- }
-
- }
view plaincopy
- package com.weijia.demo;
-
- import java.util.ArrayList;
- import java.util.List;
-
- public class RadixSort {
-
- 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};
-
- public RadixSort(){
- sort(a);
- for(int i=0;i
- System.out.println(a[i]);
- }
-
- public void sort(int[] array){
- //首先确定排序的趟数;
- int max=array[0];
- for(int i=1;i
- if(array[i]>max){
- max=array[i];
- }
- }
-
- int time=0;
- //判断位数;
- while(max>0){
- max/=10;
- time++;
- }
-
- //建立10个队列;
- List
queue=new ArrayList();
- for(int i=0;i<10;i++){
- ArrayList
queue1=new ArrayList();
- queue.add(queue1);
- }
-
- //进行time次分配和收集;
- for(int i=0;i
- //分配数组元素;
- for(int j=0;j
- //得到数字的第time+1位数;
- int x=array[j]%(int)Math.pow(10, i+1)/(int)Math.pow(10, i);
- ArrayList
queue2=queue.get(x);
- queue2.add(array[j]);
- queue.set(x, queue2);
- }
- int count=0;//元素计数器;
- //收集队列元素;
- for(int k=0;k<10;k++){
- while(queue.get(k).size()>0){
- ArrayList
queue3=queue.get(k);
- array[count]=queue3.get(0);
- queue3.remove(0);
- count++;
- }
- }
- }
- }
-
- }
注明:转载请提示出处:http://blog.csdn.net/hguisu/article/details/7776068
总结:
来源:http://blog.csdn.net/cangchen/article/details/44816905