Sort arrays according to a certain benchmark

Source: Internet
Author: User

Question: given an integer array, the rightmost element is used as the benchmark to sort out the array. The elements greater than the benchmark are placed on the right of the array, the element smaller than this benchmark is placed on the left of it. Do not use temporary arrays.

Idea: First find an element that is greater than the benchmark and exchange the benchmark. At this time, the benchmark element is already in the middle of the array (not the rightmost/middle), Scanning backward from the benchmark, and exchanging if it is less than the benchmark.

Public class arrayparts {public static void main (string [] ARGs) {int [] C = partition (New int [] {2, 3, 5, 1, 6, 4}); For (INT I = 0; I <C. length; I ++) system. out. print (C [I] + ",");} public static int [] substring (INT [] A) {int Len =. length; If (LEN <= 1) return a; int P = A [Len-1]; int I = 0, temp = 0 ;; // for (I = 0; I <Len; I ++) {if (a [I]> P) {temp = A [I]; A [I] = A [Len-1]; A [Len-1] = temp; break;} For (; I <len-1; I ++) {if (a [I + 1] <A [I] & A [I + 1] <p) {temp = A [I + 1]; A [I + 1] = A [I]; A [I] = temp ;}} return ;}}


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.