1.快速排序
import java.util.concurrent.BrokenBarrierException;
public class Main {
public static void main(String[] args) throws InterruptedException, BrokenBarrierException {
Main main = new Main();
int[] array = {1, 4, 10, 101, 2, 5, 9};
main.quickSort(array, 0, array.length - 1);
for (int i : array) {
System.out.print(i + ",");
}
}
// 快速排序
private void quickSort(int[] numbers, int from, int to) {
if (from >= to) {
return;
}
int pivot = dispart(numbers, from, to );
quickSort(numbers, from, pivot -1 );
quickSort(numbers, pivot + 1, to);
}
private int dispart(int[] numbers, int low, int high) {
int pivot = numbers[low];
while (low < high) {
while (low < high && numbers[high] >= pivot) {
high--;
}
numbers[low] = numbers[high];
while (low < high && numbers[low] <= pivot) {
low++;
}
numbers[high] = numbers[low];
}
numbers[low] = pivot;
return low;
}
}
2.归并排序
import java.util.concurrent.BrokenBarrierException;
public class Main {
public static void main(String[] args) {
Main main = new Main();
int[] array = {1, 4, 10, 101, 2, 5, 9};
main.mergeSort(array, 0, array.length - 1);
for (int i : array) {
System.out.println(i + ",");
}
}
// 归并排序
private void mergeSort(int[] numbers, int from, int to) {
if (from >= to) {
return;
}
int mid = (to + from) / 2;
mergeSort(numbers, from, mid);
mergeSort(numbers, mid + 1, to);
merge(numbers, from, mid, to);
}
private void merge(int[] numbers, int from, int mid, int to) {
int[] tmp = new int[to - from + 1];
int left = from,right=mid+1;
int idx = 0;
while (left <= mid && right <= to) {
if (numbers[left] <= numbers[right]) {
tmp[idx++] = numbers[left++];
}else{
tmp[idx++] = numbers[right++];
}
}
if(left <= mid) {
while (left <= mid) {
tmp[idx++] = numbers[left++];
}
}
if(right <= to) {
while (right <= to) {
tmp[idx++] = numbers[right++];
}
}
System.arraycopy(tmp, 0, numbers, from, to-from+1);
}
}
3.堆排序
import java.util.ArrayList;
import java.util.Arrays;
public class Solution {
public ArrayList GetLeastNumbers_Solution(int [] input, int k) {
ArrayList res = new ArrayList<>();
if (input == null || input.length == 0 || input.length < k || k <= 0) {
return res;
}
//deal
int[] heap = new int[k];
for (int i = 0;i < k ; i++) {
insert(heap, i, input[i]);
}
for (int i = k; i < input.length; i++) {
if (input[i] < heap [0]) {
delete(heap, input[i]);
}
}
Arrays.sort(heap);
for (int i = 0; i < k; i++) {
res.add(heap[i]);
}
return res;
}
//insert 大根堆
private void insert(int[] heap, int idx, int num) {
heap[idx] = num;
//向上调整
while (idx != 0) {
int parent = idx/2;
if (heap[parent] > heap[idx]) {
break;
} else {
int tmp = heap[idx];
heap[idx] = heap[parent];
heap[parent] = tmp;
idx = parent;
}
}
}
//delete
private void delete(int[] heap, int num) {
heap[0] = num;
int len = heap.length;
//向下调整
int left = 1, right = 2;
int idx = 0;
while (left < len) {
int largest = idx;
if (heap[left] > heap[idx]){
largest = left;
}
if (right < len && heap[right] > heap[largest]) {
largest = right;
}
if (largest == idx) {
break;
}
int tmp = heap[idx];
heap[idx] = heap[largest];
heap[largest] = tmp;
left = 2 * largest + 1;
right = 2 * largest + 2;
}
}
}
4.冒泡排序
import java.util.concurrent.BrokenBarrierException;
public class Main {
public static void main(String[] args) {
Main main = new Main();
int[] array = {1, 4, 10, 101, 2, 5, 9};
main.bubbleSort(array,array.length);
for (int i : array) {
System.out.println(i + ",");
}
}
private void bubbleSort(int[] array, int len) {
for (int i = 0; i < len; i++) {
boolean flag = false;
for (int j = len - 1; j > i; j--) {
if (array[j-1] > array[j]) {
int tmp = array[j-1];
array[j-1] = array[j];
array[j] = tmp;
flag = true;
}
}
if (!flag) {
break;
}
}
}
}