插入排序
//插入排序
public static void insertionsort(int[] arr){
if (arr == null || arr.length < 2){
return;
}
for (int i = 0; i < arr.length; i++) {
for (int j = i-1; j >= 0 ; j--) {
if (arr[j] > arr[j+1]){
sway(arr,j,j+1);
}
}
}
}
归并排序
//归并排序
public static void process(int[] arr,int L, int R){
if (L == R){
return;
}
int mid = L + ((R - L) >> 1);
process(arr,L,mid);
process(arr,mid+1,R);
merge(arr,L,mid,R);
}
private static void merge(int[] arr, int l, int mid, int r) {
int[] help = new int[r-l+1];
int i = 0;
int p1 = l;
int p2 = mid+1;
while (p1 <= mid && p2 <= r){
help[i++] = arr[p1] <= arr[p2]? arr[p1++] : arr[p2++];
}
while (p1 <= mid){
help[i++] = arr[p1++];
}
while (p2 <= r){
help[i++] = arr[p2++];
}
for (int j = 0; j < help.length; j++) {
arr[l+j] = help[j];
}
}
求小合
public static int process(int[] arr,int left,int right){
if (left == right){
return 0;
}
int mid = left + ((right - left) >> 1);
return process(arr,left,mid)
+ process(arr,mid+1,right)
+ merge(arr,left,mid,right);
}
private static int merge(int[] arr, int left, int mid, int right) {
int[] help = new int[right-left+1];
int i = 0;
int p1 = left;
int p2 = mid+1;
int res = 0;
while (p1 <= mid && p2 <= right){
res += arr[p1] < arr[p2] ? (right - p2 + 1) * arr[p1] : 0;
help[i++] = arr[p1] < arr[p2] ? arr[p1++] : arr[p2++];
}
while (p1 <= mid){
help[i++] = arr[p1++];
}
while (p2 <= right){
help[i++] = arr[p2++];
}
for (int j = 0; j < help.length; j++) {
arr[left+j] = help[j];
}
return res;
}
快排3.0
public static void quickSort(int[] arr,int left,int right){
if (left < right){
swap(arr,left+(int)(Math.random()*(right-left+1)),right);
int[] p = partition(arr,left,right);
quickSort(arr,left,p[0]-1);
quickSort(arr,p[1]+1,right );
}
}
private static int[] partition(int[] arr, int left, int right) {
int less = left -1;
int more = right;
while (left < more){
if (arr[left] < arr[right]){
swap(arr, ++less, left++);
}else if (arr[left] > arr[right]){
swap(arr,--more,left);
}else {
left++;
}
}
swap(arr,more,right);
return new int[] {less+1,more};
}
public static void swap(int[] arr,int a,int b){
int temp=0;
temp = arr[a];
arr[a] = arr[b];
arr[b] = temp;
}
堆
//插入一个数
public static void heapinsert(int[] arr, int index){
while (arr[index] > arr[(index-1)/2]){
swap(arr,index,(index-1)/2);
index = (index-1)/2;
}
}
//移除一个数
public static void heapify(int[] arr, int index, int heapsize){
int left = index*2+1;
while (left < heapsize){
int largest = left+1 < heapsize && arr[left+1]>arr[left]
? left+1 : left;
largest = arr[largest] > arr[index] ? largest:index;
if (largest == index){
break;
}
swap(arr,largest,index);
index = largest;
left = index*2+1;
}
}
public static void swap(int[] arr,int a,int b){
int temp=0;
temp = arr[a];
arr[a] = arr[b];
arr[b] = temp;
}
堆排序
//堆排序
public static void heapsort(int[] arr){
if (arr==null || arr.length<2){
return;
}
for (int i = 0; i < arr.length; i++) {
heapinsert(arr,i);
}
int heapsize = arr.length;
swap(arr,0,--heapsize);
while (heapsize > 0){
heapify(arr,0,heapsize);
swap(arr,0,--heapsize);
}
}
小根堆
小根堆就是优先级队列
扩容代价低
//小根堆
public static void sortArrDistanceLessK(int[] arr, int k){
PriorityQueue<Integer> heap = new PriorityQueue<>();
int index = 0;
for (; index <= Math.min(arr.length, k); index++) {
heap.add(arr[index]);
}
int i = 0;
for (; index < arr.length; index++,i++) {
heap.add(arr[index]);
arr[i] = heap.poll();
}
while (!heap.isEmpty()){
arr[i++] = heap.poll();
}
}
比较器
//比较器
public static class AComp implements Comparator<Integer> {
//如果返回负数,第一个数在上面
//如果返回正数,第二个数在上面
//如果返回0,无所谓
// (假设o1<o2,升序o1-o2,降序o2-o1)
@Override
public int compare(Integer o1, Integer o2) {
return o2 - o1;
}
}
用法:
PriorityQueue<Integer> haed = new PriorityQueue<Integer>(new AComp());
桶排序
public static void radixsort(int[] arr){
if (arr == null || arr.length < 2){
return;
}
radixSort(arr,0,arr.length-1,maxbits(arr));
}
public static int maxbits(int[] arr){
int max = Integer.MIN_VALUE;
for (int i = 0; i < arr.length; i++) {
max = Math.max(max,arr[i]);
}
int res = 0;
while (max != 0){
res++;
max /= 10;
}
return res;
}
public static void radixSort(int[] arr, int left, int right, int digit){
final int radix = 10;
int j =0;
int[] bucket = new int[right-left+1];
for (int k = 1; k <= digit; k++) {
int[] count = new int[radix];
for (int l = left; l <= right ; l++) {
j = getDigit(arr[l],k);
count[j]++;
}
for (int l = 1; l < radix ; l++) {
count[l] = count[l]+count[l-1];
}
for (int l = right; l >= left ; l--) {
j = getDigit(arr[l],k);
bucket[count[j]-1] = arr[l];
count[j]--;
}
for (int i = left,o=0; i < right; i++,o++) {
arr[i] = bucket[o];
}
}
}
private static int getDigit(int i, int k) {
return ((i/((int) Math.pow(10,k-1)))%10);
}
}
总结