各种排序算法的实现——Java

各种排序算法

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]+" ");
//    	}
    }
}


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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值