<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之旅 廣告
                # 選擇排序 Java 示例 > 原文: [https://howtodoinjava.com/algorithm/selection-sort-java-example/](https://howtodoinjava.com/algorithm/selection-sort-java-example/) **選擇排序**是一種簡單且緩慢的排序算法,**反復從未排序部分中選擇最低或最高元素,然后將其移至已排序部分**的末尾。 通常,從性能角度來看,它甚至比[**插入排序**](//howtodoinjava.com/2015/10/27/insertion-sort-java-example/)還要慢。 它不會以任何方式適應數據,因此其運行時間始終是二次的。 但是,您不應得出結論,永遠不要使用選擇排序。 好東西是選擇排序具有**的屬性,可將每次迭代**的交換次數減至最少。 在交換項目成本很高的應用中,選擇排序可能是很好的選擇算法。 ## 選擇排序算法 以下是邏輯代碼結構或通常的選擇排序的偽代碼。 ```java for i = 1:n, k = i for j = i+1:n, if a[j] < a[k], k = j //-> invariant: a[k] smallest of a[i..n] swap a[i,k] //-> invariant: a[1..i] in final position end ``` 用簡單的英語來說,會發生以下情況: 1. 數據元素分為兩部分:一個已排序的部分(最初為空)和一個未排序的部分(最初為完整數組)。 2. 假定排序順序是從低到高,請從未排序部分中找到可比較順序最低的元素。 3. 將找到的元素放在已排序部分的末尾。 4. 重復步驟 2 和 3,直到未排序的部分中沒有剩余的元素。 ## 選擇排序 Java 源代碼 以下是 Java 中的示例**選擇排序實現**。 ```java public class SelectionSortExample { public static void main(String[] args) { // This is unsorted array Integer[] array = new Integer[] { 12, 13, 24, 10, 3, 6, 90, 70 }; // Let's sort using selection sort selectionSort(array, 0, array.length); // Verify sorted array System.out.println(Arrays.toString(array)); } @SuppressWarnings({ "rawtypes", "unchecked" }) public static void selectionSort(Object[] array, int fromIndex, int toIndex) { Object d; for (int currentIndex = fromIndex; currentIndex < toIndex; currentIndex++) { int indexToMove = currentIndex; for (int tempIndexInLoop = currentIndex + 1; tempIndexInLoop < toIndex; tempIndexInLoop++) { if (((Comparable) array[indexToMove]).compareTo(array[tempIndexInLoop]) > 0) { //Swapping indexToMove = tempIndexInLoop; } } d = array[currentIndex]; array[currentIndex] = array[indexToMove]; array[indexToMove] = d; } } } Output: [3, 6, 10, 12, 13, 24, 70, 90] ``` 如果你們知道**有什么方法可以提高選擇排序**的性能,請分享。 我無能為力。 學習愉快!
                  <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>

                              哎呀哎呀视频在线观看