Ex 2_14 Remove all duplicate elements from the array ... _ Second job

Source: Internet
Author: User

First, we use the merge sort algorithm to sort the array, the time complexity is O (Nlogn), and then use the time complexity O (n) de-duplication algorithm to remove the repeating elements in the arrays. The total time complexity is O (NLOGN).

(This problem should be solved by branching algorithm) The following is the branching algorithm

Code is not a branching algorithm

1  PackageOrg.xiu68.ch02.ex2;2 3  Public classex2_14 {4     //merging sorting algorithm based on divide-and-conquer method5      Public Static voidMain (string[] args) {6         7         int[] A=New int[]{5,5,4,4,3,3,3,2,2,1,1};8         9         //Merge sorted array First, time complexity O (nlog2n)TenMergeSort (A, a.length-1); One         intMin=a[0]-1; A          -         //removes duplicate elements from an ordered array with a time complexity of O (n) - Removesame (a); the          -         //The total time complexity is O (nlog2n) -          for(inti=0;i<a.length;i++){ -             if(a[i]!=min) +System.out.print (a[i]+ ""); -         } +      A     } at      -      -     //one merge, two and one -      Public Static voidMergeint[] Start,int[] result,intSintMintt) { -         //S,m+1 is the first record of two ordered sequences, and T is the last record of the second sequence -         intI=s; in         intJ=m+1; -         intk=s; to          +          while(I<=m && j<=t) -             if(Start[i]<=start[j])//take Start[i] and start[j] The smallest person put result[k] theresult[k++]=start[i++]; *             Else $result[k++]=start[j++];Panax Notoginseng          -         if(i<=m)//The first sequence does not traverse the end of the              while(i<=m) +result[k++]=start[i++]; A          the         Else            //The second sequence does not traverse the end of +              while(j<=t) -result[k++]=start[j++]; $     } $      -     //a trip to sort, h for sequence length -      Public Static voidMergepass (int[] Start,int[] result,intNinth) { the         intI=0; -          while(i<=n-2*h+1){WuyiMerge (start,result,i,i+h-1,i+2*h-1); thei+=2*h; -         } Wu         if(i<n-h+1) -Merge (start,result,i,i+h-1, n); About         Else $              for(intk=i;k<=n;k++) -result[k]=Start[k]; -     } -      A     //Merge Sort +      Public Static voidMergeSort (int[] Start,intN) { the         intH=1; -         int[] result=New int[N+1]; $          while(h<N) { the Mergepass (start, result, N, h); theh=2*h; the Mergepass (result, start, N, h); theh=2*h; -         } in     } the      the     //remove the repeating part of the array About      Public Static voidRemovesame (int[] a) { the         intMin=a[0]-1;//sets the repeating part of the array to min the          for(inti=0;i<a.length;) { the             intJ=i+1; +              while(J<a.length && a[i]==A[j]) { -a[j]=min; theJ + +;Bayi             } theI=J; the}// for -     } -      the      the}
View Code

Ex 2_14 Remove all duplicate elements from the array ... _ Second job

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.