前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >shell脚本编程(快速排序)

shell脚本编程(快速排序)

原创
作者头像
用户7108768
修改2021-11-01 09:16:58
5090
修改2021-11-01 09:16:58
举报
代码语言:javascript
复制
#!/bin/bash  
    #shell脚本编程之快速排序的实现(以最右边为元点的思想)  
    a=(8 5 10 3 2 93 4 1 2 3 40 9 61 8 6 29)  
    #a=(0  0  0  0  1  2  3  3  4  4  8  28  30495  921  43716)  
    temp=  
    buff=  

    #交换函数  
    swap()  
    {  
        buff=${a[$1]}  
        a[$1]=${a[$2]}  
        a[$2]=$buff  
    }  

    fun()  
    {  
        i=$(($1-1))  
        j=$1  
        temp=${a[$2]}  
        if test $1 -ge $2 ;then  
            return 2   
        fi  
            while [ $j -le $2 ];do  
                [ $j -lt $2 ] && while [[ ${a[$j]} -gt $temp ]] ;do   
                 j=$(($j+1))  
                done  
                i=$(($i+1))  
                swap $i $j    
                j=$(($j+1))  
            done  
         fun 0 $(($i-1))  
         fun $(($i+2)) $2  
    }  
    fun 0 15  
    for((i=0;i<16;i++))  
    {  
        echo -n ${a[$i]} " "  
    }  
    echo  
    exit 0  

下面是C++快速排序的对照代码

代码语言:javascript
复制
    #include <iostream>  
    using namespace std;  

    void swap(int *a,int *b)  
    {  
        int temp = *a;  
        *a = *b;  
        *b = temp;  
    }  
    void Grial(int a[],int x,int y)  
    {  
        int i=x-1;  
        int j=x;  
        if(x>=y)return;  
        int temp = a[y];  
        while(j<=y)  
        {  
            while(j<y&&a[j]>a[y])  
                j++;  
            i++;  
            swap(&a[i],&a[j]);  
            j++;      
        }  
        Grial(a,0,i-1);  
        Grial(a,i+1,y);  
    }  
    int main()  
    {  
        int a[]={4,3,921,30495,28,43716,3,4,0,0,2,0,0,1,8,5};  
        Grial(a,0,14);  
        for(int i=0;i<15;i++)  
        {     
            cout<<a[i]<<"  ";  
        }  
        cout<<endl;  
    }  

原创声明:本文系作者授权腾讯云开发者社区发表,未经许可,不得转载。

如有侵权,请联系 cloudcommunity@tencent.com 删除。

原创声明:本文系作者授权腾讯云开发者社区发表,未经许可,不得转载。

如有侵权,请联系 cloudcommunity@tencent.com 删除。

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档