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
相关推荐
激流丶2 分钟前
【Kafka 实战】如何解决Kafka Topic数量过多带来的性能问题?
java·大数据·kafka·topic
Themberfue6 分钟前
Java多线程详解⑤(全程干货!!!)线程安全问题 || 锁 || synchronized
java·开发语言·线程·多线程·synchronized·
小码农<^_^>21 分钟前
优选算法精品课--滑动窗口算法(一)
算法
让学习成为一种生活方式22 分钟前
R包下载太慢安装中止的解决策略-R语言003
java·数据库·r语言
羊小猪~~23 分钟前
神经网络基础--什么是正向传播??什么是方向传播??
人工智能·pytorch·python·深度学习·神经网络·算法·机器学习
晨曦_子画28 分钟前
编程语言之战:AI 之后的 Kotlin 与 Java
android·java·开发语言·人工智能·kotlin
软工菜鸡1 小时前
预训练语言模型BERT——PaddleNLP中的预训练模型
大数据·人工智能·深度学习·算法·语言模型·自然语言处理·bert
南宫生1 小时前
贪心算法习题其三【力扣】【算法学习day.20】
java·数据结构·学习·算法·leetcode·贪心算法
Heavydrink1 小时前
HTTP动词与状态码
java
ktkiko111 小时前
Java中的远程方法调用——RPC详解
java·开发语言·rpc