排序算法Java实现
堆排序
public class HeapSort {
public static void main(String[] args) {
int[] array = {10, 10, 8, 7, 6, 6, 6, 5, 4, 3, 2, 1};
System.out.println(Arrays.toString(array));
heapSort(array);
System.out.println(Arrays.toString(array));
}
public static void heapSort(int[] array) {
if (array == null || (array.length <= 1)) {
return;
}
for (int i = array.length - 1; 0 < i; i--) {
adjustMaxHeap(array, i);
swap(array, 0, i);
}
}
public static void adjustMaxHeap(int array[], int length) {
int parent = (length - 1) / 2;
for (int i = parent; 0 <= i; i--) {
if (i * 2 + 2 <= length) {
if (array[i * 2 + 1] <= array[i * 2 + 2] && array[i] < array[i * 2 + 2]) {
swap(array, i, i * 2 + 2);
}
if (array[i * 2 + 2] < array[i * 2 + 1] && array[i] < array[i * 2 + 1]) {
swap(array, i, i * 2 + 1);
}
} else {
if (array[i] <= array[i * 2 + 1]) {
swap(array, i, i * 2 + 1);
}
}
}
}
public static void swap(int array[], int i, int j) {
int team = array[i];
array[i] = array[j];
array[j] = team;
}
}
快速排序
public class QuickSort {
public static void main(String[] args) {
testQuick();
}
public static void testQuick() {
int[] array = {10, 8, 11, 5, 2, 4, 3, 3, 1, 7};
quickSort(array, 0, array.length - 1);
System.out.println(Arrays.toString(array));
}
public static void quickSort(int[] array, int low, int high) {
if (low < high) {
int location = partition(array, low, high);
quickSort(array, low, location - 1);
quickSort(array, location + 1, high);
}
}
public static int partition(int[] array, int low, int high) {
int e = array[low];
int l = low;
int h = high;
while (l < h) {
while (l < h && e <= array[h]) h--;
array[l] = array[h];
while (l < h && array[l] <= e) l++;
array[h] = array[l];
}
array[l] = e;
return l;
}
}
双轴快速排序
public class DoubleQuick {
public static void main(String[] args) {
int[] array = {10, 8, 11, 5, 2, 10, 4, 12, 3, 1, 7, 4, 10};
dualPivotQuickSort(array, 0, array.length - 1);
System.out.println(Arrays.toString(array));
}
public static void dualPivotQuickSort(int[] array, int start, int end) {
if (start >= end) {
return;
}
if (array[start] > array[end]) {
swap(array, start, end);
}
int pivot1 = array[start];
int pivot2 = array[end];
int left = start;
int right = end;
int k = left + 1;
while (k < right) {
if (array[k] < pivot1) {
swap(array, left + 1, k);
left++;
k++;
} else if (array[k] <= pivot2) {
k++;
} else {
while (array[right - 1] > pivot2) {
if (right-- == k) break;
}
if (k >= right) break;
swap(array, k, right - 1);
}
}
swap(array, start, left);
swap(array, end, right);
dualPivotQuickSort(array, start, left - 1);
dualPivotQuickSort(array, left + 1, right - 1);
dualPivotQuickSort(array, right + 1, end);
}
public static void swap(int array[], int i, int j) {
int team = array[i];
array[i] = array[j];
array[j] = team;
}
}
直接插入排序
数组
public class DirectInsertSort {
public static void main(String[] args) {
int[] array = {10, 9, 5, 8, 7, 6, 2, 5, 4, 3, 2, 1};
directInsertSort(array);
System.out.println(Arrays.toString(array));
}
public static void directInsertSort(int[] array) {
if (array == null || array.length <= 1) return;
int j;
int tmp;
for (int i = 1; i < array.length; i++) {
j = i - 1;
tmp = array[i];
while (0 <= j && tmp < array[j]) {
array[j + 1] = array[j];
j--;
}
array[j+1] = tmp;
}
}
}
链表
public class DirectInsertListSort {
public static void main(String [] args){
ListNode head = directInsertListSort(getUnSortList());
printList(head);
}
public static ListNode getUnSortList(){
ListNode node1 = new ListNode(7);
ListNode node2 = new ListNode(6);
ListNode node3 = new ListNode(5);
ListNode node4 = new ListNode(6);
ListNode node5 = new ListNode(5);
ListNode node6 = new ListNode(8);
node1.next = node2;
node2.next = node3;
node3.next = node4;
node4.next = node5;
node5.next = node6;
return node1;
}
public static void printList(ListNode head){
while(head != null){
System.out.print(head.val + " ");
head = head.next;
}
}
public static ListNode directInsertListSort(ListNode list){
if(list == null || list.next == null) return list;
ListNode head = list;
ListNode tmp = list.next;
ListNode move = head;
ListNode pre = null;
ListNode current = null;
head.next = null;
while(tmp != null) {
current = tmp;
tmp = tmp.next;
current.next = null;
while(move != null){
if(move.val <= current.val) {
pre = move;
move = move.next;
} else {
break;
}
}
if(pre == null){
current.next = move;
head = current;
} else {
current.next = pre.next;
pre.next = current;
}
pre = null;
move = head;
}
return head;
}
}
class ListNode {
public int val;
public ListNode next;
public ListNode(int x) {
val = x;
next = null;
}
}
归并排序
数组
public class MergeSort {
public static void main(String[] args) {
int[] array = {5, 6, 4, 7, 1, 2, 3, 7, 4};
mergeSort(array, 0, array.length - 1);
System.out.println(Arrays.toString(array));
}
public static void mergeSort(int[] array, int low, int high) {
if (low < high) {
int mid = (high - low) / 2 + low;
mergeSort(array, low, mid);
mergeSort(array, mid + 1, high);
merge(array, low, mid, high);
}
}
public static void merge(int[] array, int low, int mid, int high) {
int[] temp = new int[high - low + 1];
int l = low;
int r = mid + 1;
int p = 0;
while (l <= mid && r <= high) {
temp[p++] = array[l] < array[r] ? array[l++] : array[r++];
}
while (l <= mid) {
temp[p++] = array[l++];
}
while (r <= high) {
temp[p++] = array[r++];
}
for (int i = 0; i < p; i++) {
array[low++] = temp[i];
}
}
}
链表
public class MergeSort {
public static ListNode mergeSort(ListNode head) {
if (head == null || head.next == null) {
return head;
}
ListNode slow = head;
ListNode fast = head;
while (fast.next != null && fast.next.next != null) {
slow = slow.next;
fast = fast.next.next;
if (slow == fast) {
break;
}
}
fast = slow.next;
slow.next = null;
ListNode p1 = mergeSort(head);
ListNode p2 = mergeSort(fast);
return mergeTwoLists(p1, p2);
}
public static ListNode mergeTwoLists(ListNode l1, ListNode l2) {
if (l1 == null ) {
return l2;
}
if (l2 == null ) {
return l1;
}
ListNode l3 = new ListNode(0);
ListNode p;
p = l3;
while (l1 != null && l2 != null) {
if (l1.val < l2.val) {
p.next = l1;
l1 = l1.next;
} else {
p.next = l2;
l2 = l2.next;
}
p = p.next;
}
if (l1 != null) {
p.next = l1;
}
if (l2 != null) {
p.next = l2;
}
return l3.next;
}
}
class ListNode {
public int val;
public ListNode next;
public ListNode(int x) {
val = x;
next = null;
}
}
基数排序
public class RadixSort {
public static void main(String[] args) {
int[] data = new int[]{1, 5, 6, 89, 1100, 192, 221, 12, 23};
radixSort(data, 10, 4);
System.out.println(Arrays.toString(data));
}
public static void radixSort(int[] data, int radix, int d) {
int[] tmp = new int[data.length];
int[] location = new int[radix];
for (int i = 0, rate = 1; i < d; i++) {
Arrays.fill(location,0);
System.arraycopy(data, 0, tmp, 0, data.length);
for (int j = 0; j < data.length; j++) {
location[(tmp[j] / rate) % radix]++;
}
for (int j = 1; j < radix; j++) {
location[j] = location[j] + location[j - 1];
}
for (int j = data.length - 1; j >= 0; j--) {
data[--location[(tmp[j] / rate) % radix]] = tmp[j];
}
rate = rate * radix;
}
}
}