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

                合規國際互聯網加速 OSASE為企業客戶提供高速穩定SD-WAN國際加速解決方案。 廣告
                # Java 程序:選擇排序 > 原文: [https://beginnersbook.com/2019/04/java-program-for-selection-sorting/](https://beginnersbook.com/2019/04/java-program-for-selection-sorting/) 在本教程中,我們將為**選擇排序**編寫一個 Java 程序。 ## 選擇排序算法如何工作? **選擇排序算法**的工作原理是將原始數組分成兩個子數組:排序子數組和未排序子數組,最初排序的子數組為空。該算法通過從未排序的子數組中重復查找最小元素并將其替換為數組的第一個元素來工作,從而使該數組的一部分成為已排序的子數組。這種情況反復發生,直到整個數組被排序。 ## Java 程序:在數組上執行選擇排序 在下面的示例中,我們定義了一個實現選擇排序算法的方法`selectionSort()`。它從數組中找到最小元素,并將其與數組的第一個元素交換。 我們創建了另一種方法`printArr()`來顯示數組的元素。我們在排序之前和之后調用此方法,以在選擇排序之前和之后顯示數組元素。 ```java class JavaExample { void selectionSort(int arr[]) { int len = arr.length; for (int i = 0; i < len-1; i++) { // Finding the minimum element in the unsorted part of array int min = i; for (int j = i+1; j < len; j++) if (arr[j] < arr[min]) min = j; /* Swapping the found minimum element with the first * element of the sorted subarray using temp variable */ int temp = arr[min]; arr[min] = arr[i]; arr[i] = temp; } } // Displays the array elements void printArr(int arr[]) { for (int i=0; i<arr.length; i++) System.out.print(arr[i]+" "); System.out.println(); } public static void main(String args[]) { JavaExample obj = new JavaExample(); int numarr[] = {101,5,18,11,80, 67}; System.out.print("Original array: "); obj.printArr(numarr); //calling method for selection sorting obj.selectionSort(numarr); System.out.print("Sorted array: "); obj.printArr(numarr); } } ``` **輸出:** ![Java Selection sort program with example](https://img.kancloud.cn/1e/ab/1eab746e7b6661bfb7274d586579cbeb_822x184.jpg) #### 相關的 Java 示例 1. [Java 程序:對字符串執行冒泡](https://beginnersbook.com/2019/04/java-program-to-perform-bubble-sort-on-strings/) 2. [Java 程序:按升序對數組進行排序](https://beginnersbook.com/2018/10/java-program-to-sort-an-array-in-ascending-order/) 3. [Java 程序:升序和降序冒泡排序](https://beginnersbook.com/2014/07/java-program-for-bubble-sort-in-ascending-descending-order/) 4. [Java 程序:二分搜索](https://beginnersbook.com/2014/04/java-program-to-perform-binary-search/) 5. [Java 程序:線性搜索](https://beginnersbook.com/2014/04/java-program-for-linear-search-example/)
                  <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>

                              哎呀哎呀视频在线观看