Introduction to Algorithms 9.2 Choosing with expected linear time

Source: Internet
Author: User

#include <stdint.h> #include <time.h> #include <iostream> #ifdef __linux#include <stdio.h>#    Include <stdlib.h> #endifvoid swap (int64_t* A, uint64_t I, uint64_t j) {int64_t tmp = a[i];    A[i] = A[j]; A[J] = tmp;}    int64_t partition (int64_t* A, int64_t p, int64_t r) {int64_t x = a[r];    int64_t i = p;            for (int64_t j = p; J < R; J + +) {if (A[j] <= x) {swap (A, I, J);        i++;    }} swap (A, I, R); return i;}    int64_t randomizedpartition (int64_t* A, int64_t p, int64_t r) {int64_t i = (rand ()% (r-p)) + p;    Swap (A, I, R); return partition (A, p, r);} Randomized-select (A, p, R, i)/if p = = r//return a[p]//q = randomized-partition (A, p, r)/k = q-p + 1//If I = = k//return a[q]//else if I < k//return Randomized-select (A, p, q-1, I)//else//return Randomized-select  (A, q + 1, R, I-k) int64_t randomizedselect (int64_t* A, int64_t p, int64_t R, int64_t i) {if (P = = r)  {return a[p];    } int64_t q = randomizedpartition (A, p, R);    int64_t k = q-p + 1;    if (i = = k) {return a[q];    } else if (I < k) {return randomizedselect (A, p, q-1, i);    } else {return randomizedselect (A, q + 1, R, I-k);        }}int Main () {int64_t array[] = {2, 8, 7, 1, 3, 5, 6, 4};    Std::cout << randomizedselect (array, 0, 7, 3) << Std::endl;    GetChar (); return 0;}
BIN = BINFMCSA = Find_max_crossing_subarrayias = Ia_solutionist = Insertion_sort_tlmsa = liner_time_max_s Ubarraymerge = mergemerge_t = Merge_tvms = Violate_max_subarraystra = 4.2_strassencount_sort = 8.2_counting_sortM Inimummaximum = 9.1_minimummaximumrs = 9.2_RANDOMIZEDSELECTRQ = 7.3_randomizedquicksortqs = 7.1_quicksortCFLAGS =-g-wal Lall: ${bin}/${count_sort} ${bin}/${rs} ${bin}/${qs} ${BIN}/${FMCSA} ${bin}/${rq} ${bin}/${ias} ${BIN}/${IST} ${BIN}/$ {LMSA} ${bin}/${merge} ${bin}/${merge_t} ${bin}/${vms} ${bin}/${stra} ${bin}/${minimummaximum}${bin}/${count_sort}: ${count_sort}/counting_sort.cppg++ ${count_sort}/counting_sort.cpp ${cflags}-o ${BIN}/${COUNT_SORT}${BIN}/${FMCSA }: ${fmcsa}/main.cpp ${fmcsa}/max_sub_array.hg++ ${fmcsa}/main.cpp ${cflags}-o ${bin}/${fmcsa}${bin}/${ias}: ${IAS}/ Insertion_sort.cpp ${ias}/insertion_sort.hg++ ${ias}/insertion_sort.cpp ${cflags}-o ${BIN}/${IAS}${BIN}/${IST}: ${ Ist}/${ist}.cpp ${ist}/${ist}.hg++ ${ist}/${ist}.cPP ${cflags}-o ${bin}/${ist}${bin}/${lmsa}: ${lmsa}/source.cppg++ ${lmsa}/source.cpp ${cflags}-o ${BIN}/${LMSA}${BIN }/${merge}: ${merge}/${merge}.cpp ${merge}/${merge}.hg++-std=c++0x ${merge}/${merge}.cpp ${CFLAGS}-o ${BIN}/${MERGE }${bin}/${merge_t}: ${merge_t}/${merge_t}.cpp ${merge_t}/${merge_t}.hg++-std=c++0x ${MERGE_T}/${MERGE_T}.cpp ${ CFLAGS}-o ${bin}/${merge_t}${bin}/${vms}: ${vms}/source.cppg++ ${vms}/source.cpp ${cflags}-o ${bin}/${vms}${bin}/${ Stra}: ${stra}/strassen.cpp ${stra}/strassen.hg++-std=c++0x ${stra}/strassen.cpp ${CFLAGS}-o ${BIN}/${STRA}${BIN}/$ {minimummaximum}: ${minimummaximum}/${minimummaximum}.cppg++ ${minimummaximum}/${minimummaximum}.cpp ${CFLAGS}-o $ {Bin}/${minimummaximum}${bin}/${rs}: ${rs}/${rs}.cppg++ ${rs}/${rs}.cpp ${cflags}-o ${bin}/${rs}${bin}/${rq}: ${RQ} /${rq}.cppg++ ${rq}/${rq}.cpp ${cflags}-o ${bin}/${rq}${bin}/${qs}: ${qs}/${qs}.cpp ${qs}/${qs}.hg++ ${QS}/${QS}.cpp ${cflags}-o ${bin}/${qs}clean:rm ${bin}/*

Introduction to Algorithms 9.2 Choosing with expected linear time

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.