插入排序
public static void insertSort(int[] array){
for(int i = 1;i>array.length - 1;i++){
int j;
int tmp = array[i];
for(j = i -1;j>=0;j--){
if(array[j]>tmp){
array[j+1] = array[j];
}else{
break;
}
}
array[j+1] = tmp;
}
}
希尔排序
public static void shell(int[] array,int gap){
for(int i = gap;i<array.length;i++){
int tmp = array[i];
int j = i-gap;
for(;j>=0;j-=gap){
if(array[j]>gap){
array[j+gap] = array[j];
}else{
break;
}
}
array[j+gap] = tmp;
}
}
public static void shellSort(int[] array){
int[] drr = {5,3,1};
for(int i = 0;i < drr.length;i++){
shell(array,drr[i]);
}
}
选择排序
public static void selectSort(int[] array){
for(int i = 0;i<array.length;i++){
for(intj = i+1;j<array.length;j++){
if(array[i]>array[j]){
int tmp = array[i];
array[i] = array[j];
array[j] = tmp;
}
}
}
}
堆排序
public static void heapSort(int[] array){
for (int i = (array.length-1-1)/2; i >= 0 ; i--) {
adjustDown(array,i,array.length);
}
int end = array.length-1;
while(end>0){
int tmp = array[0];
array[0] = array[end];
array[end] = tmp;
adjustDown(array,0,end);
end--;
}
}
public static void adjustDown(int[] elem,int root, int len) {
int parent = root;
int child = 2 * parent + 1;
while (child < len) {
if (child + 1 < len && elem[child] < elem[child + 1]) {
child = child + 1;
}
if (elem[child] > elem[parent]) {
int t = elem[child];
elem[child] = elem[parent];
elem[parent] = t;
parent = child;
child = 2 * parent + 1;
}else{
break;
}
}
}
冒泡排序
public static void bubbleSort(int[] array){
for(int i = 0;i<array.length-1;i++){
boolean flg = false;
for(int j= 0;j < array.length - 1-i;j++){
if(array[j]>array[j+1]){
int tmp = array[j];
array[j] = array[j+1];
array[j+1] = tmp;
flg = true;
}
}
if(!flg){
break;
}
}
}
快速排序
public static void quickSort(int[] array){
quick(array,0,array.length-1);
}
public static void quick(int[] array,int low,int high) {
if(high-low+1<=100){
insertSort(array,low,high);
return;
}
threeNumMid(array,low,high);
int par = partion(array,low,high);
System.out.println("low"+low);
System.out.println("high"+high);
if(par>low+1) {
quick(array,low,par-1);
}
if(par <high -1) {
quick(array, par + 1, high);
}
}
public static int partion(int[] array,int low,int high){
int i = low;
int j = high;
int p = array[low];
while (i < j) {
while (i < j && array[j] >= p) {
j--;
}
while (i < j && array[i] <= p) {
i++;
}
swap(array, i, j);
}
swap(array, i, low);
return i;
}
public static void insertSort(int[] array,int low,int high) {
for (int i = low+1; i <= high; i++) {
int tmp = array[i];
int j = i - 1;
for (; j >= low; j--) {
if(array[j] > tmp) {
array[j + 1] = array[j];
}else {
break;
}
}
array[j+1] = tmp;
}
}
public static void threeNumMid(int[] array,int low,int high){
int mid = (low+high)/2;
if(array[mid]>array[low]){
swap(array,low,mid);
}
if(array[mid]>array[high]){
swap(array,high,mid);
}
if(array[low]>array[high]){
swap(array,low,high);
}
}
public static void swap(int[] array,int i,int j){
int tmp = array[i];
array[i] = array[j];
array[j] = tmp;
}
public static void quickSort1(int[] array){
Stack<Integer> stack = new Stack<>();
int low = 0;
int high = array.length-1;
int par = partion(array,low,high);
if(par<low+1){
stack.push(low);
stack.push(par - 1);
}
if(par<high-1){
stack.push(par+1);
stack.push(high);
}
while(!stack.empty()){
high = stack.pop();
low = stack.pop();
par = partion(array,low,high);
if(par>low +1){
stack.push(low);
stack.push(par-1);
}
if(par<high-1){
stack.push(par+1);
stack.push(high);
}
}
}
![在这里插入图片描述](https://i-blog.csdnimg.cn/blog_migrate/f08d9198fef9ba7a861288ade6086736.png)
归并排序
public static void mergeSort(int[] array,int low,int high){
if(low >= high || high - low == 1){
return;
}
int mid = (low+high)/2;
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 s1 = low;
int s2 = mid+1;
int len = high-low+1;
int[] arr = new int[len];
int i = 0;
while(s1 <= mid && s2 <= high){
if(array[s1]<=array[s2]){
arr[i++]=array[s1++];
}else{
arr[i++]= array[s2++];
}
}
while(s1<=mid){
arr[i++]=array[s1++];
}
while(s2<=high){
arr[i++] = array[s2++];
}
for (int j = 0; j < arr.length; j++) {
array[low+j] = arr[j];
}
}
public static void merge1(int[] array,int gap) {
int[] tmp = new int[array.length];
int i = 0;
int s1 = 0;
int e1 = s1+gap-1;
int s2 = e1+1;
int e2 = s2+gap-1 >= array.length ? array.length-1 : s2+gap-1;
while (s2 < array.length) {
while (s1 <= e1 && s2 <= e2) {
if(array[s1] <= array[s2]){
tmp[i++] = array[s1++];
}else{
tmp[i++] = array[s2++];
}
}
while(s1<=e1){
tmp[i++] = array[s1++];
}
while(s2<=e2){
tmp[i++] = array[s2++];
}
s1 = e2+1;
e1 = s1+gap-1;
s2 = e1+1;
e2 = s2+gap-1>=array.length?array.length-1:s2+gap-1;
}
while(s1<=array.length-1){
tmp[i++] = array[s2++];
}
for (int j = 0; j < tmp.length; j++) {
array[j] = tmp[j];
}
}