1. 程式人生 > 實用技巧 >常用排序演算法

常用排序演算法

1.氣泡排序

 1 /**
 2  * 
 3  */
 4 package suanfa;
 5 
 6 import java.util.Arrays;
 7 
 8 public class Maopao {
 9 
10     public static void main(String[] args) {
11         int[] nums = { 11, 3, 15, 8, 9, 7, 6 };
12         System.out.println("排序前:" + Arrays.toString(nums));
13         int i, j, temp;
14
boolean swaped = true; 15 for (i = nums.length - 1; i > 0 && swaped; i--) { 16 swaped = false; 17 for (j = 0; j < i; j++) { 18 if (nums[j] > nums[j + 1]) { 19 temp = nums[j]; 20 nums[j] = nums[j + 1];
21 nums[j + 1] = temp; 22 swaped = true; 23 } 24 } 25 } 26 System.out.println("排序後:" + Arrays.toString(nums)); 27 } 28 }

2.選擇排序

 1 /**
 2  * 
 3  */
 4 package suanfa;
 5 
 6 import java.util.Arrays;
 7 
 8 public
class Xuanze { 9 10 public static void main(String[] args) { 11 int[] nums = { 11, 3, 15, 8, 9, 7, 6 }; 12 System.out.println("排序前:" + Arrays.toString(nums)); 13 int i, j, min, temp; 14 for (i = 0; i < nums.length - 1; i++) { 15 min = i; 16 for (j = i + 1; j < nums.length; j++) { 17 if (nums[j] < nums[min]) { 18 min = j; 19 } 20 } 21 temp = nums[min]; 22 nums[min] = nums[i]; 23 nums[i] = temp; 24 } 25 System.out.println("排序後:" + Arrays.toString(nums)); 26 } 27 }

3.插入排序

 1 /**
 2  * 
 3  */
 4 package suanfa;
 5 
 6 import java.util.Arrays;
 7 
 8 public class Charu {
 9 
10     public static void main(String[] args) {
11         int[] nums = { 1, 2, 3, 4, 5, 8, 9, 7, 6 };
12         System.out.println("排序前:" + Arrays.toString(nums));
13         int i, j;
14         for (i = 1; i < nums.length; i++) {
15             for (j = i; j > 0; j--) {
16                 if (nums[j] < nums[j - 1]) {
17                     int temp = nums[j];
18                     nums[j] = nums[j - 1];
19                     nums[j - 1] = temp;
20                 } else {
21                     break;
22                 }
23             }
24         }
25         System.out.println("排序後:" + Arrays.toString(nums));
26     }
27 }

4.快速排序

 1 /**
 2  * 
 3  */
 4 package suanfa;
 5 
 6 import java.util.Arrays;
 7 
 8 public class Kuaisu {
 9 
10     public static void main(String[] args) {
11         int[] a = { 4, 2, 10, 5, 3, 7, 6, 8 };
12         System.out.println("排序前:" + Arrays.toString(a));
13         quickSort(a, 0, a.length - 1);
14         System.out.println("排序後:" + Arrays.toString(a));
15     }
16 
17     public static void quickSort(int[] a, int l, int r) {
18         if (l < r) {
19             int i, j, x;
20             i = l;
21             j = r;
22             x = a[i];
23             while (i < j) {
24                 while (i < j && a[j] > x)
25                     j--; // 從右向左找第一個小於x的數
26                 if (i < j)
27                     a[i++] = a[j];
28                 while (i < j && a[i] < x)
29                     i++; // 從左向右找第一個大於x的數
30                 if (i < j)
31                     a[j--] = a[i];
32             }
33             a[i] = x;
34             quickSort(a, l, i - 1); /* 遞迴呼叫 */
35             quickSort(a, i + 1, r); /* 遞迴呼叫 */
36         }
37     }
38 
39 }