Sort data structures and algorithms 1: Merge and sort data structures

Source: Internet
Author: User

Sort data structures and algorithms 1: Merge and sort data structures

We have to admit the fact that if we have mastered various programming methods and tools during java learning, we can do some development, this is why some training institutions dare to tell you that you can master a language in just a few months. However, with the development of time, we will always feel that if we do not upgrade ourselves, this kind of person will only be a codoon. The technology will change with each passing day and will be updated at any time. But in the past few decades, some people have said that algorithms will be outdated. If we say that the engine of the java language is a variety of development methods and technologies, then we can say that algorithms will be his soul. The improvement and elevation of a programmer must be smooth and smooth, so I hope this foundation must be data structures and algorithms. After mastering these principles, we will actually be very relaxed in the future learning process.

First, we need to master several basic sorting methods, such as simple sorting, insert sorting, fast sorting, heap sorting, Bubble sorting, selection sorting, Hill sorting, and merge sorting. Each sorting method has its own characteristics. You can select an appropriate sorting method for the size of data or the requirements on time complexity and space complexity. In my personal opinion, because I have a special liking for merging and sorting during my learning process, we will introduce merging and sorting here today. We will demonstrate the java implementation code of merging and sorting, as well as the features of merging, and the computing of time complexity and space.

I. Features of Merge Sorting:

The first characteristic of Merge Sorting is that it is a representative sorting method with a divide-and-conquer idea, which permeate the recursive idea. When we face a lot of complicated and complex data, it may be our first choice to divide and conquer, which determines that the sorting should be used when the data volume is large, the space complexity is not high, and the time complexity is required. Because it will occupy the stack space in the Process of recursive data splitting. It may be a bit confusing here, so we can directly look at the code to see how to achieve the sort of merging.

Ii. Code implementation:

Step 1: we call it "merge", which must involve merging. If there is a merge, there must be a corresponding split. In this step, we correspond to the sort method, so that a complete array is continuously split until the final data unit is a separate data location. At this time, we proceed to the second step, that is, continuous integration of separate data from an ascending perspective.

Step 2: Let's see the mergeArray () method. It stores the sorted arrays after comparison through a temporary array, the most temporary container, then retrieve and assign the value to the original data. Here we need to put forward a few things that need to be understood,

The first point is that we merge the data sets on both sides of the middle data during the merge. Through comparison, let the small data enter the temporary array, and add a badge for comparison, until the end of a data set.

Second: After the comparison, there will inevitably be a data set that will leave some data not inserted. At this time, we will compare their starting and ending labels, if not, add the data in the dataset to our temporary array through the while loop.

The third point: we add the data in the temporary array to the original array at the end. We must remember to add the start value, because we all know that during the merge, we perform both operations at the same time, the data on the left may start from 0, but the data on the right side may not exist. Therefore, we need to add the start position when merging.

Point 4: you must pay attention to one thing. It is difficult to understand algorithms, but we may need to practice it. When the logic processing of Operations is not understood, we can perform process decomposition on the draft paper, or use the source code to debag the data.

1 public static void sort (int arr [], int emp [], int start, int end) {2 // judge whether to continue splitting recursion 3 int middle = start + (end-start)/2; 4 if (start <end) {5 sort (arr, emp, start, middle); // split the data on the left and implement ordered 6 sort (arr, emp, middle + 1, ens ); // split the data on the right and implement an ordered 7 mergeArray (arr, emp, start, middle, end ); // merge 8} 9} 10 public static void mergeArray (int arr [], int emp [], int start, int middle, int end) {11 int I = start, j = middle; 12 int k = middle + 1, z = end; 13 int x = 0; 14 // determine the data size inserted into the temporary array 15 while (I <= j & k <= z) {16 if (arr [I] <arr [k]) {17 emp [x ++] = arr [I ++]; 18} else {19 emp [x ++] = arr [k ++]; 20} 21} 22 // when data on both sides of the split is inserted into a temporary array, there will certainly be unfinished data in the original array 23 // continue to clean up the data, add a temporary array 24 while (I <= j) {25 emp [x ++] = arr [I ++]; 26} 27 while (k <= z) {28 emp [x ++] = arr [k ++]; 29} 30 // after all the data enters the temporary array, we export the data and place it in the original array, these data must be ordered 31 for (I = 0; I <x; I ++) {32 arr [start + I] = emp [I]; 33} 34 35}

3. algorithm Process Analysis diagram:

          

4. Time Complexity Calculation and Analysis of merging: For this part, we may try to understand it. It may be difficult for beginners to consider all aspects.

It can be said that Merge Sorting is a complex sort, especially for those who do not understand the basic idea of the division and Control Law, it may be difficult to understand. Total time = Decomposition Time + solution time + merge Time.

Decomposition Time: divides a sequence to be sorted into two sequences. The time is a constant and the time complexity is o (1 ).

Problem solving time: Two recursive models divide a problem with a scale of n into two subproblems with a scale of n/2, with a time of 2 TB (n/2 ). the merging time complexity is o (n ).

Total time: T (n) = 2 T (n/2) + o (n ). this recursive formula can be solved using a recursive tree, whose solution is o (nlogn ). in addition, the Merge Sorting time complexity is o (nlogn) in the worst, best, and average cases ). from the merge process, we can see that the Merge Sorting is stable.

Use the recursive tree method to solve the recursive T (n) = 2 T (n/2) + o (n): assume that the constant c is used to solve the final subproblem, for n to-be-sorted records, the overall problem scale is cn.

 

           

From this recursive tree, we can see that the first layer of time price is cn, and the second layer of time price is cn/2 + cn/2 = cn ..... the cost for each layer is cn, with a total of logn + 1 layers. Therefore, the total time cost is cn * (logn + 1). the time complexity is o (nlogn)

 

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.