Algorithm | Selection Sort

Algorithm description of selection sort:

  1. Select the first element, compare it with the remaining elements one by one, find the smallest element until the end of the array, and then swap with the first element.
  2. Select the second element, compare it with the remaining elements one by one, find the smallest element until the end of the data, and then exchange with the second element.
  3. And so on. . . up to the Nth element

The algorithm is implemented as:

 
 
public class SelectSort {
    public static void main(String[] args) {
        int[] nums = new int[]{1, 15, 3, 78, 34, 23, 46, 2, 8, 34, 57};
        System.out.println(Arrays.toString(nums));
        sort(nums);
        System.out.println(Arrays.toString(nums));
    }

    public static void sort(int[] arrays) {
        for(int i=0; i< arrays.length; i++ ) {
            int min = i;
            for(int j=i+1; j< arrays.length; j++) {
                if (arrays[min] > arrays[j]) min = j;
            }

            //exchange the min and current arrays[i]
            if (min != i) {
                int temp = arrays[i];
                arrays[i] = arrays[min];
                arrays[min] = temp;
            }
        }
    }

}

Algorithm complexity analysis:


I downloaded this image from here  It is purely for explanation. Let me explain:
the gray elements are sorted elements, the black elements are elements to be sorted, and the red elements are the smallest elements found in a complete search process. Elements

You can see that the smallest element found each time will be placed after the sorted elements, that is--in the nth loop, find the smallest element, and then exchange it with the nth element.

Analysis of Algorithms:

According to the flow of the algorithm to analyze:

Each cycle, selecting the smallest element needs to compare Ni + 1 times, the range of i is [0-n-1]

Therefore, the number of comparisons required for all loops is: N-1, N-2, N-3,.....1, and the sum of the arithmetic progression is (N-1 + 1)(N-1)/2 approximation : N 2 /2

All swaps are at most N-1 times

The total time complexity is N 2 /2 (N squared)

Selection sort is a two-layer loop, which is divided into outer loop and inner loop: the maximum number of exchanges in the outer loop is N-1 times, and the number of comparisons in the inner loop is N-1 times. It is easy to get the above result by adding up the operations.

The disadvantage of selection sort is that in the inner loop, it is compared one by one, regardless of whether the array is ordered or not, it will be compared from beginning to end. The disadvantage is that if the array is already in an ordered state, or partially ordered, Selection sort will not use this ordering condition to reduce the number of comparisons. The next article will introduce insertion sorting, which can greatly reduce the number of comparisons by using the ordered or partially ordered state of the data, thereby improving the sorting speed!

In addition, a small optimization point is very important!

It can be seen that when we find the smallest element in each inner loop, there is an optimization point: we select the i-th (0 to n-1) element and compare it with the following elements one by one, when we find that there are smaller elements , instead of directly exchanging a[i] with this smaller element, but using this smaller element to compare with the remaining elements, and recording the index of this smaller element, the end of the inner loop is that we haven't happened yet Swap in turn, just find the index of the smallest element. After finding the smallest index, perform the swap operation instead of performing the swap operation every time the comparison meets the conditions!

I will post this code so that you can understand it carefully,

public class SelectSort {
    public static void main(String[] args) {
        int[] nums = new int[]{1, 15, 3, 78, 34, 23, 46, 2, 8, 34, 57};
        System.out.println(Arrays.toString(nums));
        sort(nums);
        System.out.println(Arrays.toString(nums));
    }

    public static void sort(int[] arrays) {
        for(int i=0; i< arrays.length; i++ ) {

            for(int j=i+1; j< arrays.length; j++) {
                if (arrays[i] > arrays[j]) {
                    //exchange
                    int temp = arrays[i];
                    arrays[i] = arrays[j];
                    arrays[j] = temp;
                }
            }

            //exchange the min and current arrays[i]  
//            if (min != i) {
//                int temp = arrays[i];
//                arrays[i] = arrays[min];
//                arrays[min] = temp;
//            }
        }
    }

}  


Next: Bubble Sort

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325811813&siteId=291194637