How to implement merge sorting algorithm in java
This article is about how java implements the merge sorting algorithm. The editor thinks it is very practical, so share it with you as a reference and follow the editor to have a look.
0x01, preface chatter
I seldom watch TV in recent years, because I don't have time to watch it. Apart from occasionally brushing the headlines, I basically don't spend a lot of time indulging in TV series and variety shows. Maybe this is some of the changes brought about by the era of short videos. We will all be deeply affected by it.
0x02, let's take a look at what this article is about.
0x03, what is merge sorting?
[introduction to Baidu Encyclopedia] merge sorting (Merge Sort) is an effective and stable sorting algorithm based on merge operation, which is a very typical application of divide-and-conquer (Divide and Conquer). The ordered subsequences are merged to get a completely ordered sequence, that is, each subsequence is ordered first, and then the subsequence segments are ordered. If two ordered tables are merged into one ordered table, it is called two-way merging.
0x04, merge sort program implementation
Public class MergeSortTest {public static void main (String [] args) {int [] arr = {1, 3, 2, 4, 9, 8, 10, 7, 6, 5, 10, 12, 13, 15, 14} int left = 0; int right = arr.length-1; mergeSort (arr, left, right); for (int num: arr) {System.out.print (num + "\ t") }}
Private static int [] mergeSort (int [] arr, int left, int right) {if (left = = right) {return new int [] {arr [left]};} int mid = left + (right-left) / 2; int [] leftArr = mergeSort (arr, left, mid); int [] rightArr = mergeSort (arr, mid + 1, right); int [] newArray = new int [leftArr.length + rightArr.length] Int m = 0; int I = 0; int j = 0; while (I < leftArr.length & & j < rightArr.length) {newArray [m] = leftArr [I] < rightArr [j]? LeftArr [I]: rightArr [j]; iTunes; jacks; masks;} while (I < leftArr.length) {newArray [masks +] = leftArr [iTunes +];} while (j < rightArr.length) {newArray [masks +] = rightArr [jacks +];} return newArray;}
}
0x05, the picture version of the program for merging and sorting
Thank you for reading! This is the end of the article on "how to achieve the merger and sorting algorithm in java". I hope the above content can be of some help to you, so that you can learn more knowledge. if you think the article is good, you can share it out for more people to see!