Programming Pearl Chapter 1 11th finding the K-small number from the array

Source: Internet
Author: User

Finding the median indicates that the question is a special case of knowing the question. Finding the number smaller than K and using the Division idea of the Quick Sort, time complexity O (N + n/2 + N/4 ....) = O (N)

Void swap (int * a, int * B) {int temp = * A; * A = * B; * B = temp;} void qselect (int * List, int left, int right, int K) // index is K, not the rank {If (Left = right) return; int key = list [left]; int I = left, j = right + 1; while (I <j) {do {I ++;} while (I <= Right & list [I] <key ); do {J --;} while (list [J]> key); if (I> J) break; swap (& list [I], & list [J]);} swap (& list [left], & list [J]); If (j = k) return; else if (j <k) qselect (list, J + 1, right, k); else qselect (list, left, J-1, k );}

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.