冒泡排序、插入排序、快速排序

例一:冒泡排序

 public class BubbleSort {
     /*
         * 冒泡排序
         *
         * 参数说明:
         *     a -- 待排序的数组
         *     n -- 数组的长度
         */
        public static void bubbleSort1(int[] a, int n) {
            int i,j;

            for (i=n-1; i>0; i--) {
                // 将a[0...i]中最大的数据放在末尾
                for (j=0; j<i; j++) {

                    if (a[j] > a[j+1]) {
                        // 交换a[j]和a[j+1]
                        int tmp = a[j];
                        a[j] = a[j+1];
                        a[j+1] = tmp;
                    }
                }
            }
        }
        
     /*
         * 冒泡排序(改进版)
         *
         * 参数说明:
         *     a -- 待排序的数组
         *     n -- 数组的长度
         */
        public static void bubbleSort2(int[] a, int n) {
            int i,j;
            int flag;                 // 标记

            for (i=n-1; i>0; i--) {

                flag = 0;            // 初始化标记为0
                // 将a[0...i]中最大的数据放在末尾
                for (j=0; j<i; j++) {
                    if (a[j] > a[j+1]) {
                        // 交换a[j]和a[j+1]
                        int tmp = a[j];
                        a[j] = a[j+1];
                        a[j+1] = tmp;

                        flag = 1;    // 若发生交换,则设标记为1
                    }
                }

                if (flag==0)
                    break;            // 若没发生交换,则说明数列已有序。
            }
        }

        public static void main(String[] args) {
            int i;
            int[] a = {9,8,5,4,2,0};

            System.out.printf("before sort:");
            for (i=0; i<a.length; i++)
                System.out.printf("%d ", a[i]);
            System.out.printf("\n");

            bubbleSort1(a, a.length);
//            bubbleSort2(a, a.length);

            System.out.printf("after  sort:");
            for (i=0; i<a.length; i++)
                System.out.printf("%d ", a[i]);
            System.out.printf("\n");
        }
 }
View Code

 例二:1、插入排序法(随机数)

 

import java.util.Random;
 
public class InsertSort {
 
    public void insertSort(int[] list) {
        // 打印第一个元素
        System.out.format("i = %d:\t", 0);
        printPart(list, 0, 0);
 
        // 第1个数肯定是有序的,从第2个数开始遍历,依次插入有序序列
        for (int i = 1; i < list.length; i++) {
            int j = 0;
            int temp = list[i]; // 取出第i个数,和前i-1个数比较后,插入合适位置
 
            // 因为前i-1个数都是从小到大的有序序列,所以只要当前比较的数(list[j])比temp大,就把这个数后移一位
            for (j = i - 1; j >= 0 && temp < list[j]; j--) {
                list[j + 1] = list[j];
            }
            list[j + 1] = temp;
 
            System.out.format("i = %d:\t", i);
            printPart(list, 0, i);
        }
    }
 
    // 打印序列
    public void printPart(int[] list, int begin, int end) {
        for (int i = 0; i < begin; i++) {
            System.out.print("\t");
        }
        for (int i = begin; i <= end; i++) {
            System.out.print(list[i] + "\t");
        }
        System.out.println();
    }
 
    public static void main(String[] args) {
        // 初始化一个随机序列
        final int MAX_SIZE = 10;
        int[] array = new int[MAX_SIZE];
        Random random = new Random();
        for (int i = 0; i < MAX_SIZE; i++) {
            array[i] = random.nextInt(MAX_SIZE);
        }
 
        // 调用冒泡排序方法
        InsertSort insert = new InsertSort();
        System.out.print("排序前:\t");
        insert.printPart(array, 0, array.length - 1);
        insert.insertSort(array);
        System.out.print("排序后:\t");
        insert.printPart(array, 0, array.length - 1);
    }
}
View Code

 2、固定数组

public class InsertSort {
 
     /*
     * 直接插入排序
     *
     * 参数说明:
     *     a -- 待排序的数组
     *     n -- 数组的长度
     */
    public static void insertSort(int[] a, int n) {
        int i, j, k;

        for (i = 1; i < n; i++) {

            //为a[i]在前面的a[0...i-1]有序区间中找一个合适的位置
            for (j = i - 1; j >= 0; j--)
                if (a[j] < a[i])
                    break;

            //如找到了一个合适的位置
            if (j != i - 1) {
                //将比a[i]大的数据向后移
                int temp = a[i];
                for (k = i - 1; k > j; k--)
                    a[k + 1] = a[k];
                //将a[i]放到正确位置上
                a[k + 1] = temp;
            }
        }
    }

    public static void main(String[] args) {
        int i;
        int[] a = {20,40,30,10,60,50};

        System.out.printf("before sort:");
        for (i=0; i<a.length; i++)
            System.out.printf("%d ", a[i]);
        System.out.printf("\n");

        insertSort(a, a.length);

        System.out.printf("after  sort:");
        for (i=0; i<a.length; i++)
            System.out.printf("%d ", a[i]);
        System.out.printf("\n");
    }
}

输出结果:
before sort:20 40 30 10 60 50 
after sort:10 20 30 40 50 60
View Code

 

例三:快速插入法  http://www.cnblogs.com/skywang12345/p/3596746.html

/**
 * 快速排序:Java
 *
 * @author skywang
 * @date 2014/03/11
 */

public class QuickSort {

    /*
     * 快速排序
     *
     * 参数说明:
     *     a -- 待排序的数组
     *     l -- 数组的左边界(例如,从起始位置开始排序,则l=0)
     *     r -- 数组的右边界(例如,排序截至到数组末尾,则r=a.length-1)
     */
    public static void quickSort(int[] a, int l, int r) {

        if (l < r) {
            int i,j,x;

            i = l;
            j = r;
            x = a[i];
            while (i < j) {
                while(i < j && a[j] > x)
                    j--; // 从右向左找第一个小于x的数
                if(i < j)
                    a[i++] = a[j];
                while(i < j && a[i] < x)
                    i++; // 从左向右找第一个大于x的数
                if(i < j)
                    a[j--] = a[i];
            }
            a[i] = x;
            quickSort(a, l, i-1); /* 递归调用 */
            quickSort(a, i+1, r); /* 递归调用 */
        }
    }

    public static void main(String[] args) {
        int i;
        int a[] = {30,40,60,10,20,50};

        System.out.printf("before sort:");
        for (i=0; i<a.length; i++)
            System.out.printf("%d ", a[i]);
        System.out.printf("\n");

        quickSort(a, 0, a.length-1);

        System.out.printf("after  sort:");
        for (i=0; i<a.length; i++)
            System.out.printf("%d ", a[i]);
        System.out.printf("\n");
    }
}
输出结果:
before sort:30 40 60 10 20 50 
after sort:10 20 30 40 50 60
View Code

 

posted @ 2018-02-07 14:51  一支穿云箭  阅读(170)  评论(0编辑  收藏  举报