Find the second largest number in the array

Source: Internet
Author: User

1 /************************************** ********************
2 * Description *
3 * find the second big number from an array .*
4 * parameters *
5 * array is the array you want to look up; size is T -*
6 * He array's length; psec is used to store the seco -*
7 * nd big number .*
8 * return value *
9 * parameters invalid,-1 is returned; not find *
10 * second big number,-2 is returned; success, 0 is *
11 * returned .*
12 *************************************** *******************/
13 int find_second_big_number (const int array [], int size, int * psec)
14 {
15 int Max;
16 int second;
17 int I = 0;
18
19 if (null = array | null = psec | size <2)
20 {
21 return-1;
22}
23
24/* initialize Max and second */

25 For (I = 1; I <size; I ++)

26 {
27 if (array [0]! = Array [I])
28 {
29 If (array [0]> array [I])
30 {
31 max = array [0];
32 second = array [I];
33}
34 else
35 {
36 max = array [I];
37 second = array [0];
38}
39 break;
40}
41}
42
43 if (I = size)
44 {
45 return-2;
46}
47
48/* find the second big number */
49 for (; I <size; I ++)

50 {
51 if (max <array [I])
52 {
53 second = max;
54 max = array [I];
55}
56 else if (max> array [I] & Second <array [I])
57 {
58 second = array [I];
59}
60}
61
62 * psec = second;
63
64 return 0;
65}

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.