<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>

                ThinkChat2.0新版上線,更智能更精彩,支持會話、畫圖、視頻、閱讀、搜索等,送10W Token,即刻開啟你的AI之旅 廣告
                # 線性搜索 > 原文: [https://www.programiz.com/dsa/linear-search](https://www.programiz.com/dsa/linear-search) #### 在本教程中,您將學習線性搜索。 此外,您還將找到線性搜索 C,C++ ,Java 和 Python 的工作示例。 線性搜索是最簡單的搜索算法,可以按順序搜索列表中的元素。 我們從一端開始,檢查每個元素,直到找不到所需的元素。 * * * ## 線性搜索如何工作? 按照以下步驟在下面的列表中搜索元素`k = 1`。 ![Initial array](https://img.kancloud.cn/8a/a7/8aa7248117bc0c9e06e496fa8c5d236a_754x196.png "Linear Search Array") 要搜索的數組 1. 從第一個元素開始,將`k`與每個元素`x`進行比較。 ![Element not found](https://img.kancloud.cn/d6/a8/d6a8cf2413e5bed4b09ea9d367d59a5d_754x890.png "Linear Search") 與每個元素比較 2. 如果為`x == k`,則返回索引。 找到 ![Element found](https://img.kancloud.cn/14/d2/14d252661fc4ee782bea0e1de7152a17_754x302.png "Linear Search") 元素 3. 否則,返回`"not found"`。 * * * ## 線性搜索算法 ``` LinearSearch(array, key) for each item in the array if item == value return its index ``` * * * ## Python,Java 和 C/C++ 示例 ```py # Linear Search in Python def linearSearch(array, n, x): # Going through array sequencially for i in range(0, n): if (array[i] == x): return i return -1 array = [2, 4, 0, 1, 9] x = 1 n = len(array) result = linearSearch(array, n, x) if(result == -1): print("Element not found") else: print("Element found at index: ", result) ``` ```java // Linear Search in Java class LinearSearch { public static int linearSearch(int array[], int x) { int n = array.length; // Going through array sequencially for (int i = 0; i < n; i++) { if (array[i] == x) return i; } return -1; } public static void main(String args[]) { int array[] = { 2, 4, 0, 1, 9 }; int x = 1; int result = linearSearch(array, x); if (result == -1) System.out.print("Element not found"); else System.out.print("Element found at index: " + result); } } ``` ```c // Linear Search in C #include <stdio.h> int search(int array[], int n, int x) { // Going through array sequencially for (int i = 0; i < n; i++) if (array[i] == x) return i; return -1; } int main() { int array[] = {2, 4, 0, 1, 9}; int x = 1; int n = sizeof(array) / sizeof(array[0]); int result = search(array, n, x); (result == -1) ? printf("Element not found") : printf("Element found at index: %d", result); } ``` ```cpp // Linear Search in C++ #include <iostream> using namespace std; int search(int array[], int n, int x) { // Going through array sequencially for (int i = 0; i < n; i++) if (array[i] == x) return i; return -1; } int main() { int array[] = {2, 4, 0, 1, 9}; int x = 1; int n = sizeof(array) / sizeof(array[0]); int result = search(array, n, x); (result == -1) ? cout << "Element not found" : cout << "Element found at index: " << result; } ``` * * * ## 線性搜索的復雜度 **時間復雜度**: `O(n)` **空間復雜度**: `O(1)` * * * ## 線性搜索應用 1. 用于較小數組中的搜索操作(`< 100`個項目)。
                  <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>

                              哎呀哎呀视频在线观看