排序算法

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了排序算法相关的知识,希望对你有一定的参考价值。

1、选择排序

 选择排序法先找到数列中的最小的数,然后将它放在数列的最前面。接下来,在剩下的数中找到最小数,将它放在第一个数的后面,以此类推,知道数列中仅剩一个数为止。

  具体代码如下:

package com.hyq.chapter01;

/**
 * Created by Administrator on 2016/10/8.
 */
public class SelectionSort {

    public static void sort(int[] nums){

        for (int i = 0;i<nums.length;i++){
            int min = nums[i];
            int index = i;
            for(int j = i+1;j<nums.length;j++){
                if (nums[j]<min){
                    min = nums[j];
                    index = j;
                }
            }
            if (index!=i){
                nums[index] = nums[i];
                nums[i] = min;
            }
        }

    }

    public static void main(String[] args) {
        int[] nums = {13,15,52,25,6,11,37,43,12,1};
        sort(nums);
        for (int n:nums) {
            System.out.print(n+" ");
        }
    }

}

 

2、插入排序

  插入排序法是在已经排好序的子数列中反复插入一个新元素来对数列值进行排序,直到整个数列全部排好序。代码如下:

package com.hyq.chapter01;

import java.util.Random;

/**
 * Created by Administrator on 2016/10/8.
 */
public class InsertionSort {

    public static void sort(int[] nums){

        for(int i=1;i<nums.length;i++){
            int currentNum = nums[i];
            int j;
            for(j = i-1;j>=0&&nums[j]>currentNum;j--){
                nums[j+1] = nums[j];
            }
            nums[j+1] = currentNum;
        }
    }

    public static void main(String[] args) {
        int[] nums = {13,15,52,25,6,11,37,43,12,1};
        sort(nums);
        for (int n:nums) {
            System.out.print(n+" ");
        }
    }

}

 

3、冒泡排序

  冒泡排序算法需要遍历几次数组。在每次遍历中,比较连续相邻的元素。如果某一对元素是降序,则互换它们的值;否则保持不变。代码如下:

package com.hyq.chapter03;

/**
 * Created by Administrator on 2016/10/11.
 */
public class BubbleSort {

    public static void bubbleSort(int[] list){
        boolean needNextPass = true;
        for(int k=1;k<list.length&&needNextPass;k++){
            needNextPass = false;
            for (int i=0;i<list.length-k;i++){
                if (list[i]>list[i+1]){
                    int temp = list[i];
                    list[i] = list[i+1];
                    list[i+1] = temp;
                    needNextPass = true;
                }
            }
        }
    }

    public static void main(String[] args) {
        int[] list = {1,4,25,6,7,3,6,2,0};
        bubbleSort(list);
        for (int i=0;i<list.length;i++){
            System.out.print(list[i]+" ");
        }
    }


}

 

4、归并排序

  归并排序可以描述为:算法将数组分为两半,对每部分递归地应用归并排序。在两部分都排好序后,对它们进行归并。代码如下:

package com.hyq.chapter03;

/**
 * Created by Administrator on 2016/10/11.
 */
public class MergeSort {

    public static void mergeSort(int[] list){
        if (list.length>1){
            int[] firstHalf = new int[list.length/2];
            System.arraycopy(list,0,firstHalf,0,list.length/2);
            mergeSort(firstHalf);

            int secondHalfLength = list.length-list.length/2;
            int[] secondHalf = new int[secondHalfLength];
            System.arraycopy(list,list.length/2,secondHalf,0,secondHalfLength);
            mergeSort(secondHalf);

            int[] temp = merge(firstHalf,secondHalf);
            System.arraycopy(temp,0,list,0,temp.length);
        }
    }

    private static int[] merge(int[] list1,int[] list2){
        int[] temp = new int[list1.length+list2.length];
        int current1 = 0;
        int current2 = 0;
        int current3 = 0;

        while (current1<list1.length&&current2<list2.length){
            if (list1[current1]<list2[current2]){
                temp[current3++] = list1[current1++];
            }else{
                temp[current3++] = list2[current2++];
            }
        }

        while (current1<list1.length){
            temp[current3++] = list1[current1++];
        }

        while (current2<list2.length){
            temp[current3++] = list2[current2++];
        }

        return temp;
    }


    public static void main(String[] args) {
        int[] list = {3,2,6,1,83,2,33,4,11};
        mergeSort(list);
        for (int i = 0;i<list.length;i++){
            System.out.print(list[i]+" ");
        }
    }
}

 

5、快速排序

 

以上是关于排序算法的主要内容,如果未能解决你的问题,请参考以下文章

算法排序之堆排序

快速排序-递归实现

从搜索文档中查找最小片段的算法?

在第6731次释放指针后双重免费或损坏

TimSort算法分析

以下代码片段的算法复杂度