Median and sequential statistics

Source: Internet
Author: User

1. maximum and minimum values of Arrays

2. Given array A, search for small I data in the array

3.CodeDownload

1. maximum and minimum Problems

1.1 given an array a, how can we obtain the maximum and minimum elements in array? The most directAlgorithmObviously, it is implemented by traversing arrays. The question is, is this algorithm optimal? Compared with the game elimination mechanism, we can see that the above algorithm is optimal in terms of the number of comparisons. Simple implementation:

Int maxelementinarray (int * arr, int length)

{Int max = arr [0]; for (INT I = 1; I <length; ++ I) {If (ARR [I]> MAX) max = arr [I];} return Max ;}

1.2 The following question is the expansion of the above question. Given an array, how can we obtain the maximum and minimum values of the array through the minimum number of comparisons? Here we have detailed code implementation. Http://www.cnblogs.com/xuqiang/archive/2010/12/07/1953368.html

2. How to Find the small I element of array

2.1 SIMPLE algorithm: first sort array A and then search for the elements smaller than I. Algorithm Implementation is relatively simple and omitted.

2.2. There is a lot of useless work in it. We don't need to sort the array, but we just need to find the small I element. Obviously, the algorithm has a space for optimization. Lenovo's quick sorting algorithm splits array a into two parts by means of segmentation. Then, the sort element's sorting in the array has been determined. Obviously, the above algorithm can be quickly implemented through recursion.

Void swap (int * a, int * B) {int TMP = * A; * A = * B; * B = TMP;} // [start, end] closed interval int quick_partition (int * arr, int start, int end) {// here, the partition is always the last element of the array int partition = arr [end]; // start to split int I =-1; Int J = start; while (j <End) {// incrementing order if (ARR [J] <else) {++ I; swap (& arr [J], & arr [I]);} J ++;} // locate the callback Position + + I; swap (& arr [I], & arr [end]); return I;} // index indicates the iint my_random_select (int * arr, int start, int end, int index) smaller than I) {// recursive end condition // only one element returns if (START = END) return arr [start]; int q = quick_partition (ARR, start, end ); int K = Q-start + 1; if (k = index) return arr [Q]; else if (index <k) return my_random_select (ARR, start, (Q-1), index); else // index> = K return my_random_select (ARR, q + 1, end, (index-k);} int main () {int arr [5] = {1, 2, 10, 4, 5}; cout <my_random_select (ARR, 0, 4, 5) <Endl; return 0 ;} 3. Download Code

/Files/xuqiang/algorithm/maxarrayelement.rar

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.