Java Algorithm learning --------------- Merge Sorting Algorithm, java Merge Sorting Algorithm
Pairs the Adjacent Elements in the array and sorts them by means of the merge algorithm to form a sub-array segment with a sorted order of 2 in n/2 groups, then arrange the sub-array segments with a length of 4 until the entire array is sorted.
According to this idea, the recursive Merge Sorting Algorithm (Pseudo Code) is eliminated as follows:
Public class MergeSort {public static void mergeSort (Comparable [] a) {Comparable [] B = new Comparable [. length]; int s = 1; while (s <. length) {mergePass (a, B, s); // merged to the array bs + = s; mergePass (B, a, s ); // merge to the array as + = s;} private static void mergePass (Comparable [] x, Comparable [] y, int s) {// The adjacent array int I = 0 whose merging size is s; while (I <= x. length-2 * s) {// merge the adjacent word group merge (x, y, I, I + s-1, I + s * 2-1) at both ends of s ); I = I + 2 * s;} // The number of remaining elements is smaller than 2sif (I + s <x. length) {merge (x, y, I, I + s-1, x. length-1);} else // copy to y {for (int j = I; j <x. length; j ++) {y [j] = x [j] ;}} private static void merge (Comparable [] c, Comparable [] d, int l, int m, int r) {// merge c [l: m] and c [m + 1, r] to d [l: r] int I = 1, j = m + 1, k = l; while (I <= m & j <= r) {if (c [I]. compareTo (c [j]) <= 0) {d [k ++] = c [I ++];} else {d [k ++] = c [j ++] ;}} if (I> m) {for (int k2 = 0; k2 <= r; k2 ++) {d [k ++] = c [k2] ;}} else {for (int k2 = 0; k2 <= m; k2 ++) {d [k ++] = c [k2] ;}}}
In java, how is the difference between simple exchange sorting and Bubble sorting?
This is an algorithm-level problem and has nothing to do with java.
Both sorting and Bubble sorting are based on element exchange, so your classification is incorrect.
Basic Idea of Bubble Sorting: Sink the heaviest one to the bottom of the sea each time
Basic Idea of sorting: one of the heaviest scans exchanged with the first one
In addition, the time complexity of selection and bubbling is the same (both O (N ^ 2 ))
Therefore, it does not play a major role in real life. General languages (such as java, c ++, and c) have good sorting algorithms.
For example, qsort in c Language (using quick sorting) and sort in stl in c ++ (using mixed sorting in multiple ways)
The sort method in java (it is also a hybrid sorting. When there are few elements, it uses bubble, and when there are many elements, it uses Merge Sorting)
Of course, algorithms need to be learned. These algorithms must be understood by themselves. When used, they can be used as ready-made. If there are special requirements for sorting, of course you can implement the comparator yourself.
What classic algorithms have you learned in java? Answers from learned friends
Merge Sort Algorithm
Enumeration Method
Sequence Number sorting
Optimize high-precision subtraction with distinct
¤ Precision factorial
¤ Precision Subtraction
¤ High-precision Multiplication
¤ Dijkstra shortest path (the shortest path from one point to each vertex)
¤
Sort quick Sorting Algorithm
¤
Tracking through the maze
Sort common sorting algorithms
✓ Binary Search full version
Sort standard quick Sorting Algorithm
Quick sorting method
Sort quick Sorting Algorithm
Sort insert Sorting Algorithm
Sort select Sorting Algorithm
Sort Bubble Sorting Algorithm
✓ Statistical algorithm
Frequently Used NLP algorithms -- breadth-first search
Frequently Used NLP algorithms-deep Priority Search