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); 
        }
    }
    
  • 相关阅读:
    开网页自动进入路由器设置界面的解决办法(腾达路由器)
    SQL基本语句
    驱动调试配置
    【转】snort
    【转】snort.conf分析(中文)
    【转】snort 笔记2 ----- 规则编写
    【转】Snort语法规则说明及实例讲解
    【转】Snort 命令参数详解
    POST教程笔记
    POST教程笔记
  • 原文地址:https://www.cnblogs.com/mrdblog/p/7553732.html
Copyright © 2011-2022 走看看