Dictionary Ordering-Perfection arrangement (elements have duplicates)

Source: Internet
Author: User
Tags array length

Idea: The elements of array A are 1,2,3,3; dictionary ordering is to find the next larger than 1,2,3,3 array sequence, namely 1,3,2,3;

The steps are as follows: 1. First use Arrays.sort () to sort the sorted array, such as input 3213, order 1233, from the last element of the array (i.e. I = array length), compare a[i] and a[i-1], and find the first a[i-1]<a[i] The i-1;

2, at this time, A[i] may also exist in front of the element larger than A[i-1], starting from the end of the array to find the first more than a[i-1] large element x, will x and A[i-1] Exchange, to Arrays.sort (A,i,a.length);

1  Public classSolution {2      Public Static voidMain (String args[]) {3Scanner scan =NewScanner (system.in);4         intn =scan.nextint ();5         int[] A =New int[n];6         intI,j = 0;7         inttemp = 0;8          for(i = 0; i<n; i++)9A[i] =scan.nextint ();Ten Arrays.sort (a); One          for(i = 0; i<a.length;i++){ ASystem.out.print (A[i] + ""); -         } - System.out.println (); the          while(true){ -              for(i = a.length-2; i>=0; i--){ -                 if(A[i+1] >A[i]) { - //i = i-1; +                      Break; -                 } +             } A             if(I < 0) at                  Break; -              -              for(j = a.length-1; j> i; j--){ -                 if(A[j] >A[i]) { -                     intK =A[i]; -A[i] =A[j]; inA[J] =K; -                      Break; to                 } +             } -Arrays.sort (a,i+1, a.length); the              for(i = 0; i<a.length;i++){ *System.out.print (A[i] + ""); $             }Panax Notoginseng System.out.println (); -         } the          +     } A}

Dictionary Ordering-Perfection arrangement (elements have duplicates)

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.