简单的选择排序(内部排序)
生活随笔
收集整理的這篇文章主要介紹了
简单的选择排序(内部排序)
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
1 /**
2 *
3 */
4 package com.trfizeng.selectionsort;
5
6 /**
7 * @author trfizeng 內部排序 選擇排序—簡單選擇排序(Simple Selection Sort)
8 */
9 public class SimpleSelectionSort {
10
11 /**
12 * 每次選擇一個最小記錄放在前面去
13 */
14 public static int[] simpleSelectionSort(int[] array) {
15 // 對傳來的待排序數組進行合法驗證
16 if (array != null && array.length != 0) {
17 for (int i = 0; i < array.length; i++) {
18 // 記錄最小記錄的下標
19 int k = i;
20 // 查找最小的記錄 內層循環執行完將找到最小記錄
21 for (int j = i + 1; j < array.length; j++) {
22 // 如果當前記錄是比后面一個大的話就把后面一個下標賦給當前下標
23 if (array[k] > array[j]) {
24 k = j;
25 }
26 }
27 // 把最小記錄與當前記錄進行兌換
28 int temp = array[i];
29 array[i] = array[k];
30 array[k] = temp;
31 }
32 }
33 return array;
34 }
35 } View Code 1 package com.trfizeng.test;
2
3 import com.trfizeng.insertionsort.StraightInsertionSort;
4 import com.trfizeng.selectionsort.SimpleSelectionSort;
5
6 /**
7 * 測試類
8 *
9 * @author trfizeng
10 *
11 */
12 public class SortTest {
13 // 待排序數組
14 static int[] array = new int[] { 6, 1, 4, 10, 11, 8, 7, 1, 0 };
15
16 /**
17 * 直接插入排序法測試
18 */
19 public static void straightInsertionSortTest() {
20 System.out.print("待排序數組:[ ");
21 for (int i = 0; i < array.length; i++) {
22 System.out.print(array[i] + " ");
23 }
24 System.out.print("] ");
25
26 array = StraightInsertionSort.straightInsertionSort(array);
27 System.out.print("排好序的數組:[ ");
28 for (int i = 0; i < array.length; i++) {
29 System.out.print(array[i] + " ");
30 }
31 System.out.print("]");
32 }
33
34 /**
35 * 選擇排序
36 */
37 public static void simpleSelectionSort() {
38 System.out.print("待排序數組:[ ");
39 for (int i = 0; i < array.length; i++) {
40 System.out.print(array[i] + " ");
41 }
42 System.out.print("] ");
43
44 array = SimpleSelectionSort.simpleSelectionSort(array);
45 System.out.print("排好序的數組:[ ");
46 for (int i = 0; i < array.length; i++) {
47 System.out.print(array[i] + " ");
48 }
49 System.out.print("]");
50
51 }
52
53 public static void main(String[] args) {
54 // SortTest.straightInsertionSortTest();
55
56 SortTest.simpleSelectionSort();
57
58 }
59 } View Code
?
轉載于:https://www.cnblogs.com/trfizeng/p/4307723.html
總結
以上是生活随笔為你收集整理的简单的选择排序(内部排序)的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 处理器与内存和硬盘的交互
- 下一篇: 熟悉淘宝API