shell脚本编程(快速排序),shell脚本编程排序


[cpp]view plaincopy

#!/bin/bash

#shell脚本编程之快速排序的实现(以最右边为元点的思想)

a=(851032934123409618629)

#a=(00001233448283049592143716)

temp=

buff=

#交换函数

swap()

{

buff=${a[$1]}

a[$1]=${a[$2]}

a[$2]=$buff

}

fun()

{

i=$(($1-1))

j=$1

temp=${a[$2]}

iftest$1-ge$2;then

return2

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

fun0$(($i-1))

fun$(($i+2))$2

}

fun015

for((i=0;i<16;i++))

{

echo-n${a[$i]}""

}

echo

exit0

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

[cpp]view plaincopy

#include<iostream>

usingnamespacestd;

voidswap(int*a,int*b)

{

inttemp=*a;

*a=*b;

*b=temp;

}

voidGrial(inta[],intx,inty)

{

inti=x-1;

intj=x;

if(x>=y)return;

inttemp=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);

}

intmain()

{

inta[]={4,3,921,30495,28,43716,3,4,0,0,2,0,0,1,8,5};

Grial(a,0,14);

for(inti=0;i<15;i++)

{

cout<<a[i]<<"";

}

cout<<endl;

}


相关内容

    暂无相关文章