Finding the number of the nth in an unordered array-fast selection algorithm

Source: Internet
Author: User

When you visit other people's blogs, you can see this algorithm by accident, by the way, with C + + implementation.

The simplest solution is to sort the array first and return the nth number.

The following code is a quick selection algorithm (I don't know if the name is right)

1#include <vector>2#include <iostream>3#include <stdexcept>4#include <cstdio>5 6 Const intQs_eerrmsg_len = the;7 8 9 /**Ten * Quickly select the number of the nth large in the unordered array One * Because select returns a reference to an object in the array, A * So error handling has chosen exception -  */ -Template <typename t> the classQuickselect - { -  Public: -typedef TypeName Std::vector<t>:: Size_type size_type; +  -     /** + * Constructor Function A * @brief Quickselect at * @param array of type Array-vector -      */ -Quickselect (std::vector<t>&Array) - : M_array (array) -     {} -  in     /** - * Select the element of the nth large, to * Failed throws Std::out_of_range exception, + * Successful return of the resulting element - * @brief Select the * @param nth * * @return $      */Panax Notoginseng     Constt& -     Select(Size_type nth)Throw(Std::out_of_range) the     { +         //S_pos is the subscript of the nth -largest array after sorting ASize_type S_pos = m_array.size ()-nth; the  +         if(S_pos >m_array.size ()) { -             CharErrmsg[qs_eerrmsg_len]; $  $std::snprintf (ErrMsg, Qs_eerrmsg_len,"Array access Violation:{access:%ld range_length:%ld begin:%ld}.", Nth, M_array.size (),0); - std::out_of_range Oor (errmsg); -             ThrowOor; the         } - Wuyi         return  This->select_pos (S_pos,0, M_array.size ()); the     } -  Wu     /** - * @brief Select About * @param nth $ * @param begin - * @param end - * @return -      */ A     Constt& +     Select(Size_type nth, size_type begin, Size_type end)Throw(Std::out_of_range) the     { -Size_type array_size =m_array.size (); $  the         if(Begin > Array_size | | end > Array_size | | End <begin) { the             CharErrmsg[qs_eerrmsg_len]; the  thestd::snprintf (ErrMsg, Qs_eerrmsg_len,"The begin or end is not correct:{array_size:%ld begin:%ld end:%ld}.", array_size, begin, end); - std::out_of_range Oor (errmsg); in             ThrowOor; the         } the  About         //to find the length of a range theSize_type range_length = End-begin +1; the  the         //The position of the nth- largest element to find within the current range +Size_type S_pos = range_length-nth; -  the         if(S_pos >range_length) {Bayi             CharErrmsg[qs_eerrmsg_len]; the  thestd::snprintf (ErrMsg, Qs_eerrmsg_len,"Array access Violation:{access:%ld range_length:%ld begin:%ld}.", Nth, range_length, begin); - std::out_of_range Oor (errmsg); -             ThrowOor; the         } the         Else the  the         return  This-Select_pos (S_pos, begin, end); -     } the Private: the     /** the * POS represents the position of the element from begin94 * @brief Select_pos the * @param pos the * @param begin the * @param end98 * @return About      */ -     Constt&Select_pos (size_type pos, size_type begin, Size_type end)101     {102t& pivot =M_array[begin];103Size_type Swap_pos =begin;104  the         if(Begin = =end) {106             returnM_array[pos];107         }108 109         //make a quick sort the          for(Size_type i = begin +1; i < End;i + +){111             if(M_array[i] <pivot) { theStd::swap (M_array[i], M_array[swap_pos + +]);113             } the         } the  the         //multiple exchanges when the number of array elements is odd117         if(M_array.size ()%2!=0){118             if(M_array[end-1] <pivot) {119Std::swap (M_array[end-1], M_array[swap_pos + +]); -             }121         }122 123         if(Swap_pos-begin = =POS) {124             returnM_array[swap_pos]; the         }126         Else if(Swap_pos-begin <POS) {127             return  This->select_pos (POS, Swap_pos +1, end); -         }129         Else{ the             return  This-Select_pos (POS, begin, Swap_pos);131         } the     }133 134     /**135 * @brief M_array136      */137std::vector<t>&M_array;138};

Then for the sake of testing convenience, the vector output is realized, very simple output

1 namespacestd{2 3Template <typename t>4std::ostream&operator<< (std::ostream& out,Conststd::vector<t>&Array)5 {6      for(Auto E:array) {7          out<< e <<" ";8     }9      out<<Std::endl;Ten  One     return  out; A } -  -}

Here is the test case

1 intMain ()2 {3std::vector<int> some{1,2,3,5,4};4 5Std::cout <<some;6 7quickselect<int>QS (some);8 9Std::cout <<qs.Select(2) <<Std::endl;Ten  One     return 0; A}

The result output is

[Email protected] myjourney]# g++ quick_select. cpp -std=c++[[email protected] myjourney]#. /123544[[email protected] myjourney]# 



Finding the number of the nth in an unordered array-fast selection algorithm

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.