选择冒泡快速插入排序

排序算法实现
package ers;
import java.util.Scanner;
public class Main{

	public  static void main(String[] args)
	{
		Scanner sc = new Scanner(System.in);
		while(sc.hasNext())
		{
			String[] str = sc.nextLine().split(" ");
			int len = str.length;
			int[] arr = new int[len];
			for(int i = 0;i<len;i++)
			{
				arr[i] = Integer.valueOf(str[i]);
			}
			  quickSort(arr, 0, len-1);
			 //selectSort(arr);   //选择排序
			//bubbleSort(arr);    //冒泡排序
			//insertSort(arr);  //插入排序
			for (int i = 0; i < len;i++)
			{
				System.out.print(arr[i]+" ");
			}
		}
	}
	/*冒泡排序*/
	public static void bubbleSort(int[] arr)   
	{
		for (int i = 0; i < arr.length-1; i++) {
			for (int j = 0; j < arr.length-i-1; j++) {
				if(arr[j]>arr[j+1])  //每次排序将最大的排在最后
				{
					int temp = arr[j+1];
					arr[j+1] = arr[j];
					arr[j] = temp;
				}
			}
		}
		
	}
	/*插入排序*/
 	public static void insertSort(int[] arr)
	{
		for (int i = 1; i < arr.length; i++) {
			int h = i;
			int t=arr[i];  //将当前需要被安排插入的元素保存起来,因为此元素为对比标准
			while(h>0)
			{
				if(t>=arr[h-1]) break;
				else {
					arr[h] = arr[h-1];
					h--;
				}	
			}
			arr[h] = t;
		}
	}
 	/*选择排序*/
    public static void selectSort(int[] arr) {  
        int n = arr.length;  
        for (int i = 0; i < n; i++) {  
            int minIndex = i;  
            for (int j = i + 1; j < n; j++) {  
                if (arr[minIndex] > arr[j]) {  
                    minIndex = j;  
                }  
            }  
            if (i != minIndex) {  
                int temp = arr[i];  
                arr[i] = arr[minIndex];  
                arr[minIndex] = temp;  
            }  
        }  
    }
    /*快排的固定切分方式*/
    public static int partition(int []array,int lo,int hi){
        //固定的切分方式
        int key=array[lo];
        while(lo<hi){
            while(array[hi]>=key&&hi>lo){//从后半部分向前扫描
                hi--;
            }
            array[lo]=array[hi];
            while(array[lo]<=key&&hi>lo){//从前半部分向后扫描
                lo++;
            }
            array[hi]=array[lo];
        }
        array[hi]=key;
        return hi;
    }
    /*快速排序*/
    public static void quickSort(int[] array,int lo ,int hi){
        if(lo>=hi){
            return ;
        }
        int index=partition(array,lo,hi);
        quickSort(array,lo,index-1);
        quickSort(array,index+1,hi); 
    }
}

 

转载于:https://www.cnblogs.com/mrdblog/p/7553732.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值