基本排序算法总结


类别排序方法时间复杂度空间复杂度稳定性
最好最坏平均辅助存储 
插入排序直接插入O(n)O(n²)O(n²)O(1)稳定
希尔排序O(n)O(n²)O(n1.3)~O(n1.5)O(1)不稳定
选择排序直接选择O(n²)O(n²)O(n²)O(1)不稳定
堆排序O(nlogn)O(nlogn)O(nlogn)O(1)不稳定
交换排序冒泡O(n)O(n²)O(n²)O(1)稳定
快速O(nlogn)O(n²)O(nlogn)O(nlogn)不稳定
归并排序O(nlogn)O(nlogn)O(nlogn)O(n)稳定

直接插入排序:
	void insertSort(int [] arr){
		int temp,j;
		for(int i=1;i<arr.length;i++){
			temp=arr[i];
			j=i-1;
			while(j>=0&&temp<arr[j]){
				arr[j+1]=arr[j];
				j--;
			}
			arr[j+1]=temp;
		}
	}


希尔排序:
	void shellSort(int [] arr){
		int temp;
		int pointer;
		int gap=arr.length/2;
		while(gap!=0){
			for(int i=gap;i<arr.length;i++){
				temp=arr[i];
				pointer=i-gap;
				while(pointer>=0&&temp<arr[pointer]){
					arr[pointer+gap]=arr[pointer];
					pointer-=gap;
				}
				arr[pointer+gap]=temp;
			}
		gap/=2;
		}
	}


直接选择排序:
void selsectSort(int [] arr){

}
堆排序:
void heapSort(int [] arr){
		buildHeap(arr);
		for(int i=0;i<arr.length;i++){
		swap(arr,0,arr.length-1-i);
		adjustHeap(arr,0,arr.length-1-i);
		}
	}


	void buildHeap(int[] arr){
		if(arr.length<=0)return;
		for(int i=arr.length/2;i>=0;i--){
			adjustHeap(arr,i,arr.length);
		}
	}
		void adjustHeap(int [] arr,int index,int length){
			int left=index*2+1;
			int right=left+1;
			int maxnum=index;
			if(left>length)return;
			if(arr[left]>arr[maxnum])
				maxnum=left;
			if(right<length&&arr[right]>arr[maxnum])
				maxnum=right;
			if(maxnum!=index){
				swap(arr,index,maxnum);
				adjustHeap(arr,maxnum,length);
			}
		}

	void swap(int [] a,int i,int j){
		int temp=a[i];
		a[i]=a[j];
		a[j]=temp;
	}



冒泡排序:
void bubbleSort(int [] a){
		int temp;
		for(int i=0;i<a.length;i++){
			for(int j=0;j<a.length-1-i;j++){
				if(a[j]>a[j+1]){
					temp=a[j+1];
					a[j+1]=a[j];
					a[j]=temp;
				}
			}
		}
	}


快速排序:
	void quickSort(int [] arr,int left,int right){
		if(left>=right)return;
		int index=partition(arr,left,right);
		quickSort(arr,left,index-1);
		quickSort(arr,index+1,right);
	}
	
	int partition(int [] arr,int left,int right){
		int temp=arr[left];
		int i=left,j=right;
		while(i<j){
			while(i<j&&arr[j]>=temp)
				j--;
			swap(arr,i,j);
			while(i<j&&arr[i]<=temp)
				i++;
			swap(arr,i,j);
		}
		return i;
	}

归并排序:
	void mergeSort(int []arr,int low,int high){
		int mid=(high+low)/2;
		if(low<high){
			mergeSort(arr,low,mid);
			mergeSort(arr,mid+1,high);
			merge(arr,low,mid,high);
		}
	}
	void merge(int [] arr,int low,int mid,int high){
		int []temp=new int[high-low+1];
		int i=low;
		int j=mid+1;
		int k=0;
		while(i<mid&&j<high){
			if(arr[i]<arr[j]){
				temp[k]=arr[i];
				k++;
				i++;
			}
			else {
				temp[k]=arr[j];
				k++;
				j++;
			}
		}
		while(i<=mid)
			temp[k++]=arr[i++];
		while(j<=high)
			temp[k++]=arr[j++];
		for(k=0;k<temp.length;k++){
			arr[k+low]=temp[k];
		}
	}

  • 2
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值