zoukankan      html  css  js  c++  java
  • 排序算法(六)

    1. 交换排序—冒泡排序(Bubble Sort)

    基本思想:

    排序的一组数中,对当前还未排好序的范围内的全部数,自上而下对相邻的俩个数依次进行比较

    和调整,让较大的数下沉,较小的数往上冒。即:每当俩相邻的数比较后发现他们的排序与排序的要求相反时,就将他们交换。

    冒泡排序示例:

    算法的实现:

    public class BubbleSort2 {
    
        public static void main(String[] args) {
            int[] a = {12,43,65,72,87,21,98,21,911,679,22,4,1,8,2456,32};
            bubbleSort(a,a.length);
            
            for(int i=0; i<a.length; i++){
                System.out.print(a[i]+" ");
            }
        }
        public static void bubbleSort(int[] a,int n){
            int temp;
            for(int i=0; i<n-1; i++){
                for(int j=0; j<n-1-i; j++){
                    if(a[j] > a[j+1]){
                        temp = a[j];
                        a[j] = a[j+1];
                        a[j+1] = temp;
                    }
                }
            }
        }
    }

    冒泡排序算法的改进:

     对冒泡排序的改进是加入一标志性变量exchange,用于标志某一趟排序过程中是否有数据交换,如果进行某一趟排序时,并没有数据交换,则说明数据已经按要求排列好,可立即结束排序,避免不必要的比较过程,本文提供一下俩种改进算法:

    1、设置一标志性变量pos,用于记录每一趟排序过程中最后一交换数据的位置。由于pos位置之后的数据已经按要求排列好了,所以下一趟排序的时候只需要扫描到pos位置即可。

    改进后算法如下:

    public class BubbleSort3 {
    
        public static void main(String[] args) {
            int[] a = { 12, 43, 65, 72, 87, 21, 98, 21, 911, 679, 22, 4, 1, 8,
                    2456, 32 };
            bubbleSort(a, a.length);
    
            for (int i = 0; i < a.length; i++) {
                System.out.print(a[i] + " ");
            }
        }
    
        public static void bubbleSort(int[] a, int n) {
            int i = n - 1; // 初始时,最后位置保持不变
            while (i > 0) {
                int pos = 0; // 每趟开始时,无记录交换
                for (int j = 0; j < i; j++) {
                    if (a[j] > a[j + 1]) {
                        pos = j; // 记录交换的位置
                        int temp = a[j];
                        a[j] = a[j + 1];
                        a[j + 1] = temp;
                    }
    
                }
                i = pos;// 为下一趟排序作准备
            }
        }
    }

    2、传统冒泡排序每一趟冒泡排序只能找到一个最大值或者最小值,我们考虑利用在每趟排序中进行正向和反向俩遍的冒泡方法一次可以得到俩个值(最大值和最小值),从而使排序趟数几乎减少一半。

    改进后的算法为:

    public class BubbleSort4 {
    
        public static void main(String[] args) {
            int[] a = { 12, 43, 65, 72, 87, 21, 98, 21, 911, 679, 22, 4, 1, 8,
                    2456, 32 };
            bubbleSort(a, a.length);
    
            for (int i = 0; i < a.length; i++) {
                System.out.print(a[i] + " ");
            }
        }
    
        public static void bubbleSort(int[] a, int n) {
            int low = 0;
            int high = n - 1;
            int temp, j;
            while (low < high) {
                for (j = low; j < high; ++j) { // 正向冒泡,找到最大值
                    if (a[j] > a[j + 1]) {
                        temp = a[j];
                        a[j] = a[j + 1];
                        a[j + 1] = temp;
                    }
    
                }
                --high;// 修改high值, 前移一位
                for (j = high; j > low; --j) {// 反向冒泡,找到最小者
                    if (a[j] < a[j - 1]) {
                        temp = a[j];
                        a[j] = a[j - 1];
                        a[j - 1] = temp;
                    }
    
                }
                ++low;// 修改low值,后移一位
            }
        }
    }
  • 相关阅读:
    【SSH网上商城项目实战25】使用java email给用户发送邮件
    14个Xcode中常用的快捷键操作
    图文解释XCode常用快捷键的使用
    **iOS开发系列--IOS程序开发概览
    IOS:类方法(静态方法)和实例方法
    IOS:利用dispatch_once创建单例
    ios 沙盒 NSCoding(相当于JAVA对象序列化) 归档 数据存储
    NSString+URLEncoding.h --使用Obj-C对数据等进行URLEncoding编码
    iOS开发网络篇—NSURLConnection基本使用
    Block、委托、回调函数原理剖析(在Object C语境)——这样讲还不懂,根本不可能!
  • 原文地址:https://www.cnblogs.com/Gaojiecai/p/4082451.html
Copyright © 2011-2022 走看看