2.排序

作者: 司徒伯明 | 来源:发表于2018-08-19 22:22 被阅读0次

[algorithm.html](https://www.runoob.com/w3cnote/ten-sorting-algorithm.html

排序算法

冒泡排序

bubble Sort.gif
    public static void bubbleSort(int[] arr){
      for(int i = 0;i< arr.length-1;i++){//外层循环控制排序趟数
          for(int j = 0;j< arr.length-1-i;j++){//内层循环控制每一趟排序多少次
            if(arr[j]>arr[j+1]){
                int temp=arr[j];
                arr[j]=arr[j+1];
                arr[j+1]=temp;
            }
          }
      }
    }

选择排序

insertSort.gif
public  static void selectSort(int[] arr){
    for (int i = 0; i <arr.length-1 ; i++) {
        int min = i;
        for(int j = i; j < arr.length ; j++){
            if(arr[j] < arr[min]){
                min = j;
            }
        }
        int temp = arr[i];
        arr[i] = arr[min] ;
        arr[min] = temp;
    }
}

插入排序

    public static void insertSort(int[] arr){
          int j,temp;
        for(int i = 1; i < arr.length; i++){
            if(arr[i] < arr[i-1]){
                temp = arr[i];
                for(j= i - 1; j >= 0 && arr[j] > temp; j--){
                    arr[j+1] = arr[j];
                }
                arr[j+1] = temp;
            }
        }
    }

插入排序的基本思路:
将较小的插入到元素的左端;
为插入的元素腾出空间,让后将其余元素都往后移动;

需要一个图来说明啊

希尔排序

在希尔排序时一种在出入排序的基础上进行,拓展得到的一种快速排序算法

    public static void shellSort(int[] arrays){
        int length = arrays.length;
        int step = 1;
        if( step < length/3){
            step = 3 * step +1;
        }
        while (step >=1){
            for (int i = step; i < arrays.length; i++) {
                int j = i;
                int temp = arrays[i];

                // j - step 就是代表与它同组隔壁的元素
                while (j - step >= 0 && arrays[j - step] > temp) {
                    arrays[j] = arrays[j - step];
                    j = j - step;
                }
                arrays[j] = temp;
            }
            step /=3;
        }
    }

归并排序

将数组切分然后递归的使用~~

    public static void sort(int[] arrays){
    }

向上归并 和向下递归并

快速排序

设置关键字然后分组进行划分,然后递归的排序字数组
java中是如何实现的比较的?

数据结构

相关文章

  • 2.排序

    [algorithm.html](https://www.runoob.com/w3cnote/ten-sorti...

  • [算法导论]-第七章-快速排序

    本章重点 1.快速排序 2.冒泡排序 3.希尔排序 1.快速排序 2.冒泡排序 3.希尔排序 希尔排序,也称递减增...

  • 【排序算法】2.选择排序

    选择排序(Selection sort)是一种简单直观的排序算法。它的工作原理如下。首先在未排序序列中找到最小(大...

  • 七大经典排序算法总结(C语言描述)

    目录 一.交换排序 1.冒泡排序 2.快速排序 二.插入排序 1.直接插入排序 2.希尔(shell)排序 三.选...

  • 2.快速排序

    1.原理划分交换排序。它采用了一种分治的策略,通常称其为分治法(Divide-and-ConquerMethod)...

  • 2.希尔排序

    1.什么是希尔排序 对于大规模的乱序数组插入排序很慢,因为它只会交换相邻的元素,因此元素只能一点一点的从一端移动到...

  • 2.冒泡排序

    2.冒泡排序 2.1冒泡排序的思想和复杂度 冒泡思想 冒泡排序,顾名思义就是像气泡一样,大小不一的气泡会依次逐个交...

  • 2.快速排序

    Array.prototype.quick_sort = function() { var len = this....

  • 2.冒泡排序

    之前给大家介绍的桶排序想必让大家狠狠的爽了一把给别人排成绩的乐趣哈哈,不过它只能给成绩排序,这些成绩如果对应着人呢...

  • 2.希尔排序

    基本思想:先将整个待排序的记录序列分割成为若干子序列分别进行直接插入排序,待整个序列中的记录“基本有序”时,再对全...

网友评论

      本文标题:2.排序

      本文链接:https://www.haomeiwen.com/subject/lzdhiftx.html