public class BasicSort {
public static void swap(int[] number,int i,int j){
int temp=number[i];
number[i]=number[j];
number[j]=temp;
}
//选择排序
public static void selectionSort(int[] number){
for(int i=0;i<number.length-1;i++){
for(int j=i+1;j<number.length;j++){
if(number[j]<number[i]){
swap(number,i,j);
}
}
}
}
//插入排序
public static void injectionSort(int[] number){
for(int i=1;i<number.length;i++){
for(int j=i-1;j>=0;j--){
if(number[i]<number[j]){
swap(number,i,j);
}
}
}
}
//冒泡排序
public static void bubbleSort(int[] number){
for(int i=0;i<number.length-1;i++){
for(int j=0;j<number.length-i;j++){
if(number[j+1]<number[j]){
swap(number,j+1,j);
}
}
}
}
//快速排序
public static void quickSort(int[] number,int left,int right){
int key=left;
int i=left;
int j=right;
while(i<j){
while(number[++i]<number[key]&&i<number.length);
while(number[--j]>number[key]&&j>-1);
if(i>=j){
break;
}
swap(number,i,j);
}
swap(number,i,key);
quickSort(number,left,i-1);
quickSort(number,i+1,right);
}
//希尔排序
public static void shellSort(int[] number){
for(int gap=number.length/2;gap>0;gap/=2){
for(int i=gap;i<number.length;i++){
for(int j=i-gap;j>=0;j-=gap){
if(number[i]<number[j]){
swap(number,i,j);
}
}
}
}
}
//归并排序
public static void mergeSort(int[] number1,int[] number2){
int[] number3=new int[number1.length+number2.length];
int i=0,j=0,k=0;
while(i<number1.length&&j<number2.length){
if(number1[i]<number2[j]){
number3[k++]=number1[i++];
}else{
number3[k++]=number2[j++];
}
}
while(i<number1.length){
number3[k++]=number1[i++];
}
while(j<number2.length){
number3[k++]=number2[j++];
}
}
//堆排序
public static void createHeap(int[] number){
int[] heap=new int[number.length];
for(int i=0;i<heap.length;i++){
heap[i]=-1;
}
for(int i=1;i<heap.length;i++){
heap[i]=number[i];
int s=i;
int root=s/2;
while(s>=2&&heap[root]>heap[s]){
swap(heap,root,s);
s=root;
root=s/2;
}
}
for(int i=1;i<number.length;i++){
number[i]=heap[i];
}
int[] temp=new int[number.length+1];
for(int i=1;i<temp.length;i++){
temp[i]=number[i-1];
}
createHeap(temp);
int n=number.length;
while(n>1){
swap(temp,1,n);
n--;
int root=1;
int s=2*root;
while(s<=n){
if(s<n&&temp[s+1]<temp[s]){
s++;
}
if(temp[root]<=temp[s]){
break;
}
swap(temp,root,s);
root=s;
s=2*root;
}
}
for(int i=0;i<number.length;i++){
number[i]=temp[i+1];
}
}
}
public static void swap(int[] number,int i,int j){
int temp=number[i];
number[i]=number[j];
number[j]=temp;
}
//选择排序
public static void selectionSort(int[] number){
for(int i=0;i<number.length-1;i++){
for(int j=i+1;j<number.length;j++){
if(number[j]<number[i]){
swap(number,i,j);
}
}
}
}
//插入排序
public static void injectionSort(int[] number){
for(int i=1;i<number.length;i++){
for(int j=i-1;j>=0;j--){
if(number[i]<number[j]){
swap(number,i,j);
}
}
}
}
//冒泡排序
public static void bubbleSort(int[] number){
for(int i=0;i<number.length-1;i++){
for(int j=0;j<number.length-i;j++){
if(number[j+1]<number[j]){
swap(number,j+1,j);
}
}
}
}
//快速排序
public static void quickSort(int[] number,int left,int right){
int key=left;
int i=left;
int j=right;
while(i<j){
while(number[++i]<number[key]&&i<number.length);
while(number[--j]>number[key]&&j>-1);
if(i>=j){
break;
}
swap(number,i,j);
}
swap(number,i,key);
quickSort(number,left,i-1);
quickSort(number,i+1,right);
}
//希尔排序
public static void shellSort(int[] number){
for(int gap=number.length/2;gap>0;gap/=2){
for(int i=gap;i<number.length;i++){
for(int j=i-gap;j>=0;j-=gap){
if(number[i]<number[j]){
swap(number,i,j);
}
}
}
}
}
//归并排序
public static void mergeSort(int[] number1,int[] number2){
int[] number3=new int[number1.length+number2.length];
int i=0,j=0,k=0;
while(i<number1.length&&j<number2.length){
if(number1[i]<number2[j]){
number3[k++]=number1[i++];
}else{
number3[k++]=number2[j++];
}
}
while(i<number1.length){
number3[k++]=number1[i++];
}
while(j<number2.length){
number3[k++]=number2[j++];
}
}
//堆排序
public static void createHeap(int[] number){
int[] heap=new int[number.length];
for(int i=0;i<heap.length;i++){
heap[i]=-1;
}
for(int i=1;i<heap.length;i++){
heap[i]=number[i];
int s=i;
int root=s/2;
while(s>=2&&heap[root]>heap[s]){
swap(heap,root,s);
s=root;
root=s/2;
}
}
for(int i=1;i<number.length;i++){
number[i]=heap[i];
}
}
int[] temp=new int[number.length+1];
for(int i=1;i<temp.length;i++){
temp[i]=number[i-1];
}
createHeap(temp);
int n=number.length;
while(n>1){
swap(temp,1,n);
n--;
int root=1;
int s=2*root;
while(s<=n){
if(s<n&&temp[s+1]<temp[s]){
s++;
}
if(temp[root]<=temp[s]){
break;
}
swap(temp,root,s);
root=s;
s=2*root;
}
}
for(int i=0;i<number.length;i++){
number[i]=temp[i+1];
}
}
}