• 八大基础排序中(直接插入排序,希尔排序,冒泡排序, 快速排序,归并排序,简单选择排序)


    package com.wang.sort;
    
    import java.util.Arrays;
    
    public class Sort {
    	
    	/**
    	 * 1.直接插入排序
    	 * 思想:当前数与前面已经排好顺序的数进行比较,插入到合适的位置
    	 * @param arra
    	 */
    	public void simpleSort(int[] arra) {
    		
    		for (int i = 1; i < arra.length; i++) {
    			int temp = arra[i];
    			int j = i -1;
    			for (; j >= 0 && arra[j] > temp; j--) {
    				arra[j + 1] = arra[j];
    			}
    			arra[j+1] = temp;	
    		}
    		
    	}
    	
    	/**
    	 * 2.冒泡排序
    	 * 思想:相邻的两个数比较,找到最大的数往下沉
    	 * @param args
    	 */
    	public void bubbleSort(int[] arra) {
    	    int temp;
    		for (int i = 0; i < arra.length - 1; i++) {
    			for (int j = 0; j < arra.length - 1 - i; j++) {
    				if (arra[j] > arra[j+1]) {
    					temp = arra[j];
    					arra[j] = arra[j+1];
    					arra[j+1] = temp;
    				}
    			}
    		}
    	}
    	/**
    	 * 找中间值
    	 * @param arra
    	 * @param low
    	 * @param hight
    	 */
    	public static int getMiddle(int[] arra, int low, int hight) {
    		int temp = arra[low];
    		while (low < hight) {
    			while (low < hight && arra[hight] > temp) {
    				hight--;
    			}
    			arra[low] = arra[hight];
    			while(low < hight && arra[low] <= temp) {
    				low++;
    			}
    			arra[hight] = arra[low];
    		}
    		arra[low] = temp;
    		return low;
    	}
    	/**
    	 * 3.快速排序
    	 * 思想:找一个中间值,将比中间值大的放到中间值的右边,小的放到左边,依此进行递归
    	 * @param arra
    	 * @param low
    	 * @param hight
    	 */
    	public static void qickSort(int[] arra, int low, int hight) {
    		if (low > hight) {
    			return;
    		}
    		int middle = getMiddle(arra, low, hight);
    		qickSort(arra, low, middle -1);
    		qickSort(arra, middle + 1, hight);
    	}
    	
    	
    
    	/**
    	 * 归并排序
    	 * @param arra
    	 * @param left
    	 * @param center
    	 * @param right
    	 */
    	
    	public static void merge(int[] arra, int left, int center, int right) {
    		int[] tempArr = new int[right - left + 1];
    		int temp = left;
    		int mid = center + 1;
    		int third = 0;
    		
    		while(temp <= center && mid <= right) {
    			if (arra[temp] < arra[mid]) {
    				tempArr[third++] = arra[temp++];
    			} else {
    				tempArr[third++] = arra[mid++];
    			}			
    		}
    		
    		while(temp <= center) {
    			tempArr[third++] = arra[temp++];
    		}
    		
    		while (mid <= right) {
    			tempArr[third++] = arra[mid++];
    		}
    		
    		
    		for (int i = 0; i < tempArr.length; i++) {
    			arra[i + left] = tempArr[i];
    		}
    	}
    	
    	/**
    	 * 4.归并排序
    	 * @param arra
    	 * @param left
    	 * @param right
    	 */
    	public void mergingSort(int arra[], int left, int right) {
    		if (left < right) {
    			int center = (left + right) / 2;
    			mergingSort(arra, left, center);
    			mergingSort(arra, center + 1, right);
    			merge(arra, left, center, right);
    		}
    		
    	}
    	
    	/**
    	 * 5.希尔排序
    	 * @param args
    	 */
    	public static void shellSort(int[] array) {  
    	    int i;  
    	    int j;  
    	    int temp;  
    	    int gap = 1;  
    	    int len = array.length;  
    	    while (gap < len / 3) { 
    	    	gap = gap * 3 + 1; 
    	    }  
    	    for (; gap > 0; gap /= 3) {  
    	        for (i = gap; i < len; i++) {  
    	            temp = array[i];  
    	            for (j = i - gap; j >= 0 && array[j] > temp; j -= gap) {  
    	                array[j + gap] = array[j];  
    	            }  
    	            array[j + gap] = temp;  
    	        }  
    	    }  
    	    System.out.println(Arrays.toString(array) + " shellSort");  
    	}
    	/**
    	 * 5.希尔排序
    	 * @param arra
    	 */
    	public void shelllSort(int[] arra) {
    		int j = 0;
    		int temp = 0;
    		for (int step = arra.length / 2; step > 0; step /= 2) {
    			for (int i = step; i < arra.length; i++) {
    				temp = arra[i];
    				for (j = i - step; j >= 0 && arra[j] > temp; j -= step) {
    					arra[j + step] = arra[j];
    				}
    				arra[j + step] = temp;
    			}
    			
    		}
    	}
    	
    	
    	/**
    	 * 6.简单选择排序
    	 * 思想:找出最小的放到第一位,第二小的放到第二位,依次类推
    	 * @param args
    	 */
    	public void selectSort(int[] arra) {
    		
    		int position = 0;
    		for (int i = 0; i < arra.length; i++) {
    			int j = i + 1;
    			position = i;
    			int temp = arra[i];
    			for (; j < arra.length; j++) {
    				if (arra[j] < temp) {
    					temp =  arra[j];
    					position = j;
    				}
    			}
    			arra[position] = arra[i];
    			arra[i] = temp;
    		}
    		
    		
    	}
    	
    	
    	/**
    	 * 7.基数排序
    	 * @param args
    	 */
    /** * 基数排序 * * @param data */ public static void radixSortByPerson(int[] data) { // 查找最大值 int max = data[0]; for (int i = 0; i < data.length; i++) { if (data[i] > max) { max = data[i]; } } // 查找最大循环次数 int tims = 0; while (max > 0) { max /= 10; tims++; } // 构建二维数组列表 List<List<Integer>> queueList = new ArrayList<>(); for (int j = 0; j < 10; j++) { List<Integer> queue = new ArrayList<>(); queueList.add(queue); } // 循环次数 for (int m = 0; m < tims; m++) { // 设置对应得位数到对应得列表中 for (int k = 0; k < data.length; k++) { int x = (data[k] % (int) Math.pow(10, m + 1) / (int) Math.pow(10, m)); List<Integer> queue = queueList.get(x); queue.add(data[k]); queueList.set(x, queue); } // 将列表中的数据设置回原数组 int count = 0; for (int n = 0; n < 10; n++) { List<Integer> queue = queueList.get(n); while (queue.size() > 0) { data[count] = queue.get(0); queue.remove(0); count++; } } } }
    /** * 8.堆排序 * @param args */ 
    public static void main(String[] args) {
       Sort sort = new Sort();
       int[] arra = {1, 2, 3, 9, 6, 8, 5, 10};
       // sort.simpleSort(arra);
       // 简单排序
       // sort.bubbleSort(arra);
       // 冒泡排序
       // Sort.qickSort(arra, 0, arra.length - 1);
       // 快速排序 // sort.mergingSort(arra, 0, arra.length - 1);
       sort.shelllSort(arra);
       for (int i = 0; i < arra.length; i++) {
          System.out.println(arra[i]);
       }
    }
    
    


  • 相关阅读:
    ajax发送cookies
    关于几个入口
    关于.net的概念
    codeproject
    Chrome extension
    modern web application
    JavaScript类型转换
    js方法在iframe父子窗口
    javascript book
    MVC Controller
  • 原文地址:https://www.cnblogs.com/wangxiaowang/p/7818788.html
Copyright © 2020-2023  润新知