• Java 部分排序算法


    1

    import java.io.*;
    import java.math.*;
    import java.util.*;
    public class Algr{

    public static int array[] = new int[]{3, 9, 1, 2, 8, 11, 34, 21, 0, 23, 22, 10, 14, 2};

    public enum Type{Bubble, Selection, Insert, Shell, Counter, Quick};

    public static void main(String[] args){
    sort(array, Type.Bubble);

    random(array);
    sort(array, Type.Selection);

    random(array);
    sort(array, Type.Insert);

    random(array);
    sort(array, Type.Shell);

    random(array);
    sort(array, Type.Counter);

    random(array);
    sort(array, Type.Quick);
    }

    public static void random(int[] array){
    Random random = new Random();
    int n = array.length;
    for(int i=0; i< n; i++){
    swap(array, i, Math.abs(random.nextInt() % n));
    }
    }


    public static void sort(int[] array, Type type){
    if(array == null || array.length ==0){
    return;
    }
    output(type.name(), array);
    switch(type){
    case Bubble:
    bubble(array);
    break;
    case Selection:
    selection(array);
    break;
    case Insert:
    insert(array);
    break;
    case Shell:
    shell(array);
    break;
    case Counter:
    counterSort(array);
    break;
    case Quick:
    quickSort(array);
    break;
    }
    output(type.name(), array);
    }

    public static void quickSort(int[] array){
    int n = array.length;
    quickSort(array, 0, n-1);
    }

    public static void quickSort(int[] array, int left, int right){
    if(left >= right){
    return;
    }
    final int start = left;
    final int end = right;
    int mid = array[left];
    while(left < right){
    while(array[right] >= mid && left < right){
    right--;
    }
    if(left < right){
    array[left] = array[right];
    }

    while(array[left] < mid && left < right){
    left++;
    }
    if(left < right){
    array[right] = array[left];
    }
    }

    array[left] = mid;

    quickSort(array, start, left -1);
    quickSort(array, left +1, end);

    }

    // stable N 计数排序,需要额外的空间,并且对待排序的数范围有要求
    public static void counterSort(int[] array){
    int n = array.length;
    int max = getMax(array);

    int[] counterArray = new int[max+1];
    for(int i=0; i< n ; i++){
    counterArray[array[i]]++;
    }

    int index = 0;
    for(int i=0; i<= max; i++){
    while(counterArray[i]>0){
    array[index] = i;
    index++;
    counterArray[i]--;
    }
    }
    }

    public static int getMax(int[] array){
    int max = array[0];
    int n = array.length;
    for(int i=1; i<n; i++ ){
    if(array[i] > max){
    max = array[i];
    }
    }
    return max;
    }

    // instable N^1.5 直接插入排序的变种,利用有序化程度越高,排序越快的特性,逐步缩小增量,将数组有序化
    public static void shell(int[] array){
    int n = array.length;
    int d = n/2;
    while(d >= 1){
    shellInsertion(array, d);
    d = d/2;
    }
    }
    public static void shellInsertion(int[] array, int d){
    int n = array.length;
    for(int i=d; i< n; i = i+ d ){
    int k = i;
    int target = array[k];
    while(k >= d && target < array[k-d]){
    array[k] = array[k-d];
    k = k -d;
    }

    array[k] = target;
    }
    }

    // stable N^2 插入算法和冒泡算法的不同之处在于,它是选定值和前面所有值逐个比较。而不是前后两个比较
    public static void insert(int[] array){
    int n = array.length;
    for(int i=1; i< n; i++){
    int k = i;
    int target = array[k];
    while(k >= 1 && target < array[k-1]){
    array[k] = array[k-1];
    k--;
    }
    array[k] = target;
    }
    }

    // instable N^2 选择算法是从无序区中选择一个最小的替换到有序区的指定位置;
    public static void selection(int[] array){
    int n = array.length;
    for(int i=0; i< n; i++){
    int k = i;
    for(int j= i+1; j< n; j++){
    if(array[j] < array[k]){
    k = j;
    }
    }
    swap(array, i, k);
    }
    }

    public static void output(String name, int[] array){
    System.out.print(name +" ");
    for(int i=0; i< array.length; i++){
    System.out.print(array[i] +" ");
    }
    System.out.println();
    }

    // stable N^2
    public static void bubble(int[] array){
    int size = array.length;
    for(int i=0; i< size; i++){
    boolean bSwap = false;
    for(int j=size-1; j>0; j--){
    if(array[j]< array[j-1]){
    swap(array, j, j-1);
    bSwap = true;
    }
    }
    if(bSwap == false){
    break;
    }
    }
    }



    public static void swap(int[] array, int i, int j){
    int temp = array[i];
    array[i] = array[j];
    array[j] = temp;
    }
    }



  • 相关阅读:
    Response.Redirect、Server.Transfer、Server.Execute的区别
    js删除Array指定位置元素方法
    用Json.NET将json字符串反序列化为json匿名对象
    Asp.net中编程方式调用ashx(通过webRequest)
    Server.Transfer中传递ViewState方法
    Ext.Net中Ext.net.DirectMethods无法找到DirectMethod
    IFrame网页加载完成事件
    oracle中grant授权说明
    深度剖析Byteart Retail案例:前言
    深度剖析Byteart Retail案例:仓储(Repository)及其上下文(Repository Context)
  • 原文地址:https://www.cnblogs.com/lipeil/p/5818245.html
Copyright © 2020-2023  润新知