<ruby id="bdb3f"></ruby>

    <p id="bdb3f"><cite id="bdb3f"></cite></p>

      <p id="bdb3f"><cite id="bdb3f"><th id="bdb3f"></th></cite></p><p id="bdb3f"></p>
        <p id="bdb3f"><cite id="bdb3f"></cite></p>

          <pre id="bdb3f"></pre>
          <pre id="bdb3f"><del id="bdb3f"><thead id="bdb3f"></thead></del></pre>

          <ruby id="bdb3f"><mark id="bdb3f"></mark></ruby><ruby id="bdb3f"></ruby>
          <pre id="bdb3f"><pre id="bdb3f"><mark id="bdb3f"></mark></pre></pre><output id="bdb3f"></output><p id="bdb3f"></p><p id="bdb3f"></p>

          <pre id="bdb3f"><del id="bdb3f"><progress id="bdb3f"></progress></del></pre>

                <ruby id="bdb3f"></ruby>

                企業??AI智能體構建引擎,智能編排和調試,一鍵部署,支持知識庫和私有化部署方案 廣告
                # 查找數組中的最小和第二個最小元素 > 原文: [https://www.geeksforgeeks.org/to-find-smallest-and-second-smallest-element-in-an-array/](https://www.geeksforgeeks.org/to-find-smallest-and-second-smallest-element-in-an-array/) 編寫高效的 C 程序以查找數組中的最小和第二個最小元素。 ![](https://img.kancloud.cn/d6/d0/d6d0d58c7341579d4842937b47744b7c_1024x512.png) **示例**: ``` Input: arr[] = {12, 13, 1, 10, 34, 1} Output: The smallest element is 1 and second Smallest element is 10 ``` **簡單解決方案**是按遞增順序對數組進行排序。 排序數組中的前兩個元素將是兩個最小的元素。 該解決方案的時間復雜度為`O(N log N)`。 **更好的解決方案**是掃描數組兩次。 在第一次遍歷中找到最小元素。 將此元素設為`x`。 在第二遍歷中,找到大于`x`的最小元素。 該解決方案的時間復雜度為`O(n)`。 上面的解決方案需要兩次遍歷輸入數組。 **有效解決方案**可以在一次遍歷中找到最少兩個元素。 下面是完整的算法。 **算法**: ``` 1) Initialize both first and second smallest as INT_MAX *first* = *second* = INT_MAX 2) Loop through all the elements. a) If the current element is smaller than *first*, then update *first* and *second*. b) Else if the current element is smaller than *second* then update *second* ``` **實現**: ## C++ ```cpp // C++ program to find smallest and? // second smallest elements? #include <bits/stdc++.h> using namespace std; /* For INT_MAX */ void print2Smallest(int arr[], int arr_size)? {? ????int i, first, second;? ????/* There should be atleast two elements */ ????if (arr_size < 2)? ????{? ????????cout<<" Invalid Input ";? ????????return;? ????}? ????first = second = INT_MAX;? ????for (i = 0; i < arr_size ; i ++)? ????{? ????????/* If current element is smaller than first? ????????then update both first and second */ ????????if (arr[i] < first)? ????????{? ????????????second = first;? ????????????first = arr[i];? ????????}? ????????/* If arr[i] is in between first and second? ????????then update second */ ????????else if (arr[i] < second && arr[i] != first)? ????????????second = arr[i];? ????}? ????if (second == INT_MAX)? ????????cout << "There is no second smallest element\n";? ????else ????????cout << "The smallest element is " << first << " and second " ????????????"Smallest element is " << second << endl;? }? /* Driver code */ int main()? {? ????int arr[] = {12, 13, 1, 10, 34, 1};? ????int n = sizeof(arr)/sizeof(arr[0]);? ????print2Smallest(arr, n);? ????return 0;? }? // This is code is contributed by rathbhupendra ```
                  <ruby id="bdb3f"></ruby>

                  <p id="bdb3f"><cite id="bdb3f"></cite></p>

                    <p id="bdb3f"><cite id="bdb3f"><th id="bdb3f"></th></cite></p><p id="bdb3f"></p>
                      <p id="bdb3f"><cite id="bdb3f"></cite></p>

                        <pre id="bdb3f"></pre>
                        <pre id="bdb3f"><del id="bdb3f"><thead id="bdb3f"></thead></del></pre>

                        <ruby id="bdb3f"><mark id="bdb3f"></mark></ruby><ruby id="bdb3f"></ruby>
                        <pre id="bdb3f"><pre id="bdb3f"><mark id="bdb3f"></mark></pre></pre><output id="bdb3f"></output><p id="bdb3f"></p><p id="bdb3f"></p>

                        <pre id="bdb3f"><del id="bdb3f"><progress id="bdb3f"></progress></del></pre>

                              <ruby id="bdb3f"></ruby>

                              哎呀哎呀视频在线观看