In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-18 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/02 Report--
This article mainly explains "Java insertion sorting example Analysis". Interested friends may wish to have a look. The method introduced in this paper is simple, fast and practical. Now let the editor to take you to learn "Java insertion sorting example analysis"!
Insert sorting principle
① divides all elements into sorted and unsorted groups
② finds the first element of the unsorted group and inserts it into the sorted group
③ iterates through the elements that have been arranged in reverse order and compares them with the elements to be inserted in turn, until one element is found to be less than or equal to the element to be inserted, then the element to be inserted is placed in this position, and the other elements are moved backward one bit.
Insert sort API design class name Insertion constructor Insertion (): create Insertion object member method
1.public static void sort (Comparable [] a): sorts the elements in an array
2.private static boolean greater (Comparable v comparison w): judge whether v is greater than w
3.private static void exchange (Comparable [] a drawing int iline int j): swap the values at index I and index j in the an array
Insert the sorting code to sort public class Insertion {/ / sort the elements of array a public static void sort (Comparable [] a) {for (int item1bot i0vicjmuri -) {if (greater (a [j-1], a [j])) {exchange (adome jlim 1qj) } else {/ / found this element, end break;}} / / compare whether v element is greater than w element private static boolean greater (Comparable vcomparable w) {return v.compareTo (w) > 0 } / / Array elements I and j exchange positions private static void exchange (Comparable [] a graint iint iint j) {Comparable tactia [I]; a [I] = a [j]; a [j] = t;} / / test code class Test {public static void main (String [] args) {Integer [] a = {4, 3, 10, 12, 1, 12, 1, 5, 6}; Insertion.sort (a) Time complexity Analysis of System.out.println (Arrays.toString (a));}} insertion sort
Same as Bubble sorting Analysis!
Although a double-layer loop is used, the inner loop is the code that really completes the sorting, so you can mainly analyze the number of times the inner loop is executed!
The worst-case scenario is that the array elements are {12, 10, 6, 5, 4, 3, 2, 1}.
The number of comparisons of elements is (Nmur1) + (NMU2) + (NMU3) +. + 2cm 1 = ((NMU1) + 1) * (NMU1) / 2 = N ^ 2 / 2-N/2
The exchange times of elements are as follows: (Nmur1) + (NMuo2) + (NMuo3) +. + 2cm 1 = ((NMU1) + 1) * (NMI1) / 2 = N ^ 2 / 2-N/2
The total number of execution is: 2 * (N ^ 2 / 2-N/2) = N ^ 2-N
According to the big O derivation rule, the highest order term is retained, that is, the time complexity of insertion sorting is O (N ^ 2).
At this point, I believe that everyone on the "Java insertion sorting example analysis" have a deeper understanding, might as well to the actual operation of it! Here is the website, more related content can enter the relevant channels to inquire, follow us, continue to learn!
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.