- 冒泡排序
- import java.util.Random;
- /**
- * 依次比較相鄰的兩個數,將小數放在前面,大數放在後面
- * 冒泡排序,具有穩定性
- * 時間複雜度爲O(n^2)
- * 不及堆排序,快速排序O(nlogn,底數爲2)
- * @author liangge
- *
- */
- public class Main {
- public static void main(String[] args) {
- Random ran = new Random();
- int[] sort = new int[10];
- for(int i = 0 ; i < 10 ; i++){
- sort[i] = ran.nextInt(50);
- }
- System.out.print("排序前的數組爲");
- for(int i : sort){
- System.out.print(i+" ");
- }
- buddleSort(sort);
- System.out.println();
- System.out.print("排序後的數組爲");
- for(int i : sort){
- System.out.print(i+" ");
- }
- }
- /**
- * 冒泡排序
- * @param sort
- */
- private static void buddleSort(int[] sort){
- for(int i=1;i<sort.length;i++){
- for(int j=0;j<sort.length-i;j++){
- if(sort[j]>sort[j+1]){
- int temp = sort[j+1];
- sort[j+1] = sort[j];
- sort[j] = temp;
- }
- }
- }
- }
- }
選擇排序
import java.util.Random; /** * 選擇排序 * 每一趟從待排序的數據元素中選出最小(或最大)的一個元素, * 順序放在已排好序的數列的最後,直到全部待排序的數據元素排完。 * 選擇排序是不穩定的排序方法。 * @author liangge * */ public class Main { public static void main(String[] args) { Random ran = new Random(); int[] sort = new int[10]; for (int i = 0; i < 10; i++) { sort[i] = ran.nextInt(50); } System.out.print("排序前的數組爲"); for (int i : sort) { System.out.print(i + " "); } selectSort(sort); System.out.println(); System.out.print("排序後的數組爲"); for (int i : sort) { System.out.print(i + " "); } } /** * 選擇排序 * @param sort */ private static void selectSort(int[] sort){ for(int i =0;i<sort.length-1;i++){ for(int j = i+1;j<sort.length;j++){ if(sort[j]<sort[i]){ int temp = sort[j]; sort[j] = sort[i]; sort[i] = temp; } } } } }
快速排序
- /**
- * 快速排序 通過一趟排序將要排序的數據分割成獨立的兩部分, 其中一部分的所有數據都比另外一部分的所有數據都要小,
- * 然後再按此方法對這兩部分數據分別進行快速排序, 整個排序過程可以遞歸進行,以此達到整個數據變成有序序列。
- * @author liangge
- *
- */
- public class Main {
- public static void main(String[] args) {
- int[] sort = { 54, 31, 89, 33, 66, 12, 68, 20 };
- System.out.print("排序前的數組爲:");
- for (int data : sort) {
- System.out.print(data + " ");
- }
- System.out.println();
- quickSort(sort, 0, sort.length - 1);
- System.out.print("排序後的數組爲:");
- for (int data : sort) {
- System.out.print(data + " ");
- }
- }
- /**
- * 快速排序
- * @param sort 要排序的數組
- * @param start 排序的開始座標
- * @param end 排序的結束座標
- */
- public static void quickSort(int[] sort, int start, int end) {
- // 設置關鍵數據key爲要排序數組的第一個元素,
- // 即第一趟排序後,key右邊的數全部比key大,key左邊的數全部比key小
- int key = sort[start];
- // 設置數組左邊的索引,往右移動判斷比key大的數
- int i = start;
- // 設置數組右邊的索引,往左移動判斷比key小的數
- int j = end;
- // 如果左邊索引比右邊索引小,則還有數據沒有排序
- while (i < j) {
- while (sort[j] > key && j > start) {
- j--;
- }
- while (sort[i] < key && i < end) {
- i++;
- }
- if (i < j) {
- int temp = sort[i];
- sort[i] = sort[j];
- sort[j] = temp;
- }
- }
- // 如果左邊索引比右邊索引要大,說明第一次排序完成,將sort[j]與key對換,
- // 即保持了key左邊的數比key小,key右邊的數比key大
- if (i > j) {
- int temp = sort[j];
- sort[j] = sort[start];
- sort[start] = temp;
- }
- //遞歸調用
- if (j > start && j < end) {
- quickSort(sort, start, j - 1);
- quickSort(sort, j + 1, end);
- }
- }
- }
插入排序
- /**
- * 直接插入排序
- * 將一個數據插入到已經排好序的有序數據中,從而得到一個新的、個數加一的有序數據
- * 算法適用於少量數據的排序,時間複雜度爲O(n^2)。是穩定的排序方法。
- */
- import java.util.Random;
- public class DirectMain {
- public static void main(String[] args) {
- Random ran = new Random();
- int[] sort = new int[10];
- for (int i = 0; i < 10; i++) {
- sort[i] = ran.nextInt(50);
- }
- System.out.print("排序前的數組爲");
- for (int i : sort) {
- System.out.print(i + " ");
- }
- directInsertSort(sort);
- System.out.println();
- System.out.print("排序後的數組爲");
- for (int i : sort) {
- System.out.print(i + " ");
- }
- }
- /**
- * 直接插入排序
- *
- * @param sort
- */
- private static void directInsertSort(int[] sort) {
- for (int i = 1; i < sort.length; i++) {
- int index = i - 1;
- int temp = sort[i];
- while (index >= 0 && sort[index] > temp) {
- sort[index + 1] = sort[index];
- index--;
- }
- sort[index + 1] = temp;
- }
- }
- }