java实现六大排序算法

一、冒泡排序算法

bash 复制代码
package com.xxx.order;

public class maopao {
        public static void main(String[] args) {
            int[] arr = {64, 34, 25, 12, 22, 11, 90};
            bubbleSort(arr);
            System.out.println("Sorted array: ");
            printArray(arr);
        }

        static void bubbleSort(int[] arr) {
            int n = arr.length;
            for (int i = 0; i < n - 1; i++) {
                for (int j = 0; j < n - i - 1; j++) {
                    if (arr[j] > arr[j + 1]) {
                        // Swap arr[j+1] and arr[j]
                        int temp = arr[j];
                        arr[j] = arr[j + 1];
                        arr[j + 1] = temp;
                    }
                }
            }
        }

        /* Prints the array */
        static void printArray(int[] arr) {
            int n = arr.length;
            for (int i = 0; i < n; ++i)
                System.out.print(arr[i] + " ");
            System.out.println();
        }

}

运行结果:

bash 复制代码
Sorted array: 
11 12 22 25 34 64 90 

二、选择排序算法

bash 复制代码
public class SelectionSortExample {
    public static void main(String[] args) {  
        int[] arr = {64, 25, 12, 22, 11};  
        selectionSort(arr);  
        System.out.println("Sorted array: ");  
        printArray(arr);  
    }  
  
    static void selectionSort(int[] arr) {  
        int n = arr.length;  
        for (int i = 0; i < n-1; i++) {  
            int minIndex = i;  
            for (int j = i+1; j < n; j++) {  
                if (arr[j] < arr[minIndex]) {  
                    minIndex = j;  
                }  
            }  
            int temp = arr[minIndex];  
            arr[minIndex] = arr[i];  
            arr[i] = temp;  
        }  
    }  
  
    /* Prints the array */  
    static void printArray(int[] arr) {  
        int n = arr.length;  
        for (int i=0; i<n; ++i) {  
            System.out.print(arr[i]+" ");  
        }  
        System.out.println();  
    }  
}

运行结果:

bash 复制代码
Sorted array: 
11 12 22 25 64 

三、希尔排序算法

bash 复制代码
public class ShellSortExample {
    public static void main(String[] args) {  
        int[] arr = {64, 25, 12, 22, 11};  
        shellSort(arr);  
        System.out.println("Sorted array: ");  
        printArray(arr);  
    }  
  
    static void shellSort(int[] arr) {  
        int n = arr.length;  
        int gap = n/2;  
        while (gap > 0) {  
            for (int i = gap; i < n; i++) {  
                int temp = arr[i];  
                int j;  
                for (j = i; j >= gap && arr[j-gap] > temp; j -= gap) {  
                    arr[j] = arr[j-gap];  
                }  
                arr[j] = temp;  
            }  
            gap /= 2;  
        }  
    }  
  
    /* Prints the array */  
    static void printArray(int[] arr) {  
        int n = arr.length;  
        for (int i=0; i<n; ++i) {  
            System.out.print(arr[i]+" ");  
        }  
        System.out.println();  
    }  
}

运行结果:

bash 复制代码
Sorted array: 
11 12 22 25 64 

四、插入排序算法

bash 复制代码
public class InsertionSortExample {  
    public static void main(String[] args) {  
        int[] arr = {64, 25, 12, 22, 11};  
        insertionSort(arr);  
        System.out.println("Sorted array: ");  
        printArray(arr);  
    }  
  
    static void insertionSort(int[] arr) {  
        int n = arr.length;  
        for (int i = 1; i < n; i++) {  
            int key = arr[i];  
            int j = i - 1;  
            while (j >= 0 && arr[j] > key) {  
                arr[j + 1] = arr[j];  
                j = j - 1;  
            }  
            arr[j + 1] = key;  
        }  
    }  
  
    /* Prints the array */  
    static void printArray(int[] arr) {  
        int n = arr.length;  
        for (int i=0; i<n; ++i) {  
            System.out.print(arr[i]+" ");  
        }  
        System.out.println();  
    }  
}

运行结果:

bash 复制代码
Sorted array: 
11 12 22 25 64 

五、堆排序算法

bash 复制代码
public class HeapSortExample {  
    public static void main(String[] args) {  
        int[] arr = {64, 25, 12, 22, 11};  
        heapSort(arr);  
        System.out.println("Sorted array: ");  
        printArray(arr);  
    }  
  
    static void heapSort(int[] arr) {  
        int n = arr.length;  
        // Build heap  
        for (int i = n / 2 - 1; i >= 0; i--) {  
            heapify(arr, n, i);  
        }  
        // One by one extract an element from heap  
        for (int i = n - 1; i >= 0; i--) {  
            // Move current root to end  
            int temp = arr[0];  
            arr[0] = arr[i];  
            arr[i] = temp;  
            // call max heapify on the reduced heap  
            heapify(arr, i, 0);  
        }  
    }  
  
    /* To heapify a subtree rooted with node i which is an index in arr[]. n is size of heap */  
    static void heapify(int[] arr, int n, int i) {  
        int largest = i; // Initialize largest as root  
        int left = 2 * i + 1; // left = 2*i + 1  
        int right = 2 * i + 2; // right = 2*i + 2  
        // If left child is larger than root  
        if (left < n && arr[left] > arr[largest]) {  
            largest = left;  
        }  
        // If right child is larger than largest so far  
        if (right < n && arr[right] > arr[largest]) {  
            largest = right;  
        }  
        // If largest is not root  
        if (largest != i) {  
            int swap = arr[i];  
            arr[i] = arr[largest];  
            arr[largest] = swap;  
            // Recursively heapify the affected sub-tree  
            heapify(arr, n, largest);  
        }  
    }  
  
    /* Prints the array */  
    static void printArray(int[] arr) {  
        int n = arr.length;  
        for (int i=0; i<n; ++i) {  
            System.out.print(arr[i]+" ");  
        }  
        System.out.println();  
    }  
}

运行结果:

bash 复制代码
Sorted array: 
11 12 22 25 64 

六、合并排序算法

bash 复制代码
public class MergeSortExample {  
    public static void main(String[] args) {  
        int[] arr = {64, 25, 12, 22, 11};  
        mergeSort(arr, 0, arr.length - 1);  
        System.out.println("Sorted array: ");  
        printArray(arr);  
    }  
  
    static void mergeSort(int[] arr, int l, int r) {  
        if (l < r) {  
            int m = (l + r) / 2;  
            mergeSort(arr, l, m);  
            mergeSort(arr, m + 1, r);  
            merge(arr, l, m, r);  
        }  
    }  
  
    static void merge(int[] arr, int l, int m, int r) {  
        int n1 = m - l + 1;  
        int n2 = r - m;  
        int L[] = new int[n1];  
        int R[] = new int[n2];  
        for (int i = 0; i < n1; ++i) {  
            L[i] = arr[l + i];  
        }  
        for (int j = 0; j < n2; ++j) {  
            R[j] = arr[m + 1 + j];  
        }  
        int i = 0, j = 0;  
        int k = l;  
        while (i < n1 && j < n2) {  
            if (L[i] <= R[j]) {  
                arr[k] = L[i];  
                i++;  
            } else {  
                arr[k] = R[j];  
                j++;  
            }  
            k++;  
        }  
        while (i < n1) {  
            arr[k] = L[i];  
            i++;  
            k++;  
        }  
        while (j < n2) {  
            arr[k] = R[j];  
            j++;  
            k++;  
        }  
    }  
  
    /* Prints the array */  
    static void printArray(int[] arr) {  
        int n = arr.length;  
        for (int i=0; i<n; ++i) {  
            System.out.print(arr[i]+" ");  
        }  
        System.out.println();  
    }  
}

运行结果:

bash 复制代码
Sorted array: 
11 12 22 25 64
相关推荐
my rainy days13 分钟前
C++:友元
开发语言·c++·算法
haoly198915 分钟前
数据结构和算法篇-归并排序的两个视角-迭代和递归
数据结构·算法·归并排序
微笑尅乐15 分钟前
中点为根——力扣108.讲有序数组转换为二叉搜索树
算法·leetcode·职场和发展
小梁努力敲代码18 分钟前
java数据结构--List的介绍
java·开发语言·数据结构
摸鱼的老谭43 分钟前
构建Agent该选Python还是Java ?
java·python·agent
lang201509281 小时前
Spring Boot 官方文档精解:构建与依赖管理
java·spring boot·后端
夫唯不争,故无尤也1 小时前
Tomcat 启动后只显示 index.jsp,没有进入你的 Servlet 逻辑
java·servlet·tomcat
zz-zjx1 小时前
Tomcat核心组件全解析
java·tomcat
im_AMBER1 小时前
算法笔记 05
笔记·算法·哈希算法
Deschen1 小时前
设计模式-外观模式
java·设计模式·外观模式