各种排序算法
sort1——直接插入排序
sort2——折半插入排序
sort3——冒泡排序
sort4——快速排序
sort5——堆排序
sort6——归并排序
sortSelect——选择排序
package com.bupt;
public class Solution {
public static int[] sort1(int[] input){
for(int i = 1;i<input.length;i++){
if(input[i]<input[i-1]){
int temp = input[i];
int index = i-1;
while(index>=0&&temp<input[index]){
input[index+1] = input[index];
index = index-1;
}
input[index+1] = temp;
}
}
return input;
}
public static int[] sortSelect(int[] input){
for(int i = 0;i<input.length-1;i++){
int min = i;
for(int j = i+1;j<input.length;j++){
if(input[min]>input[j]){
min = j;
}
}
if(min!=i){
int temp = input[min];
input[min] = input[i];
input[i] = temp;
}
}
return input;
}
public static int[] sort2(int[] input){
for(int i = 1;i<input.length;i++){
int low = 0;
int high = i-1;
int key = input[i];
while(low<=high){
int mid = (low+high)/2;
if(input[mid]<key){
low = low+1;
}else{
high = high-1;
}
}
for(int j = i-1;j>=high+1;j--){
input[j+1] = input[j];
}
input[high+1] = key;
}
return input;
}
public static int[] sort3(int[] input){
for(int i = 0;i<input.length;i++){
for(int j = 0;j<input.length-i-1;j++){
if(input[j]>input[j+1]){
int temp = input[j];
input[j] = input[j+1];
input[j+1] = temp;
}
}
}
return input;
}
public static void sort4(int[] input, int low, int high){
if(low<high){
int index = partition(input, low, high);
sort4(input,low,index-1);
sort4(input,index+1,high);
}
}
public static int partition(int[] input,int low, int high){
int key = input[low];
while(low<high){
while(low<high && input[high]>=key){
high = high-1;
}
input[low] = input[high];
while(low<high && input[low]<=key){
low = low+1;
}
input[high] = input[low];
}
input[low] = key;
return low;
}
public static void sort5(int[] input){
BuildMaxHeap(input,input.length-1);
for(int i = input.length-1;i>0;i--){
System.out.print(input[1]+" ");
input[1] = input[i];
AdjustDown(input,1,i-1);
}
}
public static void BuildMaxHeap(int[] input, int lens){
for(int i = lens/2;i>0;i--){
AdjustDown(input,i,lens);
}
}
public static void AdjustDown(int[] input, int k, int lens){
int temp = input[k];
for(int i = 2*k;i<=lens;i = i*2){
if(i<lens && input[i+1]<input[i]){
i = i+1;
}
if(temp<=input[i]){
break;
}else{
input[k] = input[i];
k = i;
}
}
input[k] = temp;
}
public static void sort6(int[] input, int low, int high){
if(low<high){
int mid = (high+low)/2;
sort6(input,low,mid);
sort6(input,mid+1,high);
merge(input,mid,low,high);
}
}
public static void merge(int[] input, int mid, int low, int high){
int[] temp = new int[input.length+1];
for(int i = low;i<=high;i++){
temp[i] = input[i];
}
int index = low;
int i = low;
int j = mid+1;
while(i<=mid&&j<=high){
if(temp[i]<=temp[j]){
input[index] = temp[i];
i++;
}else{
input[index] = temp[j];
j++;
}
index++;
}
while(j<=high){
input[index] = temp[j];
j++;
}
while(i<=mid){
input[index] = temp[i];
i++;
}
}
public static void main(String[] args){
int[] input = {1,3,2,4,1,2,3,5,2,1,3,5,2,3,5};
sort5(input);
// for(int i = 0;i<input.length;i++){
//
// System.out.print(input[i]+" ");
// }
}
}