Before carrying out many convenient algorithms, it is always necessary to realize the ordering of objects, which will use the sorting related algorithms. Even though many high-level languages have completed the encapsulation of the sorting algorithm, users only need to import the corresponding library file to call the sorting algorithm to complete the sorting without handwritten sorting algorithm, However, the selection of specific sorting algorithm must be aware of the sorting algorithm. This paper will introduce two simple sorting algorithms: selective sorting and bubble sorting.
Select sort
Why is it called selective sorting?
Each time, the algorithm compares the unordered keywords, selects the smallest or largest keywords, and then exchanges their positions to realize one-time sorting, which requires multiple comparisons.
Selective sorting is an unstable sorting algorithm. Its working principle is to select the smallest (or largest) element from the data elements to be sorted every time, store it at the beginning of the sequence, and then continue to find the smallest (large) element from the remaining unordered elements, and then put it at the end of the sorted sequence. And so on until all the data elements to be sorted are arranged.
Selective sorting is a simple and intuitive sorting algorithm. No matter what data goes in, it is O(n) ²) Time complexity. So when using it, the smaller the data scale, the better. In terms of complexity analysis, its biggest feature is that it exchanges mobile data less times and does not need to occupy a large memory space.
Algorithm code implementation
Suppose there is an array of length N, which needs to be selected and sorted. Each subscript variable is set to i, that is, 1 < = i < = n. The basic idea of the algorithm implementation is to compare the array with n-1 keywords, that is, for the comparison of the corresponding value of the array subscript, record the subscript and save it in min or max, and finally exchange its keyword with the ith keyword to complete a selection.
There are basically two layers of for loop nesting for sorting. The outermost loop is to ensure n-1 sorting. The second layer of for loop is to find the subscript of the most value in each sorting process and avoid dead loop. The inner loop will start from i+1 for comparison. Their judgment conditions for cycle are different as follows:
- Outer loop: start from zero and less than the array length minus one, and the iteration statement is the iteration flag plus one
- Inner loop: starting from i+1, it is less than the length of the array, and the iteration statement adds one to the iteration flag
The selection statement in the inner loop is the comparison between the array element corresponding to the current iteration flag and the corresponding array element recording the maximum subscript of the current remaining part. Select the size relationship according to the sorting requirements, enter the selection statement, and assign the maximum subscript to the record variable.
The sorting order mainly depends on the value of your subscript record. In each comparison, the larger value of subscript record is sorted in descending order, and the smaller value of subscript record is sorted in ascending order. The implementation of ascending sorting code will be shown below.
public class SelectionSort implements IArraySort { @Override public int[] sort(int[] sourceArray) throws Exception { int[] arr = Arrays.copyOf(sourceArray, sourceArray.length); // A total of N-1 rounds of comparison are required for (int i = 0; i < arr.length - 1; i++) { int min = i; // Number of comparisons per round N-i for (int j = i + 1; j < arr.length; j++) { if (arr[j] < arr[min]) { // Record the subscript of the lowest value element that can be found at present min = j; } } // Exchange the minimum value found with the value of i position if (i != min) { int tmp = arr[i]; arr[i] = arr[min]; arr[min] = tmp; } } return arr; } }
Bubble sorting, which belongs to the same basic sorting algorithm as selection sorting, is also an algorithm for sorting objects based on comparison and exchange. The idea is very simple. Let's start the introduction!
Bubble sorting
Bubble sorting is also a simple and intuitive sorting algorithm. It repeatedly visits the sequence to be sorted, compares two elements at a time, and exchanges them if they are in the wrong order. The work of visiting the sequence is repeated until there is no need to exchange, that is, the sequence has been sorted. The name of this algorithm comes from the fact that smaller elements will slowly "float" to the top of the sequence through exchange.
Assuming that the array to be sorted is [2,1,3,4,5,6,7,8,9], if you try to use the bubble sorting algorithm under the above idea, you will find that in addition to the significance of exchanging data for the first time, for the later ordered array, you should end the cycle and complete the sorting, but it doesn't, and it still executes n-2 cycles without hesitation. This will have some room for optimization.
The idea of the optimization algorithm is to set up a flag as a sign. When the elements are not exchanged in a sequence traversal, it proves that the sequence has been orderly and does not need to repeat multiple cycles. In the case of huge data scale, this optimization plays a powerful role.
Algorithm code implementation
Consistent with the selection sorting, it has two layers of for loops, the outer loop determines the sorting times, and the inner loop determines the sorting method. Bubble sorting often moves elements from left to right. Therefore, the inner loop iteration flag needs to be less than the array length minus the number of sorted times. For each comparison of the inner loop, as long as the sequence is reversed, the position is directly exchanged.
Simple algorithm
public class BubbleSort implements IArraySort { @Override public int[] sort(int[] sourceArray) throws Exception { // Copy the arr without changing the parameters int[] arr = Arrays.copyOf(sourceArray, sourceArray.length); for (int i = 1; i < arr.length; i++) { for (int j = 0; j < arr.length - i; j++) { if (arr[j] > arr[j + 1]) { int tmp = arr[j]; arr[j] = arr[j + 1]; arr[j + 1] = tmp; } } } return arr; } }
Optimization algorithm:
public class BubbleSort implements IArraySort { @Override public int[] sort(int[] sourceArray) throws Exception { // Copy the arr without changing the parameters int[] arr = Arrays.copyOf(sourceArray, sourceArray.length); for (int i = 1; i < arr.length; i++) { // Set a flag. If it is true, it means that there is no exchange in this cycle, that is, the sequence to be arranged has been ordered and the sorting has been completed. boolean flag = true; for (int j = 0; j < arr.length - i; j++) { if (arr[j] > arr[j + 1]) { int tmp = arr[j]; arr[j] = arr[j + 1]; arr[j + 1] = tmp; flag = false; } } if (flag) { break; } } return arr; } }
Algorithm comparison
The comparison of algorithms starts from the analysis of algorithm complexity (i.e. time complexity and space complexity) and algorithm stability. Let's briefly talk about these concepts:
- Algorithm complexity: algorithm complexity can be divided into time complexity and space complexity. It is the basic standard for judging a high-quality algorithm as an algorithm. Often, a high-quality algorithm can achieve a good balance between time complexity and space complexity. However, in some cases, an algorithm with extreme preference for one complexity is not a high-quality algorithm.
- Time complexity: the time consumed by the algorithm is often expressed in capital O(), which can be divided into constant order, i.e. o (constant), function order, such as linear order O(f(n)), square order O(f(n^2)), etc
- Space complexity: the memory space consumed by the operation of algorithm code.
- Algorithm stability: for the judgment of the part that does not need to be changed before and after running the algorithm code, if there is a certain change, even if the basic properties remain unchanged, it is unstable.
For example, 5, 8, 5, 2, 9, we know that selecting the first element 5 for the first time will exchange with 2, so the relative position of the two 5 in the original sequence will be destroyed, which is unstable.
Advantages and disadvantages of bubble sorting:
- Advantages: relatively simple, low spatial complexity and stable;
- Disadvantages: time complexity is too high and efficiency is slow;
Advantages and disadvantages of selecting sorting:
- Advantages: you only need to change the position once in a round of comparison;
- Disadvantages: slow efficiency and instability