排序算法比较

快速排序参考:
http://blog.csdn.net/morewindows/article/details/6684558
希尔排序参考:
http://blog.csdn.net/yy_done/article/details/7102661
九种排序算法效率比较:

http://blog.csdn.net/cjf_iceking/article/details/7953637


public class TestSort {
   
    public static void main(String[] args) {
        int[] a={1,2,10,4,8,3};
        bubbleSort(a);                  //冒泡排序
        for(int i=0; i
            System.out.print(" "+a[i]);
        }
        System.out.println();
               
        int[] b={1,2,12,4,5,3};
        selectSort(b);                  //选择排序
        for(int i=0; i
            System.out.print(" "+b[i]);
        }
        System.out.println();
       
        int[] c={1,2,12,4,5,3};
        insertSort(c);                  //选择排序
        for(int i=0; i
            System.out.print(" "+c[i]);
        }
        System.out.println();
       
        int[] d={1,8,17,4,5,3};
        shellSort(d);                  //希尔排序
        for(int i=0; i
            System.out.print(" "+d[i]);
        }
        System.out.println();
       
        int[] e={1,8,17,4,6,3};
        quickSort(e,0,e.length-1);                  //快速排序
        for(int i=0; i
            System.out.print(" "+e[i]);
        }
        System.out.println();
       
    }
   
    public static void bubbleSort(int[] source) {   //冒泡排序,source[j]和source[j+1] 比较,大的往下沉
        for(int i=0; i
            for(int j=0; j
            {
                if(source[j] > source[j+1]) {
                    int temp = source[j];
                    source[j] = source[j+1];
                    source[j+1] = temp;
                }
            }
       
    }
   
    public static void selectSort(int[] source) { //选择排序,第一个和后面每一个比较,最小的放第一个,第二个和后面比较,依此类推
        for(int i=0; i
            for(int j=i+1; j
                if(source[i]>source[j]) {
                    int temp = source[i];
                    source[i] = source[j];
                    source[j] = temp;
                }
            }
       
    }
   
    public static void insertSort(int[] source) {//插入排序,将数组分成有序+无序,我这个版本比较易于自己理解
        for (int i=1; i
            int j = i;
            while(source[j]
                int tmp = source[j];
                source[j] = source[j-1];
                source[j-1] =tmp;
                j--;
                if(j==0) break;//防止数组越界
            }
        }
           
       
    }
   
    public static void shellSort(int[] source) {//希尔排序,时间复杂度和增量序列有关系,了解即可,一种插入排序的改进,不太熟悉
    int gap = source.length/2;
    int temp;
    while(gap>0) {
        for(int i=gap; i
            temp = source[i];
            int j = i - gap;
            while(j>=0 && temp < source[j]) {
                source[j+gap] = source[j];
                j = j - gap;
            }
            source[j+gap] = temp;
        }
        gap = gap/2;
    }   
    }
   
    public static void quickSort(int[] source, int start, int end) {//快速排序,测试下面效率最高
        if(start
        {
            int i=start, j=end, x=source[start];
            while(i
            {
                while(i=x)
                    j--;
                if(i
                    source[i++]=source[j];
                while(i
                    i++;
                if(i
                    source[j--]=source[i];
            }
            source[i]=x;
            quickSort(source, start,i-1);
            quickSort(source,i+1,end);
        }
    }


}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值