黑马程序员技术交流社区

标题: java程序员必知的8大排序(1,2) [打印本页]

作者: 乐滋滋儿    时间: 2015-9-19 19:11
标题: java程序员必知的8大排序(1,2)
由于发帖限制本贴将分为4个小帖子组成8大排序的介绍


1, 直接插入排序

   (1)基本思想:在要排序的一组数中,假设前面(n-1)[n>=2] 个数已经是排

好顺序的,现在要把第n个数插到前面的有序数中,使得这n个数

也是排好顺序的。如此反复循环,直到全部排好顺序。

(2)实例


(3)用java实现

[backcolor=rgb(27, 36, 38) !important][size=1em]1

[backcolor=rgb(27, 36, 38) !important][size=1em]2

[backcolor=rgb(27, 36, 38) !important][size=1em]3

[backcolor=rgb(27, 36, 38) !important][size=1em]4

[backcolor=rgb(27, 36, 38) !important][size=1em]5

[backcolor=rgb(27, 36, 38) !important][size=1em]6

[backcolor=rgb(27, 36, 38) !important][size=1em]7

[backcolor=rgb(27, 36, 38) !important][size=1em]8

[backcolor=rgb(27, 36, 38) !important][size=1em]9

[backcolor=rgb(27, 36, 38) !important][size=1em]10

[backcolor=rgb(27, 36, 38) !important][size=1em]11

[backcolor=rgb(27, 36, 38) !important][size=1em]12

[backcolor=rgb(27, 36, 38) !important][size=1em]13

[backcolor=rgb(27, 36, 38) !important][size=1em]14

[backcolor=rgb(27, 36, 38) !important][size=1em]15

[backcolor=rgb(27, 36, 38) !important][size=1em]16

[backcolor=rgb(27, 36, 38) !important][size=1em]17

[backcolor=rgb(27, 36, 38) !important][size=1em]18

[size=1em][backcolor=rgb(27, 36, 38) !important][size=1em]package com.njue;
[backcolor=rgb(27, 36, 38) !important][size=1em]  
[backcolor=rgb(27, 36, 38) !important][size=1em]public class insertSort {
[backcolor=rgb(27, 36, 38) !important][size=1em]public insertSort(){
[backcolor=rgb(27, 36, 38) !important][size=1em]    inta[]={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};
[backcolor=rgb(27, 36, 38) !important][size=1em]    int temp=0;
[backcolor=rgb(27, 36, 38) !important][size=1em]    for(int i=1;i<a.length;i++){
[backcolor=rgb(27, 36, 38) !important][size=1em]       int j=i-1;
[backcolor=rgb(27, 36, 38) !important][size=1em]       temp=a;
[backcolor=rgb(27, 36, 38) !important][size=1em]       for(;j>=0&&temp<a[j];j--){
[backcolor=rgb(27, 36, 38) !important][size=1em]       a[j+1]=a[j];                       //将大于temp的值整体后移一个单位
[backcolor=rgb(27, 36, 38) !important][size=1em]       }
[backcolor=rgb(27, 36, 38) !important][size=1em]       a[j+1]=temp;
[backcolor=rgb(27, 36, 38) !important][size=1em]    }
[backcolor=rgb(27, 36, 38) !important][size=1em]    for(int i=0;i<a.length;i++)
[backcolor=rgb(27, 36, 38) !important][size=1em]       System.out.println(a);
[backcolor=rgb(27, 36, 38) !important][size=1em]}
[backcolor=rgb(27, 36, 38) !important][size=1em]}

2,           希尔排序(最小增量排序)


(1)基本思想:算法先将要排序的一组数按某个增量d(n/2,n为要排序数的个数)分成若干组,每组中记录的下标相差d.对每组中全部元素进行直接插入排序,然后再用一个较小的增量(d/2)对它进行分组,在每组中再进行直接插入排序。当增量减到1时,进行直接插入排序后,排序完成。

(2)实例:



(3)用java实现

[backcolor=rgb(27, 36, 38) !important][size=1em][backcolor=rgb(67, 90, 95) !important]
[size=1em]?

[size=1em]1

[size=1em]2

[size=1em]3

[size=1em]4

[size=1em]5

[size=1em]6

[size=1em]7

[size=1em]8

[size=1em]9

[size=1em]10

[size=1em]11

[size=1em]12

[size=1em]13

[size=1em]14

[size=1em]15

[size=1em]16

[size=1em]17

[size=1em]18

[size=1em]19

[size=1em]20

[size=1em]21

[size=1em]22

[size=1em]23

[size=1em]24

[size=1em]25

[size=1em][size=1em]public class shellSort {
[size=1em]public  shellSort(){
[size=1em]    int a[]={1,54,6,3,78,34,12,45,56,100};
[size=1em]    double d1=a.length;
[size=1em]    int temp=0;
[size=1em]    while(true){
[size=1em]        d1= Math.ceil(d1/2);
[size=1em]        int d=(int) d1;
[size=1em]        for(int x=0;x<d;x++){
[size=1em]            for(int i=x+d;i<a.length;i+=d){
[size=1em]                int j=i-d;
[size=1em]                temp=a;
[size=1em]                for(;j>=0&&temp<a[j];j-=d){
[size=1em]                a[j+d]=a[j];
[size=1em]                }
[size=1em]                a[j+d]=temp;
[size=1em]            }
[size=1em]        }
[size=1em]        if(d==1)
[size=1em]            break;
[size=1em]    }
[size=1em]    for(int i=0;i<a.length;i++)
[size=1em]        System.out.println(a);
[size=1em]}
[size=1em]}










作者: becky_ny    时间: 2015-9-25 20:37
哇 这个帖子好流弊 啥都看不懂....得努力学呢
作者: michael_wlq    时间: 2015-9-25 21:45
乱码啊。。。各种格式
作者: iceknc    时间: 2015-9-25 22:30
这是什么鬼哦
作者: ringfingers    时间: 2015-9-25 22:31
6666666666666
作者: silencea    时间: 2015-9-25 22:44
谢谢大神,已收藏
作者: qiaozengji668    时间: 2015-9-25 22:47
顶一下!!!
作者: zllqxy    时间: 2015-9-25 22:48
好多都不会
作者: 舒玉琛    时间: 2015-9-25 22:59
大神的世界我不懂
作者: outxf003    时间: 2015-9-25 23:21
大神的世界真的不懂啊!!!
作者: 18204509116    时间: 2015-9-25 23:24
签到签到   支持黑马 加油!
作者: 大漠孤星    时间: 2015-9-25 23:47
哇,这么多排序方法啊,学习了!
作者: 王海彬    时间: 2015-9-26 00:00
厉害,这个东西没去学过,长知识了

作者: 且听风_R0pZK    时间: 2015-9-26 00:12
很详细啊,非常感谢分享!




欢迎光临 黑马程序员技术交流社区 (http://bbs.itheima.com/) 黑马程序员IT技术论坛 X3.2