奋斗到天明

# 排序算法

### 冒泡排序

``````private static void pop(int[] array){
for (int i = 0; i < array.length; i++) {
for (int j = 0; j < array.length - i - 1; j++) {
if(array[j] < array[j + 1]){
swap(j + 1, j, array);
}
}
}
}
``````

### 选择排序

``````private static void select(int[] array){
for (int i = 0; i < array.length; i++) {
for (int j = i; j < array.length; j++) {
if(array[i] < array[j]){
swap(j, i, array);
}
}
}
}
``````

### 插入排序

``````private static void insert(int[] array){
for (int i = 0; i < array.length; i++) {
for (int j = i; j > 0; j--) {
if(array[j - 1] < array[j]){
swap(j - 1, j, array);
}
}
}
}
``````

### 归并排序

``````private static void merge(int[] array){
int[] temp = new int[array.length];
mergeCore(0, array.length - 1, temp, array);
}

private static void mergeCore(int left, int right, int[] temp, int[] array) {
if(left == right){
return;
}else{
int mid = (left + right) / 2;

mergeCore(left, mid, temp, array);
mergeCore(mid + 1, right, temp, array);

mergeTogether(left, mid, right, temp, array);
}
}

private static void mergeTogether(int left, int mid, int right, int[] temp, int[] array) {
int curTemp = 0;
int curLeft = left;
int curRight = mid + 1;
int size = right - left;
while(curLeft <= mid && curRight <= right){
if(array[curLeft] >= array[curRight]){
temp[curTemp++] = array[curLeft++];
}else{
temp[curTemp++] = array[curRight++];
}
}

while(curLeft <= mid){
temp[curTemp++] = array[curLeft++];
}

while(curRight <= right){
temp[curTemp++] = array[curRight++];
}

for (int i = 0; i <= size; i++) {
array[left++] = temp[i];
}
}
``````

### 希尔排序

``````private static void shell(int[] array){
int salt = 1;
while(salt < array.length / 3){
salt = salt * 3 + 1;
}

while(salt > 0){
for (int i = salt; i < array.length; i++) {
for (int j = i ; j > salt - 1; j = j - salt) {
int t;
if(array[j - salt] < array[j]){
swap(j - salt, j, array);
}
}

}
salt = (salt - 1)/3;
}
}
``````

### 快速排序

``````private static void fast(int[] array){
fastCort(0, array.length - 1, array);
}

private static int calcMid(int left, int right, int[] array) {
int mid = (left + right)/2;
if(array[left] < array[mid]){
swap(left, mid, array);
}
if(array[left] < array[right]){
swap(left, right, array);
}
if(array[mid] < array[right]){
swap(mid, right, array);
}
swap(mid,right - 1, array);
return 0;
}

private static void fastCort(int left, int right, int[] array) {
if(left < right){
int pivot = array[right];
int border = findBorder(left, right, pivot, array);
fastCort(left, border - 1, array);
fastCort(border + 1, right, array);
}else{
return;
}
}

private static int findBorder(int left, int right, int pivot, int[] array) {
int curLeft = left - 1;
int curRight = right;
while(true){
while(array[++curLeft] > pivot)
;

while(curRight > left && array[--curRight] < pivot)
;

if(curLeft >= curRight){
break;
}else{
swap(curRight, curLeft, array);
}
}
swap(right, curLeft, array);

return curLeft;

}
``````

### 三项取中快速排序

``````private static void fastMid(int[] array){
fastMidCort(0, array.length - 1, array);
}

private static void fastMidCort(int left, int right, int[] array) {
if(right - left < 3){
lessSort(left, right, array);
}else{
int pivot = calcMid(left, right, array);
int border = findBorderMid(left, right, pivot, array);
fastMidCort(left, border - 1, array);
fastMidCort(border + 1, right, array);
}
}

private static int findBorderMid(int left, int right, int pivot, int[] array) {
int curLeft = left;
int curRight = right - 1;
while(true){
while (array[++left] > pivot)
;
while(array[--right] < pivot)
;

if(curLeft >= curRight){
break;
}else {
swap(curLeft, curRight, array);
}
}
swap(curLeft, right - 1, array);
return curLeft;
}

private static void lessSort(int left, int right, int[] array) {
if(right <= left){
return;
}else if(right - left < 2){
if(array[left] < array[right]){
swap(left, right, array);
}
}else{
if(array[left] < array[left + 1]){
swap(left, left + 1, array);
}
if(array[left] < array[right]){
swap(left, right, array);
}
if(array[left + 1] < array[right]){
swap(left + 1, right, array);
}
}
}

private static void swap(int a, int b, int[] array) {
int t;
t = array[b];
array[b] = array[a];
array[a] = t;
}
``````

### 奋斗到天明

2012/03/09
2.3K
1

hardyyao
2018/11/04
0
0
Rxjs实践-各种排序算法排序过程的可视化展示

xiyuyizhi
2017/10/27
0
0

Author: bakari Date: 2012.7.30 排序算法有很多种，每一种在不同的情况下都占有一席之地。关于排序算法我分“经典排序之”系列分别述之。本篇为冒泡排序。 冒泡排序是最古老的排序，我们最早...

chambai
2012/08/11
0
0

Basics Sorting - 基础排序算法 算法复习——排序 算法分析 时间复杂度-执行时间(比较和交换次数) 空间复杂度-所消耗的额外内存空间 使用小堆栈或表 使用链表或指针、数组索引来代表数据 排序...

Corwien
2016/06/17
41
0

Go 示例测试实现原理剖析

15分钟前
0
0
org.apache.commons.lang 时间格式化或者时间字符串转date

16分钟前
0
0
jdk11 HttpClient 爬虫

GOToo
32分钟前
6
0
matlab-自控原理 taylor 泰勒展开 一、二元函数

matlab : R2018a 64bit     OS : Windows 10 x64 typesetting : Markdown    blog : my.oschina.net/zhichengjiu    gitee : gitee.com/zhichengjiu   一元函数的泰勒展开 code c......

43分钟前
2
0
PreparedStatement批量执行sql

ZeroneLove

1
0