Common sorting algorithms for Android development

1. Bubble sort

Bubble sort, the array is arranged from small to large, and adjacent elements are compared. If the first one is bigger than the second, swap them two.

Example:

/*

     * 冒泡排序

     * 前后两个元素相比较,最大的往后排,每一轮都可以找到一个最大的元素

     */

    publicstaticint[] bubbleSort(int[] nums){

        int temp =0;

        for (int i = 0; i < nums.length-1; i++) {

            for (int j = 0; j < nums.length-1-i; j++) {

                if (nums[j]>nums[j+1]){

                    temp= nums[j];

                    nums[j]=nums[j+1];

                    nums[j+1]= temp;

                }

            }

        }

        return nums;

}

Before sorting: {3,1,4,9,5,10,6,19,0,7,2,3}

After sorting: [0,1, 2, 3, 3, 4, 5, 6, 7, 9, 10, 19]

Time complexity: O(n 2 )

2. Quick sort

Quick sorting is to divide the records to be sorted into two independent parts by sorting. One part of the records has a smaller keyword than the other part. Then the two parts are sorted separately until the entire sequence is in order.

Example:

/*

     * 快速排序

     * 取第一个元素为基数,循环比较最小位置值与最大位置值,较大的往后排,较小的往前排,

     * 然后并较小的位数加一,较大的位数减一,循环结果就是中间位数,

     * 最后就是以中间两部分分开做递归运算再次比较排列,直到正确排序为止

     */

    publicstaticvoid quickSort(int[] nums, int low, int high) {

        if (low < high) {

            int temp = nums[low]; // 选定的基准值(第一个数值作为基准值)

            int middle; // 记录临时中间值

            int i = low, j = high;

            do {

                while ((nums[i] < temp)&& (i < high))

                    i++;

                while ((nums[j] > temp)&& (j > low))

                    j--;

                if (i <= j) {

                    middle= nums[i];

                    nums[i]= nums[j];

                    nums[j]= middle;

                    i++;

                    j--;

                }

            }while(i <= j);

            if (low < j)

                quickSort(nums,low, j);

            if (high > i)

                quickSort(nums,i, high);

        }

}

Before sorting: {3,1,4,9,5,10,6,19,0,7,2,3}

After sorting: [0,1, 2, 3, 3, 4, 5, 6, 7, 9, 10, 19]

Time complexity: O(n 2 )

3. Select sort

Select sorting, each time find the smallest value in the sequence, and then put it in the first position.

Example:

/*

     * 选择排序

     * 从后往前排,设默认最小元素与较大位置比较,如果比最小元素大,

     * 则将该位置记为最小元素,并且循环比较最小元素,找到最小元素,

     * 最后将最小元素赋值最前方,

     */

    publicstaticvoid selectSort(int[] nums) {  

        int size = nums.length, temp;  

        for (int i = 0; i < size; i++) {  

            int k = i; //临时最小元素位数

            for (int j = size - 1; j >i; j--)  {  

                if (nums[j] < nums[k])  k = j;  

            }  

            temp = nums[i];  

            nums[i] = nums[k];  

            nums[k] = temp;  

        }  

} 

Before sorting: {3,1,4,9,5,10,6,19,0,7,2,3}

After sorting: [0,1, 2, 3, 3, 4, 5, 6, 7, 9, 10, 19]

Time complexity: O(n 2 )

4. Insertion sort

Insertion sort, by constructing an ordered sequence, for unsorted data, scan from back to front in the sorted sequence, find the corresponding position and insert.

Example:

/*

     * 插入排序

     * 基准元素相邻元素比较,较大的往后排,然后从较大的位置开始往后与基准元素比较排列,

     * 最后把最小的插入较小位

     */

    publicstaticvoid insertSort(int[] nums) {  

        int size = nums.length, temp, j;  

        for(int i=1; i<size; i++) {  

            temp = nums[i];//基准元素  

            for(j = i; j > 0 && temp < nums[j-1];j--)  

                nums[j] = nums[j-1];  

            nums[j] = temp;  

        }  

}

Before sorting: {3,1,4,9,5,10,6,19,0,7,2,3}

After sorting: [0,1, 2, 3, 3, 4, 5, 6, 7, 9, 10, 19]

Time complexity: O(n 2 )

Guess you like

Origin blog.csdn.net/xhf_123/article/details/78990929