Get the App
SLTechnology News&Howtos  ›  Development  › 

How to merge and sort MergeSort to achieve top-down and bottom-up

Shulou Source: shulou.com Published: 2022-06-03 11:35:19 10月02日 Update

How to merge and sort MergeSort to achieve top-down and bottom-up, for this problem, this article introduces the corresponding analysis and solutions in detail, hoping to help more partners who want to solve this problem to find a more simple and feasible method.

Merge sorting MergeSort is one of the earliest algorithms implemented on computers. It was proposed by von Neumann John von Neumann when he published the 101st report in 1945, and then it was applied on the EDVAC computer completed in 1951.

Merge sorting is to sort the array into sub-arrays on the basis of merging, so as to make the whole array completely order. the algorithm uses the typical divide-and-conquer method to solve the problem, that is, the problem is decomposed into sub-problems first. then the solutions of the sub-problems are merged to get the solution of the whole problem.

The core idea of merge sorting is merging. In order to better understand merge sorting, let's first understand the concept of merge in place.

Merging process

Create a new copy of the original array as a secondary array

Use two pointers to traverse two parts of the auxiliary data simultaneously

Place the smaller elements pointed to by the two pointers into the original array

Then move the pointer of the smaller element to the next bit

When one side of the data traversal is complete, the rest of the data is copied to the original array.

The code implements private static void merge (Comparable [] a, Comparable [] aux, int lo, int mid, int hi) {assert isSorted (a, lo, mid); assert isSorted (a, mid + 1, hi); for (int k = lo; k hi) a [k] = aux [iTunes +]; / / depleted on the right, take the left half of the data else if (less (aux [I], auxj)) a [k] = aux [jacks +] / take a smaller value else a [k] = aux [iTunes +];} assert isSorted (a, lo, hi);} public static boolean less (Comparable v, Comparable w) {return v.compareTo (w) < 0;} public static boolean isSorted (Comparable [] a, int lo, int hi) {for (int I = lo + 1; I)

Tags: Sort array data problem order method code element algorithm complex two complexity performance process part help top-down original continuous pointer Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno Redmi MariaDB macOS Shulou Technology NVidia