zoukankan      html  css  js  c++  java
  • java排序,效率高的是哪种排序方法

    和所有其他语言是一样的。应该还是快速排序效率最高。

    public static void bubbleSort(int a[]) {
    int len = a.length;
    for (int i = 0; i < len - 1; i++) {
    for (int j = 0; j < len - 1 - i; j++) {
    if (a[j] > a[j + 1]) {
    int temp = a[j];
    a[j] = a[j + 1];
    a[j + 1] = temp;
    }
    }
    }
    }
    public static void selectSort(int a[]) {
    int temp = 0;
    int len = a.length;
    for (int i = 0; i < len - 1; i++) {
    int min = a[i];
    int index = i;
    for (int j = i + 1; j < len; j++) {
    if (min > a[j]) {
    min = a[j];
    index = j;
    }
    }
    temp = a[i];
    a[i] = a[index];
    a[index] = temp;
    }
    }
    public static void insertSort(int a[]) {
    int len = a.length;
    for (int i = 1; i < len; i++) {
    int temp = a[i];// 待插入的值
    int index = i;// 待插入的位置
    while (index > 0 && a[index - 1] > temp) {
    a[index] = a[index - 1];// 待插入的位置重新赋更大的值
    index--;// 位置往前移
    }
    a[index] = temp;
    }
    }
    public static int partition(int a[], int low, int height) {
    int key = a[low];
    while (low < height) {
    while (low < height && a[height] >= key)
    height--;
    a[low] = a[height];
    while (low < height && a[low] <= key)
    low++;
    a[height] = a[low];
    }
    a[low] = key;
    return low;
    }
    public static void quickSort(int a[], int low, int height) {
    if (low < height) {
    int result = partition(a, low, height);
    quickSort(a, low, result - 1);
    quickSort(a, result + 1, height);
    }
    }
    测试结果
    ------------------------------------------
    测试数据10000
    冒泡排序:120ms
    选择排序:32ms
    插入排序:20ms
    快速排序:7ms
    ------------------------------------------
    测试数据100000
    冒泡排序:13098ms
    选择排序:2334ms
    插入排序:1264ms
    快速排序:23ms
    效率差距很大啊!!!! 

  • 相关阅读:
    DBA-常用到的动态视图分析语句
    SQL Server 复制(Replication) ——事务复制搭建
    SQL Server 不同网段IP通过名称访问
    [javaEE] HTTP协议总结
    [android] 从gallery获取图片
    [android] 加载大图片到内存
    [javaEE] web应用的目录结构&配置虚拟主机
    [android] 代码注册广播接收者&利用广播调用服务的方法
    [android] 采用aidl绑定远程服务
    [Linux] Linux的环境变量
  • 原文地址:https://www.cnblogs.com/zhangruifeng/p/5700939.html
Copyright © 2011-2022 走看看