一、冒泡排序算法
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