zoukankan      html  css  js  c++  java
  • 选择冒泡快速插入排序

    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); 
        }
    }
    
  • 相关阅读:
    Springboot中enable注解
    java消息队列--ActiveMQ,RabbitMQ入门
    java中的静态和动态代理Proxy
    java源码中的注解
    java中jdbc源码解读
    sql脚本练习
    spring boot中的jave注解学习
    thymeleaf入门和学习
    职场经验:互联网公司有哪些“潜规则”?职场萌新一定要注意了!
    C/C++编程笔记:C语言错误处理方法!如何更好地处理程序的错误?
  • 原文地址:https://www.cnblogs.com/mrdblog/p/7553732.html
Copyright © 2011-2022 走看看