• Java 排序(转) 规格严格


    public class Sort {   
      
        
    public long[] array;   
      
        
    //冒泡排序   
        public void bublingSort(long[] array) {   
            
    int length = array.length;   
            
    long temp;   
            
    for (int i = length; i > 0; i--) {   
                
    for (int j = 0; j < i - 1; j++) {   
                    
    if (array[j] > array[j + 1]) {   
                        temp 
    = array[j + 1];   
                        array[j 
    + 1= array[j];   
                        array[j] 
    = temp;   
                    }   
                }   
            }   
        }   
      
        
    //选择排序   
        public void selectSort(long[] array) {   
            
    int length = array.length;   
            
    int k = 0;   
            
    long temp;   
            
    for (int i = length; i > 0; i--) {   
                k 
    = 0;   
                
    for (int j = 0; j < i - 1; j++) {   
                    
    if (array[k] < array[j + 1]) {   
                        k 
    = j + 1;   
                    }   
                    
    if (j == i - 2) {   
                        temp 
    = array[j + 1];   
                        array[j 
    + 1= array[k];   
                        array[k] 
    = temp;   
                    }   
                }   
            }   
        }   
      
        
    //插入排序   
        public void insertSort(long[] array) {   
            
    int length = array.length;   
            
    int in = 0;   
            
    long temp;   
            
    for (int i = 1; i < length; i++) {   
                
    for (int j = 0; j < i; j++) {   
                    
    if (array[i] < array[j]) {   
                        
    in = j;   
                        temp 
    = array[i];   
                        
    while (j < i) {   
                            array[i] 
    = array[i - 1];   
                            i
    --;   
                        }   
                        array[
    in= temp;   
                    }   
                }   
            }   
        }   
      
        
    //希尔排序   
        public void sortArray(long[] array, int d) {   
            
    int length = array.length;   
            
    long temp;   
            
    for (int i = 0; i < d; i++) {   
                
    for (int j = i + d; j < length; j += d) {   
                    temp 
    = array[j];   
                    
    for (int k = i; k < j; k += d) {   
                        
    if (array[j] < array[k]) {   
                            
    int m = j;   
                            
    while (m > k) {   
                                array[m] 
    = array[m - d];   
                                m 
    = m - d;   
                            }   
                            array[k] 
    = temp;   
                        }   
                    }   
                }   
      
            }   
        }   
      
        
    public void shellSort(long[] array) {   
            
    int length = array.length;   
            
    int d = 1;   
            
    do {   
                d 
    = getIncrement(length);   
                sortArray(array, d);   
            } 
    while (d > 1);   
        }   
      
        
    //快速排序   
        public void recQuickSort(int left, int right) {   
            
    if (right - left <= 0) {   
                
    return;   
            } 
    else {   
                
    long pivot = array[right];   
                
    int partition = partition(left, right, pivot);   
                recQuickSort(left, partition 
    - 1);   
                recQuickSort(partition 
    + 1, right);   
            }   
        }   
      
        
    public int partition(int left, int right, long pivot) {   
            
    int leftPst = left - 1;   
            
    int rightPst = right;   
            
    while (true) {   
                
    while (array[++leftPst] < pivot)   
                    ;   
                
    while (rightPst > left && array[--rightPst] >= pivot)   
                    ;   
                
    if (leftPst < rightPst) {   
                    swap(leftPst, rightPst);   
                } 
    else {   
                    
    break;   
                }   
            }   
            swap(leftPst, right);   
            
    return leftPst;   
        }   
      
        
    public void swap(int left, int right) {   
            
    long temp = array[left];   
            array[left] 
    = array[right];   
            array[right] 
    = temp;   
        }   
      
        
    //归并排序   
        public void mergeSort(long array[], int left, int right) {   
            
    long[] temp = new long[array.length];   
            sort2(array, temp, left, right);   
        }   
      
        
    public void sort2(long[] array, long[] temp, int left, int right) {   
            
    if (left >= right)   
                
    return;   
            
    int mid = (left + right) / 2;   
            sort2(array, temp, left, mid);   
            sort2(array, temp, mid 
    + 1, right);   
      
            
    int leftPst = left;   
            
    int rightPst = mid + 1;   
            
    int position = left;   
            
    while (leftPst <= mid && rightPst <= right) {   
                
    if (array[leftPst] <= array[rightPst]) {   
                    temp[position
    ++= array[leftPst];   
                    leftPst
    ++;   
                } 
    else {   
                    temp[position
    ++= array[rightPst];   
                    rightPst
    ++;   
                }   
            }   
      
            
    while (rightPst <= right) {   
                temp[position
    ++= array[rightPst];   
                rightPst
    ++;   
            }   
            
    while (leftPst <= mid) {   
                temp[position
    ++= array[leftPst];   
                leftPst
    ++;   
            }   
            System.arraycopy(temp, left, array, left, right 
    - left + 1);   
        }   
      
        
    public int getIncrement(int d) {   
            
    int r = 1;   
            r 
    = r / 3 + 1;   
            
    return r;   
        }   
      
        
    public void swap(long a, long b) {   
            
    long temp = a;   
            a 
    = b;   
            b 
    = temp;   
        }   
      
        
    public void display(long[] array) {   
            
    for (int i = 0; i < array.length; i++) {   
                System.
    out.print(array[i] + " ");   
            }   
        }   
      
        
    public static void main(String[] args) {   
            
    long[] array = { 42319445563452435467235623467831790,   
                    
    43 };   
            
    long[] test = {1510 ,1535561004683566300500};   
            Sort sort 
    = new Sort();   
            sort.mergeSort(array, 
    0, array.length - 1);   
            sort.display(array);   
        }   
    }  
  • 相关阅读:
    持续集成
    Centos7配置安装及优化
    vi/vim如何添加或删除多行注释.
    [Ansible实战]-批量配置初始化主机环境.
    VW模板机准备
    一次CPU过载报警处理
    [Ansible实战]-免交互批量管理Zabbix
    [Ansible实战]-ansible初始化mysql数据库
    [Ansible实战]-ansible部署Redis-5.x集群
    远程管理服务(SSH).
  • 原文地址:https://www.cnblogs.com/diyunpeng/p/2076108.html
Copyright © 2020-2023  润新知