Two algorithms are used to calculate the k-th largest number in a set of numbers.

Source: Internet
Author: User

Algorithm 1:

Store the number in an array, sort the number in descending order using Bubble Sorting (or other sorting methods), and return the number at the position k-1, suppose there are 10 in this group:

# Include
 
  
# Define MAX_SIZE 10int main () {int mar [MAX_SIZE]; printf ("input these 10: \ n"); for (int I = 0; I <MAX_SIZE; + + I) // input 10 numbers to the array scanf ("% d", & mar [I]); // use the Bubble sorting method to sort the 10 numbers in descending order. int temp = 0; for (int m = 0; m <MAX_SIZE-1; ++ m) for (int n = m + 1; n <MAX_SIZE; ++ n) if (mar [m] <mar [n]) {temp = mar [m]; mar [m] = mar [n]; mar [n] = temp;} // number of output locations k-1, that is, the maximum number k int k = 0; printf ("number of output numbers in this group:"); scanf ("% d", & k); printf ("Number of % d: % d \ n ", k, mar [k-1]); return 0 ;}
 

In the above program, the input 10 numbers are: 22 13 25 32 11 68 72 33 42 90. If you want to output 5th of these numbers, the final result is

5th: 33

Same as expected.

Algorithm 2:

Store the first k number in a group into an array, sort the first k Number in descending order (Bubble sorting or other sorting methods), and then read the remaining number into the array one by one, if the number to be read is smaller than the k number in the array, ignore it. Otherwise, store the number to the correct position in the array and squeeze out a number from the array, until all the numbers in this group are passed over, the end algorithm returns the number of k-1 in the array, that is, the k-number in this group, let us assume that there are 10 numbers in this group:

# Include
 
  
# Define MAX_SIZE 10int main () {int mar [MAX_SIZE]; unsigned k = 0; printf ("Number of the numbers to be output in this group :"); scanf ("% d", & k); printf ("the first % d in the number of input groups:", k); for (unsigned I = 0; I <k; + + I) // enter the number of the first k in the array scanf ("% d", & mar [I]); // sort the k numbers in descending order using the Bubble sorting method int temp = 0; for (unsigned m = 0; m <k-1; ++ m) for (unsigned n = m + 1; n <k; ++ n) if (mar [m] <mar [n]) {temp = mar [m]; mar [m] = mar [n]; mar [n] = temp;} // read the remaining number in this group one by one int tmp = 0; int cnt = 0; printf ("read the remaining number in this group one by one: \ n"); for (unsigned j = 0; j <MAX_SIZE-k; ++ j) {scanf ("% d", & tmp); cnt = k-1; // assign the subscript of the k number in the array to cnt // when the read number is greater than the k Number, insert this number into the correct position in the array and squeeze out a number if (tmp> mar [cnt]) {while (cnt> = 0 & tmp> mar [cnt]) // ensure the subscript is reasonable when cnt is greater than or equal to 0 -- cnt; if (cnt> = 0) {// if the number to be read is no greater than the maximum number in the array, // move all the numbers marked as cnt + 1 to the subscript k-2 to a backward position. (int p = k-2; p> cnt; -- p) mar [p + 1] = mar [p]; mar [cnt + 1] = tmp; // Insert the read value into the correct position in the array} else {// If the read value is larger than all the values in the array, for (int p = k-2; p> = 0; -- p) // move all the numbers in the array to a backward position. mar [p + 1] = mar [p]; mar [0] = tmp; // put the number read into the first position in the array.} printf ("the number of % d in this group is % d \ n", k, mar [k-1]); return 0 ;}
 

In the above program, for the 10 numbers: 22 13 25 32 11 68 72 33 42 90, 5th of these numbers must be output, and the final result is

5th of the numbers in this group are: 33

Correct, consistent with expectation.


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.