Java实现各种简单排序
冒泡排序
package cn.lg.sort;
/**
* 冒泡排序
* Created by L on 2017/3/19.
*/
public class BubbleSortDemo {
public static void main(String[] args) {
int[] arr = {12, 43, 21, 353, 21, 1, 35, 6, 3, 23, 7, 98, 43, 32, 4};
System.out.println("排序前长度:" + arr.length);
bubbleSort(arr);
for (int i :
arr) {
System.out.print(i + " ");
}
System.out.println("\n排序后长度:" + arr.length);
}
public static void bubbleSort(int[] arr) {
for (int i = 0; i < arr.length; i++) {
boolean flag = false;
for (int j = arr.length - 1; j > i; j--) {
if (arr[j] < arr[j - 1]) {
swap(arr, j, j - 1);
flag = true;
}
}
if (flag == false) return;
}
}
public static void swap(int[] arr, int i, int j) {
int t = arr[i];
arr[i] = arr[j];
arr[j] = t;
}
}
选择排序
package cn.lg.sort;
/**
* 选择排序
* Created by L on 2017/3/19.
*/
public class SelectSortDemo {
public static void main(String[] args) {
int[] arr = {564, 432, 123, 765, 78, 23, 24, 98, 32, 87, 2, 45, 21, 890, 32, 4, 56, 2, 743, 32};
System.out.println("排序前长度:" + arr.length);
selectSort(arr);
for (int i :
arr) {
System.out.print(i + " ");
}
System.out.println("\n排序后长度:" + arr.length);
}
public static void selectSort(int[] arr) {
for (int i = 0; i < arr.length; i++) {
int min = i;
for (int j = arr.length - 1; j > i; j--) {
if (arr[min] > arr[j]) min = j;
}
swap(arr, i, min);
}
}
public static void swap(int[] arr, int i, int j) {
int t = arr[i];
arr[i] = arr[j];
arr[j] = t;
}
}
插入排序
package cn.lg.sort;
/**
* 插入排序
* Created by L on 2017/3/19.
*/
public class InsertSortDemo {
public static void main(String[] args) {
int[] arr = {12, 43, 21, 353, 21, 1, 35, 6, 3, 23, 7, 98, 1, 43, 32, 4};
System.out.println("排序前长度:" + arr.length);
insertSort(arr);
for (int i :
arr) {
System.out.print(i + " ");
}
System.out.println("\n排序后长度:" + arr.length);
}
public static void insertSort(int[] arr) {
for (int i = 1; i < arr.length; i++) {
for (int j = i; j > 0; j--) {
if (arr[j] < arr[j - 1]) {
swap(arr, j, j - 1);
}
}
}
}
public static void swap(int[] arr, int i, int j) {
int t = arr[i];
arr[i] = arr[j];
arr[j] = t;
}
}
快速排序
package cn.lg.sort;
/**
* 快速排序
* Created by L on 2017/3/19.
*/
public class QuickSortDemo {
public static void main(String[] args) {
int[] arr = {54, 34, 365, 3, 1, 56, 8, 5, 4356, 43242,4356,467, 98, 45, 97, 67, 43, 342, 0, 345,1,0};
System.out.println("排序前长度:" + arr.length);
quickSort(arr, 0, arr.length - 1);
for (int i : arr) {
System.out.print(i + " ");
}
System.out.println("\n排序后长度:" + arr.length);
}
public static void quickSort(int[] arr, int low, int high) {
if (low < high) {
int mid = partition(arr, low, high);
quickSort(arr, low, mid - 1);
quickSort(arr, mid + 1, high);
}
}
public static int partition(int[] arr, int low, int high) {
int key = arr[low];
while (low < high) {
while (low < high && arr[high] >= key) --high;
arr[low] = arr[high];
while (low < high && arr[low] <= key) ++low;
arr[high] = arr[low];
}
arr[low] = key;
return low;
}
}
堆排序
package cn.lg.sort;
import java.util.Arrays;
/**
* 大根堆
* Created by L on 2017/3/19.
*/
public class HeapSortDemo {
public static void main(String[] args) {
int[] a = {51, 46, 20, 18, 65, 97, 82, 30, 77, 50};
heapSort(a);
System.out.println(Arrays.toString(a));
}
public static void heapSort(int[] a) {
int i;
for (i = a.length / 2 - 1; i >= 0; i--) {
adjustHeap(a, i, a.length - 1);
}
for (i = a.length - 1; i >= 0; i--) {
int temp = a[0];
a[0] = a[i];
a[i] = temp;
adjustHeap(a, 0, i - 1);
}
}
public static void adjustHeap(int[] a, int i, int len) {
int temp, j;
temp = a[i];
for (j = 2 * i; j < len; j *= 2) {
if (j < len && a[j] < a[j + 1])
++j;
if (temp >= a[j])
break;
a[i] = a[j];
i = j;
}
a[i] = temp;
}
}
希尔排序
package cn.lg.sort;
import java.util.Arrays;
/**
* 希尔排序
* Created by L on 2017/3/19.
*/
public class ShellSortDemo {
public static void main(String[] args) {
int[] data = new int[]{26, 53, 67, 48, 57, 13, 48, 32, 60, 50};
shellSortSmallToBig(data);
System.out.println(Arrays.toString(data));
}
public static void shellSortSmallToBig(int[] data) {
int j = 0;
int temp = 0;
for (int increment = data.length / 2; increment > 0; increment /= 2) {
System.out.println("increment:" + increment);
for (int i = increment; i < data.length; i++) {
temp = data[i];
for (j = i - increment; j >= 0; j -= increment) {
if (temp < data[j]) {
data[j + increment] = data[j];
} else {
break;
}
}
data[j + increment] = temp;
}
for (int i = 0; i < data.length; i++)
System.out.print(data[i] + " ");
}
}
}
归并排序
package cn.lg.sort;
import java.util.Arrays;
/**
* 归并排序
* Created by L on 2017/3/19.
*/
public class MergeSortDemo {
public static void main(String[] args) {
int a[] = {51, 46, 20, 18, 65, 97, 82, 30, 77, 50};
mergeSort(a, 0, a.length - 1);
System.out.println("排序结果:" + Arrays.toString(a));
}
public static void mergeSort(int[] a, int low, int high) {
int mid = (low + high) / 2;
if (low < high) {
mergeSort(a, low, mid);
mergeSort(a, mid + 1, high);
merge(a, low, mid, high);
System.out.println(Arrays.toString(a));
}
}
public static void merge(int[] a, int low, int mid, int high) {
int[] temp = new int[high - low + 1];
int i = low;
int j = mid + 1;
int k = 0;
while (i <= mid && j <= high) {
if (a[i] < a[j]) {
temp[k++] = a[i++];
} else {
temp[k++] = a[j++];
}
}
while (i <= mid) {
temp[k++] = a[i++];
}
while (j <= high) {
temp[k++] = a[j++];
}
for (int k2 = 0; k2 < temp.length; k2++) {
a[k2 + low] = temp[k2];
}
}
}