Loading

排序算法

待排序数组 int[] num={1,9,3,0,7,5,4}; 记数组长度为i

1.冒泡排序

算法描述:进行i-1个轮次的比较,每个轮次将两两靠近的数字进行比较,将大的数字右移,以上述待排序数组为例

第一轮 第一次 (1和9比较) 1 9 3 0 7 5 4 

第一轮 第二次 (9和3比较) 1 3 9 0 7 5 4

第一轮 第三次 (9和0比较) 1 3 0 9 7 5 4

第一轮 第四次 (9和7比较) 1 3 0 7 9 5 4

第一轮 第五次 (9和5比较) 1 3 0 7 5 9 4

第一轮 第六次 (9和4比较) 1 3 0 7 5 4 9

 

第二轮 第一次 (1和3比较) 1 3 0 7 5 4 9

第二轮 第二次 (3和0比较) 1 0 3 7 5 4 9

第二轮 第三次 (3和7比较) 1 0 3 7 5 4 9

第二轮 第四次 (7和5比较) 1 0 3 5 7 4 9

第二轮 第五次 (7和4比较) 1 0 3 5 4 7 9

 

第三了 第一次 (1和0比较) 0 1 3 5 4 7 9

 

示例代码:

package com.test.sort;

/**
 * @author wangx
 * @Date: 2016年8月4日 
 * @func: 冒泡排序
 * @Copyright: 2016 wangx. All rights reserved.
 */
public class BubbleSort {
    private static int[] num={1,9,3,0,7,5,4};
    private static int temp;
    public static void main(String[] args) {
        sort();
    }
    public static void sort() {
        show();
        for (int i = 0; i < num.length-1; i++) {
            for (int j = 0; j < num.length-i-1; j++) {
                if(num[j]>num[j+1]) {
                    temp=num[j];
                    num[j]=num[j+1];
                    num[j+1]=temp;
                }
            }
        }
        show();
    }
    public static void show() {
        for (int i = 0; i < num.length; i++) {
            System.out.print(num[i]);
            System.out.print(" ");
        }
        System.out.println(" ");
    }
}

2.选择排序

算法描述:i-1个轮次的排序,比如第一轮时,将第一个数字与其后的每一个数字进行比较,寻找最小的数字,并与第一个数字进行位置交换,第二轮时,将第二个数字与其后的每一个数字进行比较,找出最小的数字,并与第二个数字进行位置交换

第一轮 第一次 (1和9比较) 1 9 3 0 7 5 4

第一轮 第二次 (1和3比较) 1 9 3 0 7 5 4

第一轮 第三次 (1和0比较) 0 9 3 1 7 5 4

第一轮 第四次 (0和7比较) 0 9 3 1 7 5 4

第一轮 第五次 (0和5比较) 0 9 3 1 7 5 4

第一轮 第六次 (0和4比较) 0 9 3 1 7 5 4

 

第二轮 第一次 (9和3比较) 0 3 9 1 7 5 4

第二轮 第二次 (3和1比较) 0 1 9 3 7 5 4

第二轮 第三次 (1和7比较) 0 1 9 3 7 5 4

 

代码示例:

package com.test.sort;

/**
 * @author wangx
 * @Date: 2016年8月4日 
 * @func: 选择排序
 * @Copyright: 2016 wangx. All rights reserved.
 */
public class SelectionSort {
    
    private static int[] num={1,9,3,0,7,5,4};
    private static int temp;
    public static void main(String[] args) {
        show();
        sort();
        show();
    }
    public static void sort() {
        for (int i = 0; i < num.length; i++) {
            for (int j = i+1; j < num.length; j++) {
                if(num[j] < num[i]) {
                    temp = num[i];
                    num[i] = num[j];
                    num[j] = temp;
                }
            }
        }
    }
    public static void show() {
        for (int i = 0; i < num.length; i++) {
            System.out.print(num[i]);
            System.out.print(" ");
        }
        System.out.println(" ");
    } 
}

 

3.插入排序

算法描述:和扑克牌一样,每当右手拿到一张牌,便插入到左手合适的位置,这样左手的牌一直是排序好的。其核心思想就是"固定元素找位置"

示例代码:

package com.test.sort;

/**
 * @author wangx
 * @Date: 2016年8月4日 
 * @func: 插入排序
 * @Copyright: 2016 wangx. All rights reserved.
 */
public class InsertSort {
    private static int[] num={1,9,3,0,7,5,4};
    public static void main(String[] args) {
        show(num);
        mysort();
        show(num);
    }
    //固定的元素num[i] 找位置j
    public static void mysort() {
        int temp;
        for (int i = 0; i < num.length; i++) {
            int j=i;
            temp = num[i];
            while(j>0 && temp<num[j-1]) {
                num[j] = num[j-1];
                j--;
            }
            num[j]=temp;
        }
    }
    public static void show(int array[]) {
        for (int i = 0; i < array.length; i++) {
            System.out.print(array[i]);
            System.out.print(" ");
        }
        System.out.println(" ");
    }
}

 4.快速排序

算法描述:核心思想就是"挖坑填数,分而治之";不妨取数组第一个数num[0]为基准数,使用一个变量比如pivot先保存起来,从num[num.length-1]开始向左,找一个比基准数小的数,从找到的地方挖出来,填到num[0]里面去,这时候就出现了坑,然后从num[1]开始向右找,当然下标是不能大于或等于之前向左找形成的下标的,找一个比基准数大的数,找到之后,挖出来,填到之前形成的坑中,于是又形成了新的坑,如此往复,知道左游标和右游标相等,此时整个数组被分成了两部分,以基准数为间隔,左边的数都比基准数要小,右边的数都比基准数要大,接着对两部分的数以递归的形式执行上面的算法,最终就将整个数组进行了排序。

示例代码:

package com.test.sort;

/**
 * @author Ash
 * @date: 2016年8月6日 上午12:50:05 
 * @func: 快速排序
 * @email 408657544@qq.com
 * @Copyright: 2016 Ash. All rights reserved.
 */
public class QuickSort {
    private static int[] num={1,9,3,0,7,5,4,8,6,2,11};
    private static int[] sortedArray = new int[num.length];
    private static int temp;
    public static void main(String[] args) {
        show(num);
        sort(num,0,num.length-1);
        show(num);
    }

    public static void sort(int[] num, int left, int right) {
        if (left < right) {
            int origleft = left;
            int origright = right;
            int pivot = num[left]; // 基准数

            while (left < right) {
                // System.out.println("left: "+left + " right: "+right);

                while (left < right && num[right] >= pivot)
                    right--;
                if (left < right)
                    num[left++] = num[right];
                while (left < right && num[left] <= pivot)
                    left++;
                if (left < right)
                    num[right--] = num[left];
                
            }
            num[left] = pivot;
            sort(num, origleft, left - 1);
            sort(num, right + 1, origright);
        }
    }

    public static void show(int array[]) {
        for (int i = 0; i < array.length; i++) {
            System.out.print(array[i]);
            System.out.print(" ");
        }
        System.out.println(" ");
    }
}

 

posted @ 2016-08-04 11:44  注销111  阅读(182)  评论(0编辑  收藏  举报