On 03/20/2012 10:09 AM, Per Jessen wrote:
I'm not really much of a bash guru, but in C insertion sort would look like this:
for ( i=0; i<count; i++ ) { for ( j=count-1; j>i; j-- ) { if ( t[i]>t[j] ) { tmp=t[i]; t[i]=t[j]; t[j]=tmp; } } }
Sure you are! It works in bash too :) #!/bin/bash array=( 21 9 18 2 54 83 91 76 15 25 37 ) ## my sort integer array 'msia' msia() { elements=${#array[@]} for((i=0;i<$elements;i++)); do for((j=$((elements-1));j>i;j--)); do [[ ${array[$i]} -gt ${array[$j]} ]] && { _tmp=${array[$i]} array[$i]=${array[$j]} array[$j]=$_tmp } done done unset _tmp unset elements } echo "Original array[]: ${array[@]}" msia echo " Sorted array[]: ${array[@]}" -- David C. Rankin, J.D.,P.E. -- To unsubscribe, e-mail: opensuse+unsubscribe@opensuse.org To contact the owner, e-mail: opensuse+owner@opensuse.org