快速排序
package test2;
import java.util.Arrays;
public class Server1 {
public static void quickSort(int arr[],int low,int hight) {
if(low<hight) {
int mid=partition(arr,low,hight);
quickSort(arr,low,mid-1);
quickSort(arr,mid+1,hight);
}
}
public static int partition(int arr[],int low,int hight) {
int key=arr[low];
int i=low,j=hight;
if(low<hight) {
while(i<j) {
while(i<j&&arr[j]>=key) {
j--;
}
if(i<j) {
arr[i]=arr[j];
i++;
}
while(i<j&&arr[i]<=key) {
i++;
}
if(i<j) {
arr[j]=arr[i];
j--;
}
}
arr[i]=key;
System.out.println("排序结果"+Arrays.toString(arr));
}
return i;
}
public static void main(String[] args) {
int array[]= {12,65,34,2,56,23,4,5,56,45};
System.out.println("排序前"+Arrays.toString(array));
Server1.quickSort(array,0,array.length-1);
System.out.println("排序后"+Arrays.toString(array));
}
}
插入排序
package test2;
public class Server1 {
public static void main(String[] args) {
int arr[]= {2,65,74,24,5,67,32,53};
insert(arr);
print(arr);
}
public static void insert(int arr[]) {
for (int i=1;i<arr.length;i++) {
for (int j=0;j<i;j++) {
if(arr[j]>arr[i]) {
int temp=arr[i];
for(int k=i;k>j;k--) {
arr[k]=arr[k-1];
}
arr[j]=temp;
}
}
}
}
public static void print(int arr[]) {
for(int n=0;n<arr.length;n++) {
System.out.print(arr[n]+",");
}
}
}