Warning: Use of undefined constant USERLEN - assumed 'USERLEN' (this will throw an Error in a future version of PHP) in /home/public/wp-content/themes/irresistible/functions.php on line 31

Warning: Use of undefined constant AMZSRVLINK - assumed 'AMZSRVLINK' (this will throw an Error in a future version of PHP) in /home/public/wp-content/themes/irresistible/functions.php on line 410

Warning: Use of undefined constant AMZSRVTAGS - assumed 'AMZSRVTAGS' (this will throw an Error in a future version of PHP) in /home/public/wp-content/themes/irresistible/functions.php on line 412

Warning: Use of undefined constant AMZIMG160 - assumed 'AMZIMG160' (this will throw an Error in a future version of PHP) in /home/public/wp-content/themes/irresistible/functions.php on line 415

Warning: Use of undefined constant AMZIMG110 - assumed 'AMZIMG110' (this will throw an Error in a future version of PHP) in /home/public/wp-content/themes/irresistible/functions.php on line 417

Warning: Use of undefined constant AMZPPSLINK - assumed 'AMZPPSLINK' (this will throw an Error in a future version of PHP) in /home/public/wp-content/themes/irresistible/functions.php on line 421

Warning: Use of undefined constant AMZAFFLINK - assumed 'AMZAFFLINK' (this will throw an Error in a future version of PHP) in /home/public/wp-content/themes/irresistible/functions.php on line 424
Bubble Sort in Csh/Tcsh

Bubble Sort in Csh/Tcsh


Warning: Use of undefined constant woothemes - assumed 'woothemes' (this will throw an Error in a future version of PHP) in /home/public/wp-content/themes/irresistible/single.php on line 19
// March 17th, 2011 // Posted in Shell // Tagged as , ,

Many commercial 3D applications and Compositing softwares on Unix (Linux, OSX or any other Unix) requires csh/tcsh shell to run. All this time I never go deep into tcsh until few weeks ago. I just learned the scripting side of it and to play around with it I decided to implement Bubble sort in tcsh. This is just a direct-and-naive implementation from the pseudo code on Wikipedia’s Bubble sort page. I’m sure there are rooms for improvements.

#!/bin/tcsh
#
# Bubble sort in csh/tcsh
# @author: Pietra Arumaga (arumaga@gmail.com)

set arr = (15 70 123 55 6 2 4 1)

# index of loop. in tcsh we need to keep track of our own loop
@ s = 1

# size of the array
@ n = $#arr

# check whether we need to swap or not
@ swapped = 1

while( $swapped )
    @ swapped = 0
    foreach i($arr)
        # check whether we're at the last index or not
        if($s == $n) then
            @ s = 1
            continue
        endif

        @ next_s = $s + 1
        set a = $arr[$s]
        set b = $arr[$next_s]
        # echo "loop number is $s"
        
        if($a < $b) then
            # swap it
            set temp = $arr[$next_s]
            @ arr[$next_s] = $arr[$s]
            @ arr[$s] = $temp
            @ swapped = 1
        endif
        @ s = $s + 1
    end
end

echo $arr

Ads by Amazon.com:

Please share this journal with others:


Warning: Use of undefined constant woothemes - assumed 'woothemes' (this will throw an Error in a future version of PHP) in /home/public/wp-content/themes/irresistible/comments.php on line 46

Warning: Use of undefined constant woothemes - assumed 'woothemes' (this will throw an Error in a future version of PHP) in /home/public/wp-content/themes/irresistible/comments.php on line 46
Leave a Reply