六大经典排序算法 java 选择排序、插入排序、冒泡排序、快速排序、堆排序、归并排序,六大经典排序算法,

来源:互联网 发布:为什么双击打不开软件 编辑:程序博客网 时间:2024/04/28 06:09

1. [文件] ChaRuFa.java ~ 890B     下载(71)     

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
publicclass ChaRuFa {
    publicstatic void main(String[] args)
    {
        //数组声明
        int[] a = {5,1,3,0,1,4,-1};
        intL = a.length;
        //首先确保前两个元素是升序的
        if(a[1]<a[0])
        {
            inttmp=a[0];
            a[0]=a[1];
            a[1]=tmp;
        }
        //其次进行插入排序
        for(inti=2;i<L;i++)
        {
            //如果待插入的数小于或等于已排序的表头
            if(a[i]<=a[0])
            {
                inttmp=a[i];
                for(intj=i;j>0;j--)
                {
                    a[j]=a[j-1];
                }
                a[0]=tmp;
            }
            //如果待插入的数大于或等于已排序的表尾
            elseif(a[i]>=a[i-1])
                continue;
            //如果待插入的数介于已排序的表中某两个数中间
            else
            {
                for(intk=0;k<i-1;k++)
                {
                    if(a[i]>=a[k]&&a[i]<=a[k+1])
                    {
                        inttmp1=a[i];
                        for(intm=i;m>k+1;m--)
                        {
                            a[m]=a[m-1];
                        }
                        a[k+1]=tmp1;
                    }
                }
            }
        }
        for(intelement:a)
            System.out.println(element+" ");
    }
}

2. [文件] DuiPaiXu.java ~ 3KB     下载(60)     

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
importjava.util.*;
 
 
/*
 * 本程序是进行堆排序
 * 首先需要明白堆是一种特殊的完全二叉树,并且它的孩子结点永远小于父母结点
 */
 
publicclass DuiPaiXu<E extendsComparable> {
 
    publicstatic void main(String[] args) {
        //声明一个列表,存储数据
         Integer[]list={1,0,-2,3,5,1,2,10,11,21,3,2};
         heapsort(list);
          
         for(Integer element:list)
             System.out.println(element);
    }
     
    /*
     * 首先编写堆类
     * 堆类用线性列表来存储数据
     *
     *   */
    privateArrayList<E> list=newArrayList<E>();
    publicDuiPaiXu(E[] objects)
    {
        for(inti=0;i<objects.length;i++)
            add(objects[i]);
    }
     
    publicDuiPaiXu() {
         
    }
 
    publicvoid add(E object)
    {
        list.add(object);
        intcurrentindex=list.size()-1;
         
        while(currentindex>0)
        {
            intparentindex=(currentindex-1)/2;
            /*
             * 如果父类结点小于子类结点,则将它们交换值
             * 这个过程一直持续到父类结点不再小于子类结点为止
             */
            if(list.get(parentindex).compareTo(list.get(currentindex))<0)
            {
                E temp=list.get(currentindex);
                list.set(currentindex, list.get(parentindex));
                list.set(parentindex, temp);
            }
            else
                break;
            currentindex=parentindex;//把新加的数的下标更新
        }
    }
     
    @SuppressWarnings("unchecked")
    publicE remove()
    {
        if(list.size()==0)
            returnnull;
        /*
         * 基本思想是
         * remove()方法只能移除根处的元素
         * 然后重新组建堆
         * 首先把最后一个元素移到根处
         * 然后将根与它的结点进行比较,若小于其子结点,则交换下移即可
         * 直到下标最大为止
         */
        E removedObject=list.get(0);
        list.set(0, list.get(list.size()-1));
        list.remove(list.size()-1);
         
        intcurrentindex=0;
        while(currentindex<list.size())
        {
            intleftchildindex=2*currentindex+1;//左孩子
            intrightchildindex=2*currentindex+2;//右孩子
             
            if(leftchildindex>=list.size())
                break;
            /*
             * 下面语句确定左边子结点与右边子结点的大小
             * 以便将父结点与最大的子结点进行比较
             * 进而确定是否执行交换动作
             */
            intmaxindex=leftchildindex;
            if(rightchildindex<list.size())
            {
                if(list.get(maxindex).compareTo(list.get(rightchildindex))<0)
                    maxindex=rightchildindex;
            }
             
            /*
             * 下面开始执行交换动作
             * 若父结点比最大的子结点要小
             * 则交换它们
             */
            if(list.get(currentindex).compareTo(list.get(maxindex))<0)
            {
                E temp=list.get(currentindex);
                list.set(currentindex, list.get(maxindex));
                list.set(maxindex, temp);
                currentindex=maxindex;
            }
            /*
             * 直到父结点不再小于最大的子结点为止
             */
            else
                break
        }
        returnremovedObject;
    }
     
    publicstatic <E extendsComparable>voidheapsort(E[]list)
    {
        DuiPaiXu<E> heap=newDuiPaiXu<E>();    
        for(inti=0;i<list.length;i++)
            heap.add(list[i]);     
        for(inti=list.length-1;i>=0;i--)
            list[i]=heap.remove();
         
    }
     
 
}

3. [文件] GuiBingFa.java ~ 2KB     下载(55)     

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
publicclass GuiBingFa {
 
    //归并排序,首选采用分而治之的思想,运用递归方法,将数组分组两半
    publicstatic void guibing(int[] list)
    {
        intL=list.length;
        //将数组递归分成两部分
        if(L>1)
        {
            //第一部分为前一半元素
            int[] firstHalf=newint[L/2];
            //调用arrayCopy方法,将list中的前一半元素复制到firstHalf之中
            System.arraycopy(list,0, firstHalf, 0, L/2);
            //递归:将这个子数组再分成两半
            guibing(firstHalf);
            //第二部分为后一半元素(不一定与第一部分元素个数相同)
            int[] secondHalf=newint[L-L/2];
            //调用arrayCopy方法,将list中的后一半元素复制到secondHalf之中
            System.arraycopy(list, L/2, secondHalf, 0, secondHalf.length);
            //递归:将这个子数组再分成两半
            guibing(secondHalf);
            //一旦所有的递归拆分都已经结束,那么进行归并
            int[]temp=merge(firstHalf,secondHalf);
            //将temp数组的所有元素复制到list之中
            //参数的含义:源数组,首索引,目标数组,首索引,复制长度
            System.arraycopy(temp,0, list, 0, temp.length);
        }
    }
    //归并两个数组
    //因为两个数组已经是排序的,所以只需依次比较两个元素的对应值即可,"谁小就谁上"
    privatestatic int[] merge(int[] list1,int[] list2)
    {
        //声明最终数组及长度
        int[] tmp=newint[list1.length+list2.length];
        //设置索引量初始化
        intcurrent1=0,current2=0,current3=0;
        //当两个子数组均没有结束,需要比较它们的值
        while(current1<list1.length&&current2<list2.length)
        {
            if(list1[current1]<list2[current2])
                tmp[current3++]=list1[current1++];
            else
                tmp[current3++]=list2[current2++];
        }
        //如果只剩下第一个子数组了,就直接把剩下的元素依次放入最终数组之中
        while(current1<list1.length)
            tmp[current3++]=list1[current1++];
        //如果只剩下第二个子数组了,就直接把剩下的元素依次放入最终数组之中
        while(current2<list2.length)
            tmp[current3++]=list2[current2++];
        //至此,合并结束,返回最终数组
        returntmp;
    }
    publicstatic void main(String[] args) {
        //首先声明数组
        int[]a={-5,1,3,2,1,0,2,-2,0};
        intL=a.length;
        //归并排序
        guibing(a);
        //输出结果
        for(intelement:a)
            System.out.println(element);
         
    }
 
}

4. [文件] kuaisu.java ~ 2KB     下载(52)     

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
/*
 * @快速排序法
 */
publicclass kuaisu {
 
    //快速排序主程序
    publicstatic void kuaisu(int[] arr)
    {
         quickSort(arr,0,arr.length-1);
    }
    //快速排序法递归,表明要排序的数组,以及起始索引
    privatestatic void quickSort(int[]arr,intfirst,intlast)
    {
        //只要最后一个大于第一个的索引
        if(last>first)
        {
            //将数组分为两部分,并且选择第一个为主元
            intpivotIndex=partition(arr,first,last);
            //对主元分开的两个子数组进行排序
            quickSort(arr,first,pivotIndex-1);
            quickSort(arr,pivotIndex+1,last);
        }
    }
    //寻找主元并且划分数组的部分
    publicstatic int partition(int[] list,intfirst,intlast)//划分数组为两部分
    {
        //选择第一个为主元
        intpivot=list[first];
        //后面将数组划分为大于主元和小于主元的两大块
        intlow=first+1;
        inthigh=last;
         
        //如果high大于low
        while(high>low)
        {
            //如果low的元素小于pivot就继续推进
            while(low<=high&&list[low]<=pivot)
                low++;
            //如果high的元素大于pivot就继续后退
            while(low<=high&&list[high]>pivot)
                high--;
            //否则,说明出现了相反情况,则交换两个元素
            if(high>low)//当list[high]<list[low]时,就交换二者
            {
                inttemp=list[high];
                list[high]=list[low];
                list[low]=temp;
            }
        }
         
        while(high>first&&list[high]>=pivot)
            high--;
        //如果一开始选的主元比最后一个high元素要大
        if(pivot>list[high])
        {
            list[first]=list[high];
            list[high]=pivot;
            returnhigh;
        }
        else
            returnfirst;
    }
    publicstatic void main(String[] args) {
        //声明数组
        int[]arr={1,2,3,0,1,5,-1,2};
        //调用快速排序算法
        kuaisu(arr);
        //输出结果
        for(intelement:arr)
            System.out.println(element);
 
    }
}

5. [文件] MaoPaoFa.java ~ 540B     下载(53)     

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
publicclass MaoPaoFa {
     
    publicstatic void main(String[] args)
    {
        //声明数组
        int[]a={-5,1,3,2,1,0,2,-2};
        intL=a.length;
        //声明临时变量
        inttmp;
        intFlag=0;
        //若某次遍历中Flag的值保持为1,则说明排序已经完成,可以退出了
        while(Flag==0)
         {
             Flag=1;
             for(inti=0;i<L-1;i++)
             {
                 //交换相邻元素
                 if(a[i]>a[i+1])
                 {
                     tmp=a[i];
                     a[i]=a[i+1];
                     a[i+1]=tmp;
                     Flag=0;
                 }
             }
         }
        //输出结果
        for(intelement:a)
            System.out.println(element);
    }
}

6. [文件] XuanZeFa.java ~ 442B     下载(56)     跳至 [1] [2] [3] [4] [5] [6] [全屏预览]

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
publicclass XuanZeFa {
 
    publicstatic void main(String[] args)
    {
        //首先声明数组
        inta[]={-5,1,3,2,1,0,2,-2};
        intL=a.length;
        //找到最小的数,依次放到表头...
        for(inti=0;i<L;i++)
         {
            //找出最小的数
             for(intj=i+1;j<L;j++)
             {
                 if(a[j]<a[i])
                 {
                     intT=a[j];
                     a[j]=a[i];
                     a[i]=T;
                 }
             }
         }
        //输出结果
        for(intelement:a)
            System.out.println(element);
    }
}
1 0
原创粉丝点击