选择排序算法(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;   }   }   }  }