1.选择排序
核心思想:每次选择出一个最大或者最小的进行交换,时间复杂度 O(N^2) ,空间复杂度 O(0)
public static void main(String[] args){
Integer[] arr ={12,34,21,24,34,65,5,3};
for (int i = 0; i <arr.length-1 ; i++) {
int minIndex=i;
for (int j = i+1; j <arr.length ; j++) {
minIndex= arr[minIndex] < arr[j] ? minIndex : j;
}
//交换方法
unitMothod.swap(arr,i,minIndex);
}
//输出方法
unitMothod.print(arr);
}
2.冒泡排序
一个数组中,每次第i个数和第i+1的数相比较、替换,时间复杂度 O(N^2) ,空间复杂度 O(0)
public static void main(String[] args){
Integer[] arr ={12,34,21,24,34,65,5,3};
for (int i = 0; i < arr.length; i++) {
for (int j = i+1; j <arr.length ; j++) {
if(arr[i] <arr[j])
unitMothod.swap(arr,i,j);
}
}
unitMothod.print(arr);
}
3.插入排序
每个都和前几个比较,向前插入
public static void main(String[] args){
Integer[] arr ={12,34,21,24,34,65,5,3,7,49,52,34};
for (int i = 1; i < arr.length; i++) {
for (int j = i; j >0 ; j--) {
if(arr[j] < arr[j-1])
unitMothod.swap(arr,j,j-1);
}
}
unitMothod.print(arr);
}
4.希尔排序
带间隔的插入排序
public static void main(String[] args){
Integer[] arr ={12,34,21,24,34,65,5,3,7,49,52,34};
int h=0;
while(h<=arr.length/3){
h=h*3+1;
}
for (int gap=h; gap > 0;gap=(gap-1)/3) {
for (int i = gap; i < arr.length ; i++) {
for (int j = i; j >=gap ; j-=gap) {
if(arr[j] < arr[j-gap])
unitMothod.swap(arr, j, j-gap);
}
}
}
unitMothod.print(arr);
}
5.归并排序
public static void main(String[] args){
Integer[] qq={1,3,7,9 ,4,5,8,2,4,6};
Integer[] arr ={12,34,21,24,34,65,5,3,7,49,52,34};
sort(qq,0,qq.length-1);
unitMothod.print(qq);
}
//递归减半
public static void sort(Integer[] arr ,int left,int right){
if(left==right) return ;
int mid=left + (right - left)/2;
sort(arr,left,mid);
sort(arr,mid+1,right);
merge(arr,left,mid+1,right);
}
//合并算法
public static void merge(Integer[] arr ,int leftPor,int rightPtr,int rightBround){
int mid=rightPtr-1;
//首先定义一个和给定的数组等长的一个数组
Integer[] temp=new Integer[rightBround-leftPor+1];
int i=leftPor;
int j=rightPtr;
//定义一个指针 指向help数组
int k=0;
while(i <=mid && j<= rightBround ) {
temp[k++]=arr[i] <= arr[j] ? arr[i++] : arr[j++];
}
//假如左边的有剩余
while(i <= mid) temp[k++] = arr[i++];
//假如右边有剩余
while(j <= rightBround) temp[k++] =arr[j++];
for (int l = 0; l <temp.length ; l++) {
arr[leftPor+l]=temp[l];
}
}
7.快速排序
//快读排序 时间复杂度 O(n * logN) 最差O(N*N) 空间复杂度O(log2N)
public static void main(String[] args){
Integer[] qq ={12,34,21,24,65,5,3,7,49,52,34,6,10,6,0};
Integer[] arr ={7,3,2,8,1,9,5,4,6,10,6,4,6};
sort(arr,0,arr.length-1);
unitMothod.print(arr);
}
public static void sort(Integer[] arr, int leftPor,int rightPor){
if(leftPor > rightPor) return ;
int mid= partition(arr,leftPor,rightPor);
System.out.println("间隔"+mid);
sort(arr,leftPor,mid-1);
sort(arr,mid+1,rightPor);
}
//在数组的给定范围之内,查找以最后一个数字为轴,小于等于排前面,大于排后面。最后将轴放到中心,返回轴位置
public static int partition(Integer[] arr, int leftPor,int rightPor){
int pivot =arr[rightPor];
int left=leftPor;
int right=rightPor-1;
while(left <= right){
while(left <= right && arr[left] <= pivot) left++;
while(left <= right && arr[right] > pivot) right--;
if(left < right) unitMothod.swap(arr,left,right);
unitMothod.print(arr);
}
unitMothod.swap(arr,left,rightPor);
unitMothod.print(arr);
return left;
}
8.桶排序之计数排序
//计数排序 ,数据量 大,但是取值范围小
public static void main(String[] args){
Integer[] arr ={5,3,7,4,6,3,2,7,1,2,5,4,4,4};
sort(arr);
}
public static void sort(Integer[] arr){
//取一个和原数组等长的数组
Integer[] help=new Integer[arr.length];
int max=findMaX(arr);
int [] tongArrar =new int[max+1];
for (int i = 0; i < arr.length; i++) {
tongArrar[arr[i]]++;
}
//计算累加数组
for (int i = 1; i < tongArrar.length; i++) {
tongArrar[i] +=tongArrar[i -1] ;
}
unitMothod.print(tongArrar);
unitMothod.print(arr);
for (int i = arr.length-1; i >=0; i--) {
help[tongArrar[arr[i]]-1] = arr[i];
tongArrar[arr[i]]--;
}
/* int index=0;
for (int i = 0; i < tongArrar.length; i++) {
for (int j = 0; j <tongArrar[i] ; j++) {
help[index++]=i;
}
}
*/
unitMothod.print(help);
}
public static int findMaX(Integer[] arr){
int max=0;
for (int i = 0; i <arr.length ; i++) {
max =arr[i] > max ? arr[i] :max;
}
return max;
}