Fast ordering of Java implementations of common algorithms

Source: Internet
Author: User

Quick sort and bubble sort are similar, and are sorted by multiple comparisons and exchanges. The specific process is as follows: 1, first set a cutoff value, by dividing the array into the left and right parts, the data exchange is greater than or equal to the boundary value of the array, the data exchange less than the cutoff value to the left array; 2, then, the left and right arrays can be sorted independently. For the left array can take a cutoff value, the left array is divided into two parts, the same will be placed on the left less than the cutoff value of the data, the right place is greater than or equal to the cutoff value of the data.  Do similar processing on the right array. 3, repeating the above process, in fact, is recursion. The left array is sorted by recursion and the right data is processed recursively.      When the left and right parts of the data are sorted, the sorting of the entire array is completed.  If there is initial data: 25 11 45 26 12 78. 1, first set a cutoff value, here Select the first element---25.  Save the minimum ordinal 0 of the array in the left of the variable, save the maximum ordinal number 6 of the array in the variable right, and save the cutoff value 25 in the variable base. 2. Start with the right side of the array and compare it to the cutoff value 25, until you find data that is less than base.  Here, 12 is smaller than the cutoff value of 25.  3. Save the data of the right score bounds to A[left] (A[0]) element. 4, starting from the left of the array, one by one with the cutoff value of 25, until you find data greater than or equal to base.  Here, the leftmost data of the array is 12, the score bounds value is small, the left self-increment 1, then take A[left] (a[1]) value is 45, because 45 is greater than 25, the end of the search.  5. Save the large number of values on the left side of the score bounds to the a[right] element. 6. Save the cutoff value to A[left].  After a round of comparisons and exchanges, the number on the left side of the base data is smaller than the base value, and the right side is a number larger than the base value.   7, next, through the recursive call, the left side of the data to do the same sort, and then leave the data on the right side of the same sort. The quick sort has improved the bubbling sort and therefore has better execution efficiency.   The average time complexity is O (NLOGN). The Java code is implemented as follows: (Https://github.com/xbk417/algorithm)
1      Public voidSortint[] arr) {2QuickSort (arr, 0, Arr.length-1);3     }4     5     Private Static voidQuickSort (int[] arr,intLeftintRight ) {6         intT;7         intLtemp =Left ;8         intRtemp =Right ;9         //Cutoff valueTen         intFIndex = arr[(left + right)/2]; One          while(Ltemp <rtemp) { A             //find a large number of score bounds from the left -              while(Arr[ltemp] <FIndex) { -                 //element is less than the cutoff value, continue to find the++ltemp; -             } -             //find a small number of score bounds from the right -              while(Arr[rtemp] >FIndex) { +                 //element is greater than the cutoff value, continue to find ---rtemp;  +             } A             //If the index of a large number of scores is found to be less than or equal to the number of small points in the score bounds, then Exchange at             if(Ltemp <=rtemp) { -t =Arr[ltemp]; -Arr[ltemp] =Arr[rtemp]; -Arr[rtemp] =T; ---rtemp; -++ltemp; in             } -             if(Ltemp = =rtemp) { toltemp++; +             } - System.out.println (arrays.tostring (arr)); the             if(Left <rtemp) { *QuickSort (arr, left, ltemp-1); $             }Panax Notoginseng             if(Ltemp <Right ) { -QuickSort (arr, rtemp + 1, right); the             } +         } A}

Fast ordering of Java implementations of common algorithms

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.