Merge Sorting (implemented in Java)

Source: Internet
Author: User

Import java. util. arrays;

Public class mergesort {

Public static void mergesort (datawraper [] data ){
System. Out. println ("start sorting :");
Sort (data, 0, Data. Length-1 );
}
/* Merge and sort the array elements in the range from left to right.
* Data to be sorted Array
* Left: Index of the first element of the array to be sorted
* Right: Index of the last element of the array to be sorted
*/
Private Static void sort (datawraper [] data, int left, int right ){
// Todo auto-generated method stub
If (left <right ){
// Finds the intermediate Index
Int center = (left + right)/2;
// Recursion of the array on the left
Sort (data, left, center );
// Recursive the array on the right
Sort (data, center + 1, right );
// Merge
Merge (data, left, center, right );

}
}
/* Merge the two arrays. The first two arrays are sorted, and they are still sorted after merging.
* Data array object
* Left: Index of the first element in the left Array
* The index of the last element in the left array of center. Center + 1 is the index of the first element in the right array.
* Right: Index of the last element of the Right Array
*/
Private Static void Merge (datawraper [] data, int left, int center, int right ){
// Todo auto-generated method stub
Datawraper [] tmparr = new datawraper [data. Length];
Int mid = center + 1;
// Index of the middle array of the third record
Int third = left;
Int TMP = left;
While (left <= center & Mid <= right ){
// Extract the smallest of the two arrays and put them into the middle array.
If (data [left]. compareto (data [Mid]) <= 0 ){
Tmparr [third ++] = data [left ++];
} Else {
Tmparr [third ++] = data [Mid ++];
}
}
// Add the remaining parts to the intermediate array in sequence
While (mid <= right ){
Tmparr [third ++] = data [Mid ++];
}
While (left <= center ){
Tmparr [third ++] = data [left ++];
}
// Copy the content in the intermediate array back to the original array
While (TMP <= right ){
Data [TMP] = tmparr [TMP ++];
}
System. Out. println (arrays. tostring (data ));
}
Public static void main (string [] ARGs ){
// Todo auto-generated method stub
Datawraper [] DATA = {
New datawraper (21 ,""),
New datawraper (30 ,""),
New datawraper (49 ,""),
New datawraper (30 ,"*"),
New datawraper (16 ,""),
New datawraper (9 ,""),
New datawraper (-16 ,""),
New datawraper (10 ,""),
New datawraper (25 ,""),
New datawraper (18 ,""),
};
System. Out. println ("Before sorting: \ n" + arrays. tostring (data ));
Mergesort (data );
System. Out. println ("sorted: \ n" + arrays. tostring (data ));
}

}

Result:

Before sorting:
[21, 30, 49, 30 *, 16, 9,-16, 10, 25, 18]
Start sorting:
[21, 30, 49, 30 *, 16, 9,-16, 10, 25, 18]
[21, 30, 49, 30 *, 16, 9,-16, 10, 25, 18]
[21, 30, 49, 16, 30 *, 9,-16, 10, 25, 18]
[16, 21, 30, 30 *, 49, 9,-16, 10, 25, 18]
[16, 21, 30, 30 *, 49,-16, 9, 10, 25, 18]
[16, 21, 30, 30 *, 49,-16, 9, 10, 25, 18]
[16, 21, 30, 30 *, 49,-16, 9, 10, 18, 25]
[16, 21, 30, 30 *, 49,-16, 9, 10, 18, 25]
[-16, 9, 10, 16, 18, 21, 25, 30, 30 *, 49]
After sorting:
[-16, 9, 10, 16, 18, 21, 25, 30, 30 *, 49]

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.