How is the algorithm so difficult-Merge Sorting and quick sorting

Source: Internet
Author: User

How is the algorithm so difficult-Merge Sorting and quick sorting
I have recently learned the basic data structure knowledge for a few days. The more I learn, the more I want to worry about my IQ. Let's look at some blogs about algorithms from many experts, I really think the gap is so big. Well, there is little nonsense. Next I will briefly introduce two basic sorting methods: Merge Sorting and fast sorting: the principle of Merge Sorting and Merge Sorting: Merge Sorting divides a set into two parts: part1 and part2 sort part1 and part2 respectively (recursive method is used until the size of the child set is 1, the collection is stopped. At this time, because there is only one element in the subset, this sub-set is equivalent to the order already taken.) Finally, the sub-sets sorted by the two parts are merged into one. When writing code, pay attention to the following points: how to divide the set into two parts, each of which is in size? The first part of the set is list. length/2; the second part of the set is list. length-list.length/2; when is the recursive call of the Set stopped? When the sub-set size is 1, division of the set is stopped. Therefore, a judgment If (list. length> 1) {}; code implementation: public static void mergeSort (int [] list) {if (list. length> 1) {int [] half1 = new int [list. length/2]; int [] half2 = new int [list. length-list. length/2]; System. arraycopy (list, 0, half1, 0, half1.length); // copy the First Half of the set to mergeSort (half1) in half1; System. arraycopy (list, list. length/2, half2, 0, half2.length); // copy the second half of the set to mergeSort in half2 (Half2); int [] temp = merge (half1, half2); // merge the two sets into System. arraycopy (temp, 0, list, 0, temp. length) ;}} the merge method of the Code in the previous section combines the two sets into one and returns the result. Code implementation: private static int [] merge (int [] list1, int [] list2) {int [] temp = new int [list1.length + list2.length]; int currentPosition1 = 0; // The subscript variable int currentPosition2 = 0 when list1 is traversed; // The subscript variable int currentPositionTemp = 0 when list2 is traversed; // The subscript variable used to merge two lists // copy the elements in the two sets to the temporary variable while (currentPosition1 <list1.length & currentPosition2 <list2.length) {if (list1 [currentPosition1] <list2 [currentPosition2]) {/ /Put small elements in front. Temp [currentPositionTemp ++] = list1 [currentPosition1 ++];} else {temp [currentPositionTemp ++] = list2 [currentPosition2 ++] ;}// when running here, at least one of the list1 and list2 lists has been copied to the temporary set, but it is possible that the number of elements in list1 is smaller than the number of elements in list2. Therefore, you need to copy the remaining data in list1 or list2 to the temporary set. While (currentPosition1 <list1.length) {temp [currentPositionTemp ++] = list1 [currentPosition1 ++];} while (currentPosition2 <list2.length) {temp [currentPositionTemp ++] = list2 [currentPosition2 ++];} return temp;} test code: public static void main (String [] args) {// TODO Auto-generated method stub int [] list = {, 90,1, 0}; mergeSort (list); for (int temp: list) {System. out. print (temp + "");} Sy Stem. out. println ();} running result: the principle of fast sorting: Fast sorting: in the array, select an element called principal component to divide the array into two parts, so that all elements in the first part are less than or equal to the principal component, and all elements in the second part are greater than the principal component ). Then apply the quick sorting algorithm to the first part of recursion, and apply the quick sorting algorithm to the second part of recursion. Sort is the center point. Therefore, the Quick Sort can be understood as the center point. Then, the array is divided into two parts based on the center point. One part is smaller than or equal to the center point, and the other part is greater than the center point, then execute the two parts according to this method, and sort the array in order. For the sake of simplicity, the first element of the array will be taken as the center point in my code implementation ). The Code is as follows: Note: The function of this code segment is to divide the set into two parts based on the first element of the set as the center point, returns the subscript private static int partition (int [] list, int first, int last) of the cursor in the center. {// specify the first and last parameters, divide the segment of the set into two parts according to the sequence. Int cursor = list [first]; // take the first element as the center point of the Set (cursor) int low = first + 1; int high = last; while (high> low) {// locate the first element greater than the forward value and the first element smaller than the forward value, swapping the two of them while (low <= high & list [low] <= lower) low ++; while (low <= high & list [high]> lower) {high --;} if (high> low) {int temp = list [high]; list [high] = list [low]; list [low] = temp ;}} // after the previous loop, all the elements whose subscript is smaller than low are smaller than low, and the elements whose subscript is greater than high are greater than low. // traverse the high subscript from the back to the front, until the value of the current underlying object is found to be less than or equal to latency. while (high> first & list [high] >=)) {high --;} if (Priority> list [high]) {// if is true, it indicates that the value of the marker is not the smallest value in the set. In this case, place the value of marker to the position of list [high]. list [first] = list [high]; list [high] = cursor; return high;} else {// In this case, the cursor is the smallest element in the set. Therefore, the cursor value is the first return first ;}} the quicksort () code is as follows: private static void quickSort (int [] list, int first, int last) {if (last> first) {int partition tindex = partition (list, first, last); quickSort (list, first, shorttindex-1); quickSort (list, shorttindex + 1, last) ;}} define a public method with only one set as the parameter: public static void quickSort (int [] list) {quickSort (list, 0, list. length-1);} test code: public static void main (String [] args) {// TODO Auto-generated method stub int [] list =, 5, 2}; quickSort (list); for (int temp: list) {System. out. print (temp + "");} System. out. println ();}

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.