JAVA sorted array (selection sort, bubble sort, insertion sort, sort count)

A. Selection Sort

 

1. Basic principles

假设要按升序排列一个数列。选择排序法先找到数列中最小的数,然后将它和第一个元素交换。接下来,在剩下的数中找到最小数,将它和第二个元素交换,依此类推,直到数列中仅剩一个数为止。

 

2. The ordering process

An array {49,38,65,97,76,13,27,49} Example:

第一次 : [13, 38, 65, 97, 76, 49, 27, 49] 
第二次 : [13, 27, 65, 97, 76, 49, 38, 49] 
第三次 : [13, 27, 38, 97, 76, 49, 65, 49] 
第四次 : [13, 27, 38, 49, 76, 97, 65, 49]
第五次 : [13, 27, 38, 49, 49, 97, 65, 76] 
第六次 : [13, 27, 38, 49, 49, 65, 97, 76] 
第七次 : [13, 27, 38, 49, 49, 65, 76, 97]

3. complexity

 Selection sort time complexity of O (n ^ 2).

 The first n elements to check, but then the number of elements in order to check for the n - 1, n - 2, ..., 2 and 1. Average number of elements of each inspection 1/2 * n, so running time n * 1/2 * n, writing simple O (n ^ 2).

 

4.JAVA achieve the following:

class selectSort{
    public static void main(String[]args){
        int[] arr={49,38,65,97,76,13,27,49};
        for(int i=0;i<arr.length-1;i++){             //-1是因为没有必要进行最后一个数
            for(int j=i+1;j<arr.length;j++){
                if(arr[i]>arr[j]){
                    swap(arr,i,j);//即用即释放
                }
            }
        }
        show(arr);
    }
    public static void swap(int []arr,int i,int j ){
        int temp=arr[i];
        arr[i]=arr[j];
        arr[j]=temp;
    }
    public static void show(int[]arr){
        String s="[";
        for(int i=0;i<arr.length;i++){
            if(i==arr.length-1){
                s+=arr[i]+"]";

            }else{
                s+=arr[i]+",";
            }
        }
        System.out.println(s);
    }
}

Output:

[13, 27, 38, 49, 49, 65, 76, 97]

 

 

II. Bubble Sort

1. Rationale:

对比相邻的元素值,如果满足前一个元素值小于后一个元素值就交换元素值,把较小的元素移动到数组的前面(从小到大排序),把大的元素移动到数组的后面,即交换两个元素的位置,这样较小的元素就像气泡一样一直从底部上升到顶部。

2. The ordering process

An array [5,2,4,6,1,3] as an example:


第一次:[2,4,5,1,3,6]
第二次:[2,4,1,3,5,6]
第三次:[2,1,3,4,5,6]
第四次:[1,2,3,4,5,6]

3. implement the algorithm

Double-cycle algorithm the bubble, wherein the outer loop for controlling the sorting rounds, usually to sort the array length minus one, because the last cycle only one array element, no contrast, while the sorting has been completed . The main inner loop is adjacent the size of each element in the array for comparison to determine whether the exchange position, and comparison with the number of exchanges of sorting rounds reduced.

 

4.JAVA achieve the following :

class bubbleSort{
    public static void main(String[]args){
        int[] arr={5,2,4,6,1,3};
        for(int i=0;i<arr.length-1;i++){
            for(int j=0;j<arr.length-1-i;j++){
                if(arr[j]>arr[j+1]){
                    swap(arr,j,j+1);
                }
            }
        }
        show(arr);

    }
    public static void swap(int[]arr,int i,int j){
        int temp=arr[i];
        arr[i]=arr[j];
        arr[j]=temp;

    }
    public static void show(int [] arr){
        String s="[";
        for(int i=0;i<arr.length;i++){
            if(i==arr.length-1){
                s+=arr[i]+"]";
            }else{
                s+=arr[i]+",";
            }
        }
        System.out.println(s);
    }
}

Output:

[1,2,3,4,5,6]

 

 

III. Insertion Sort

1. Examples explain an insertion sort: [6,4,7,5]

Use 6,4, 7,5 example to explain the insertion sort: the
first is the first insertion sort, first find the first data 6, 6 because the front did not, so this time it will not be insertion sort, so the first result is 6,4,7,5

Is then inserted into the second sort order of the data in this case is 6,4,7,5, and then start the second data is 4, then put the data and the 4 previously arranged sequence is compared, at this time row 6 is good data, so in this case is 4 and 6 are compared, since 4 is less than 6, it will be the case 4 will go to the front of 6, so the result at this time is 4, 6, 5

7 is the third data and the order data at this time has become 4,6,7,5, and 7 will be the previously arranged sequence and comparing this time is good data row 4 , 6, 7 and 6. first, the good ratio (Why 6 and 7 than it first, because at this time if the 7 and 6 large, then there is no need in front of 4 and 6 than the comparison, because 4 and 6 are already sorted a), and then found upon comparison is larger than 6 7, so in this case do not need to change, so the result at this time is 4,6,7,5

Then the fourth data is 5, this time sequence of the data has become 4,6,7,5, and 5, and will have the good data rows 4, 6 are compared, first 5 and 7 ratio (Why 5 and 7 than it first, because at this time if greater than 5 and 7, then you do not need and in front of 6 and 4 compare, because 4,6,7 are already sort of good), then 5 after the comparison and 7, 5 to 7 as small, so in this case the sequence is 4,6,5,7, 5 and 6 is then compared as 5 to 6 hours, so in this case the sequence is 4,5,6,7, 4 and 5 is then comparison, because of the large 5 to 4, the order of 4,5,6,7

 

2.JAVA achieve the following:

class insertSort{
    public static void main(String[]args){
        int [] arr={6,4,7,5};
        int e;
        int j;
        for(int i=1;i<arr.length;i++){
                e=arr[i];
            for(j=i;j>0&&arr[j-1]>e;j--){       //指针j从第二位数开始即j>0
                arr[j]=arr[j-1];
            }
            arr[j]=e;
        }
        show(arr);
    }
    public static void swap(int []arr,int i,int j ){
        int temp=arr[i];
        arr[i]=arr[j];
        arr[j]=temp;
    }
    public static void show(int[]arr){
        String s="[";
        for(int i=0;i<arr.length;i++){
            if(i==arr.length-1){
                s+=arr[i]+"]";

            }else{
                s+=arr[i]+",";
            }
        }
        System.out.println(s);
    }
}

Output:

[4,5,6,7]

 

 IV. Counting Sort

1. Basic principles

2.JAVA achieve the following:

class countSort{
    public static void main(String[]args){
        int[] arr={8,5,9,2,7,4,6,1,3,10,-3,-2,-10};
        int min=arr[0];
        int max=arr[0];
        for(int i=0;i<arr.length;i++){  //O(n)
            if(arr[i]>max){
                max=arr[i];
            }
            if(arr[i]<min){
                min=arr[i];
            }
        }
        int[] nums=new int[max-min+1];
        int offset=min;
        for(int i=0;i<arr.length;i++){  //O(n)
            nums[arr[i]-offset]++;
        }
        int index=0;
        for(int i=0;i<nums.length;i++){  //O(m)
            if(nums[i]!=0){
                for(int j=0;j<nums[i];j++){
                    arr[index++]=i+offset;
                }
            }
        }
        show(arr);
    }
    public static void swap(int []arr,int i,int j ){
        int temp=arr[i];
        arr[i]=arr[j];
        arr[j]=temp;
    }
    public static void show(int[]arr){
        String s="[";
        for(int i=0;i<arr.length;i++){
            if(i==arr.length-1){
                s+=arr[i]+"]";

            }else{
                s+=arr[i]+",";
            }
        }
        System.out.println(s);
    }
}

Output:

[-10,-3,-2,1,2,3,4,5,6,7,8,9,10]

 

to sum up:

  •         Alternatively, bubble, insertion sort are compared three ordered according to the magnitude relation between the data
  •         The counting base tub are compared with the characteristics of the data itself regardless of the magnitude of the sort, the sort only for integer
  •         It is a typical sort of sacrifice space for time

 

Published 31 original articles · won praise 1 · views 1277

Guess you like

Origin blog.csdn.net/qq_45824565/article/details/104364916