Select the sort of data structure

1. Basic Introduction

Selective sorting method also belongs to the internal order, from the data to be sorted, according to the rules specified in an element is selected, and then sorted according to the purpose of a predetermined position after the exchange.

2. Select the sort ideas

Selection Sort (select sorting) is also a simple method of sorting. The basic idea is: first from arr [0] minimum from arr [n-1], and arr [0] exchange, the second time arr [1] Select arr [n-1] of the minimum with arr [1] exchange, the third [2] from the ARR ARR [. 1-n-] selected minimum value, and ARR [2] exchange, ..., i th from ARR [. 1-i] ARR [N- 1] selected minimum value, and arr [i-1] exchange, ..., the n-1 times [n-2] ~ arr [ n-1] from a selected minimum value ARR with arr [n-2] exchange total by n 1-times to obtain an ordered sequence of symbols sorted in ascending order.

3. Select the sort ideas analysis

7149586-6892a578ebef5872.png
1559804180617.png

3.1 ideas illustration

7149586-215f909ddfa22b09.png
1559804360746.png

Description:

  1. Select the sort a total array size - a sort
  2. Each one sort, is a loop that rules (code)
    2.1 The first assumes that the current number is the minimum number
    2.2 and later and each number are compared, if there are a number smaller than the current number, to redefine minimum number, and subscript give
    2.3 when traversing the final array, is obtained and the minimum number of round subscript
    2.4 exchange

3.2 code implementation

package cn.smallmartial.sort;

import java.util.Arrays;

/**
 * @Author smallmartial
 * @Date 2019/6/6
 * @Email [email protected]
 */
public class SelectSort {
    public static void main(String[] args) {
        int[] arr = {101, 34, 119, 1};
        System.out.println("排序前:"+Arrays.toString(arr));
        selectSort(arr);
    }
    //选择排序
    public static void selectSort(int[] arr){

        //使用嵌套循环
        for (int i = 0; i < arr.length - 1; i++) {
            int minIndex = i;
            int min = arr[i];

            for (int j = i + 1; j < arr.length; j++) {
                if (min >arr[j]){
                    min = arr[j];
                    minIndex = j;
                }
            }
            //将最小值,放到arr[0],即交换
            if(minIndex != i){
                arr[minIndex] = arr[i];
                arr[i] = min;
            }

            System.out.println("第"+(i+1)+"轮:");
            System.out.println(Arrays.toString(arr));
        }


        //逐步推导
        int minIndex = 0;
        int min = arr[0];

        for (int j = 0 + 1; j < arr.length; j++) {
            if (min >arr[j]){
                min = arr[j];
                minIndex = j;
            }
        }
        //将最小值,放到arr[0],即交换
        if(minIndex != 0){
            arr[minIndex] = arr[0];
            arr[0] = min;
        }

        System.out.println("第一轮:");
        System.out.println(Arrays.toString(arr));

        //第二轮
         minIndex = 1;
         min = arr[1];

        for (int j = 1 + 1; j < arr.length; j++) {
            if (min >arr[j]){
                min = arr[j];
                minIndex = j;
            }
        }
        //将最小值,放到arr[0],即交换
        if(minIndex != 1){
            arr[minIndex] = arr[1];
            arr[1] = min;
        }
        System.out.println("第二轮:");
        System.out.println(Arrays.toString(arr));

        //第三轮
        minIndex = 2;
        min = arr[2];

        for (int j = 2 + 1; j < arr.length; j++) {
            if (min >arr[j]){
                min = arr[j];
                minIndex = j;
            }
        }
        //将最小值,放到arr[0],即交换
        if(minIndex !=2){
            arr[minIndex] = arr[2];
            arr[2] = min;
        }
        System.out.println("第二轮:");
        System.out.println(Arrays.toString(arr));
    }
}

operation result:

7149586-75b0fdb22d28010b.png
1559805624831.png

3.3 Test eighty thousand data

    public static void main(String[] args) {
        //int[] arr = {101, 34, 119, 1};
        int[] arr = new int[80000];
        for (int i = 0; i < arr.length -1 ; i++) {
            arr[i] = (int)(Math.random()*80000);
        }
        System.out.println("排序前");
       // System.out.println("排序前:"+Arrays.toString(arr));
        Date data = new Date();
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        String dataStr = simpleDateFormat.format(data);
        System.out.println("排序前的时间"+dataStr);
        selectSort(arr);
        System.out.println("排序后");
        //System.out.println("排序后:"+Arrays.toString(arr));
        Date data2 = new Date();
        // SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        String data2Str = simpleDateFormat.format(data2);
        System.out.println("排序前的时间"+data2Str);

    }

operation result;


7149586-1d48c872f4735cbb.png
1559805979124.png

Reproduced in: https: //www.jianshu.com/p/2876d74288bd

Guess you like

Origin blog.csdn.net/weixin_34220179/article/details/91138389