基础算法 排序

用Groovy写的,用来复习复习基本的排序算法

array = [5,8,1,3,6,2,7,4,9,0,11,15,13,12,14,10]

biggerResult = [0,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15]
smallerResult = [15,14,13,12,11,10,9,8,7,6,5,4,3,2,1,0]

length = array.size()

def copyArray(){
    def tmp = []
    array.each{ tmp << it }
    return tmp
}

def checkUpper(def result){
    println result
    assert result == biggerResult
}

def checkLower(def result){
    println result
    assert result == smallerResult
}

def swap(def result, int i, int j){
    def tmp = result[i]
    result[i] = result[j]
    result[j] = tmp
}



def sort = {
   
    bubbleSort()
    
    selectSort()
  
    insertSort()
 
    mergeSort()
 
    quickSort()
 
    heapSort()
    
    shellSort()

}

//调用Sort
sort()


//=============================冒泡排序==================================
def bubbleSort() {
    println "Bubble Sort:"

    //upper
    def result = copyArray()
    bubbleSortUpper(result)
    checkUpper(result)
    
    //lower
    result = copyArray()
    bubbleSortLower(result)
    checkLower(result)
}

def bubbleSortUpper(def result){
    for(int i=0; i<length; i++){
        for(int j=length-1; j>i; j--){
            if(result[j] < result[j-1]){     //==大小排序的不同处-1
                swap(result, j, j-1)
            }
        }
    }
}

def bubbleSortLower(def result){
    for(int i=0; i<length; i++){
        for(int j=length-1; j>i; j--){
            if(result[j] > result[j-1]){
                swap(result, j, j-1)
            }
        }
    }
}
//=============================选择排序==================================
def selectSort(){
    println "Select Sort"
    
    //upper
    def result = copyArray()
    selectSortUpper(result)
    checkUpper(result)
    
    //lower
    result = copyArray()
    selectSortLower(result)
    checkLower(result)
}

def selectSortUpper(def result){
    for(int i=0; i<length; i++){
        def index = i
        def min = result[i]
        
        for(int j=i+1; j<length; j++){
            if(result[j] < min){
                min = result[j]
                index = j
            }
        }
        
        swap(result, i, index)
    }
}

def selectSortLower(def result){
    for(int i=0; i<length; i++){
        def index = i
        def max = result[i]
        
        for(int j=i+1; j<length; j++){
            if(result[j] > max){        //==大小排序的不同处-1
                max = result[j]
                index = j
            }
        }
        
        swap(result, i, index)
    }
}

//=============================插入排序==================================
def insertSort(){
    println "Insert Sort:"
    
    //upper
    def result = copyArray()
    insertSortUpper(result)
    checkUpper(result) 
    
    //lower
    result = copyArray()
    insertSortLower(result)
    checkLower(result)
}

def insertSortUpper(def result){
    for(int i=1; i<length; i++){
    //    if(result[i] < result[i-1]){       //这里的判断其实是不需要的
            def insert = result[i]
            def j
            for(j=i-1; j>=0; j--){
                if(result[j] > insert){      //因为这里有判断
                    result[j+1] = result[j]
                } else {
                    break;
                }
            }
            result[j+1] = insert            //注意这里是 【j+1】
     //   }
    }
}

def insertSortLower(def result){
    for(int i=1; i<length; i++){
    //    if(result[i] > result[i-1]){
            def insert = result[i]
            def j
            for(j=i-1; j>=0; j--){
                if(result[j] < insert){    //==大小排序的不同处-1
                    result[j+1] = result[j]
                } else {
                    break                 //注意这里的break是必须的
                }
            }
            result[j+1] = insert
    //    }
    }
}


//=============================归并排序==================================
def mergeSort(){
    println "Merge Sort:"
    
    //upper
    def result = copyArray()
    mergeSort(result, 0, length-1, "upper")
    checkUpper(result)
    
    //lower
    result = copyArray()
    mergeSort(result, 0, length-1, "lower")
    checkLower(result)
}

def mergeSort(def result, int l, int r, def choose){    //注意这里一定要是 int 类型的参数
    if(l < r){
        int m = (l + r) / 2
        mergeSort(result, l, m, choose)
        mergeSort(result, m+1, r, choose)
        switch(choose){
            case "upper":
                mergeUpper(result, l, m, r)
                break
            case "lower":
                mergeLower(result, l, m, r)
                break
        }      
    }
}

def mergeUpper(def result, int l, int m, int r){
    //l -> m
    //m+1 -> r
    def left = []
    def right = []
    for(int i=l; i<=m; i++)
        left << result[i]
    for(int i=m+1; i<=r; i++)
        right << result[i]
   
    int x=l, i=0, j=0
    while(i<left.size() && j<right.size()){      //注意这里的循环条件,干脆把某一列空的情况单独拎出来
        if(left[i]<=right[j]){
            result[x++] = left[i++]
        }else{
            result[x++] = right[j++]
        }
    }
    if(i < left.size()){
        for(j=i; j<right.size(); j++){
            result[x++] = left[j]          //注意这里应该使用的index
        }
    }
    if(j < right.size()){
        for(i=j; i<left.size(); i++){
            result[x++] = right[i]         //注意这里应该使用的index
        }
    }
}

def mergeLower(def result, int l, int m, int r){
    //l -> m
    //m+1 -> r
    def left = []
    def right = []
    for(int i=l; i<=m; i++)
        left << result[i]
    for(int i=m+1; i<=r; i++)
        right << result[i]
        
    int x=l, i=0, j=0
    while(i<left.size() && j<right.size()){
        if(left[i] >= right[j]){              //==大小排序的不同处-1
            result[x++] = left[i++]
        } else {
            result[x++] = right[j++]
        }
    }
    if(i < left.size){
        for(j=i; j<left.size(); j++){
            result[x++] = left[j]
        }
    }
    if(j < right.size()){
        for(i=j; i<right.size(); i++){
            result[x++] = right[i]
        }
    }
}

//=============================快速排序==================================
def quickSort(){
    println "Quick Sort:"
    
    //upper
    def result = copyArray()
    quickSortUpper(result, 0, length-1)
    checkUpper(result) 
   
    //lower
    result = copyArray()
    quickSortLower(result, 0, length-1)
    checkLower(result) 
}

def quickSortUpper(def result, int left, int right){
    if(left < right){
        int middle = left
        int point = result[right]
        for(int i=left; i<right; i++){         //注意这里的截止条件是 i<right, 没有等于
            if(result[i] < point){             //==大小排序的不同处-1
                swap(result, i, middle)        //从左往右,如果比point大就略过,如果比point小,就将其和middle对换 
                middle++                       //然后middle加1
            }        
        }
        
        swap(result, right, middle)           //最后要将right和middle位置的元素交换
        
        quickSortUpper(result, left, middle-1)
        quickSortUpper(result, middle+1, right)
    }
}

def quickSortLower(def result, int left, int right){
    if(left < right){
        def point = result[left]
        int i = left
        int j = right
        
        while(true){                           //这里使用了不同的策略确定 middle
            while(result[j] < point) j--       //==大小排序的不同处-1,注意,这里不能用等于
            while(result[i] > point) i++       //==大小排序的不同处-2
            
            if(i < j){
                swap(result, i, j) 
            } else {
                assert i == j
                break                    //这里退出的时候一定满足 i==j
            } 
        }
        def middle = i

        quickSortLower(result, left, middle-1)
        quickSortLower(result, middle+1, right)
    }
}

//=============================堆排序==================================
//从0开始:x <- 2x+1, 2x+2; x -> (x-1)/2
def heapSort(){
    println "Heap Sort:"
    
    //upper
    def result = copyArray()
    heapSort(result, "upper")
    checkUpper(result) 
    
    //lower
    result = copyArray()
    heapSort(result, "lower")
    checkLower(result) 
}

def heapSort(def result, def choose){
    for(int i=length/2-1; i>=0; i--){   
        switch(choose){
            case "upper": 
                adjustMaxHeap(result, i, length-1)   //首先从序列中间(最后一个非叶子项)开始整理堆      
                break
            case "lower":
                adjustMinHeap(result, i, length-1)      
                break
        }                
    }
    for(int i=length-1; i>=0; i--){
        swap(result, i, 0)                  //将堆顶添加到后面已经序列化的序列中
       
        switch(choose){
            case "upper": 
                adjustMaxHeap(result, 0, i-1)   //首先从序列中间(最后一个非叶子项)开始整理堆      
                break
            case "lower":
                adjustMinHeap(result, 0, i-1)      
                break
        } 
    }
}

//维护最大堆,其根为root,长度最长到len
//注意,使用前提必须是:root以下的堆结构已经符合要求
def adjustMaxHeap(def result, int root, int len){   
    int child, tmp
    for(tmp=result[root]; 2*root+1<=len; root=child){
        child = 2 * root + 1
        if(child<len && result[child]<result[child+1])       //确定较大的child的位置
            child++
        
        if(result[child] > tmp)                 //如果root比最大的child要小,那就转移位置,继续循环,否则终止
            result[root] = result[child]
        else
            break
    }
    result[root] = tmp
}

//维护最小堆
def adjustMinHeap(def result, int root, int len){   
    int child, tmp
    for(tmp=result[root]; 2*root+1<=len; root=child){
        child = 2 * root + 1
        if(child<len && result[child]>result[child+1])      //==大小堆的不同处-1
            child++
        
        if(result[child] < tmp)                             //==大小堆的不同处-2
            result[root] = result[child]
        else
            break
    }
    result[root] = tmp
}

//=============================希尔排序==================================
//已知的最好步长串行是由Sedgewick提出的 (1, 5, 19, 41, 109,...)
//但是在这里直接使用步长是从 length/2 -> length/2/2 -> ... -> 1 

def shellSort(){
    println "Shell Sort:"
    
    //upper
    def result = copyArray()
    shellSortUpper(result)
    checkUpper(result)
    
    //lower
    result = copyArray()
    shellSortLower(result)
    checkLower(result)
    
}

def shellSortUpper(def result){
    for(int step=length/2; step>0; step/=2){    //定义步长,当 step=1 就成为了插入排序
        for(int i=step; i<length; i++){         //按照步长跳跃进行插入排序
            def insert = result[i]
            def j
            for(j=i-step; j>=0; j-=step){       //往前跳步进行插入排序
                if(result[j] > insert){
                    result[j+step] = result[j]
                } else {
                    break
                }
            }
            result[j+step] = insert
        }    
    }
}

def shellSortLower(def result){
    for(int step=length/2; step>0; step/=2){
        for(int i=step; i<length; i++){        
            def insert = result[i]
            def j
            for(j=i-step; j>=0; j-=step){      
                if(result[j] < insert){             //==大小排序的不同-1 
                    result[j+step] = result[j]
                } else {
                    break
                }
            }
            result[j+step] = insert
        }    
    }
}


  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值