選擇排序演算法(Selection Sort)

【選擇排序演算法基本思想和案例】

選擇排序:

         每一趟從待排序的數據元素中選出最小(或者最大)的一個元素,順序放在已經排好序的數列的後面,直到全部待排序的數據元素排完。

 案例:

         初始數組資源【63    4    24    1    3    15】

         第一趟排序後【15    4    24    1    3】 63

         第二趟排序後【15    4     3    1】 24   63

         第三趟排序後【  1    4     3】15   24   63

         第四趟排序後【  1    3】 4    15   24   63

         第五趟排序後【  1】 3     4   15   24   63

演算法主要程式碼:

 // 定義方法實現選擇排序   public static void selectionSort(int[] array) {   int count = 1;   while (count >= 1) {   int index = 0;   int max = array[0];   for (int i = 1; i <= array.length - count; i++) {   if (max < array[i]) {  					max = array[i];  					index = i;   }   }   int temp = array[array.length - count];  			array[array.length - count] = max;  			array[index] = temp;  			count++;   if (count == array.length - 1) {   break;   }   }   }  }  案例:    public class SelectSort {   public static void main(String[] args) {   int[] array = {63, 4, 24, 1, 3, 15};   System.out.println("排序前:");   for (int i : array) {   System.out.print(i + "t");   }   System.out.println();  		selectionSort(array);   System.out.println("排序後:");   for (int i : array) {   System.out.print(i + "t");   }   }     // 定義方法實現選擇排序   public static void selectionSort(int[] array) {   int count = 1;   while (count >= 1) {   int index = 0;   int max = array[0];   for (int i = 1; i <= array.length - count; i++) {   if (max < array[i]) {  					max = array[i];  					index = i;   }   }   int temp = array[array.length - count];  			array[array.length - count] = max;  			array[index] = temp;  			count++;   if (count == array.length - 1) {   break;   }   }   }  }