<html>
<head>
<style><!--
.hmmessage P
{
margin:0px;
padding:0px
}
body.hmmessage
{
font-size: 10pt;
font-family:Verdana
}
--></style>
</head>
<body class='hmmessage'>
<div><br></div><span class="Apple-style-span" style="font-family: sans-serif; line-height: 19px; ">"the bubble sort seems to have nothing to recommend it, except a catchy name and the fact that it leads to some interesting theoretical problems",</span><br><br>Thinking about it, isn't that enough to recommend it. <div><br></div><div>What would Knuth say about using pictures and animations to teach programming.<br><br>> CC: wookiz@hotmail.com; plt-scheme@list.cs.brown.edu<br>> From: matthias@ccs.neu.edu<br>> To: robby@eecs.northwestern.edu<br>> Subject: Re: [plt-scheme] Do no evil<br>> Date: Mon, 8 Mar 2010 10:47:16 -0500<br>> <br>> <br>> Knuth wrote this up in the 1960s. Anyone who teaches bubble sort<br>> in the 20xy is teaching 1970s computer science. But I think I am<br>> now in some kind of loop with wooks.<br>> <br>> <br>> <br>> <br>> On Mar 8, 2010, at 10:44 AM, Robby Findler wrote:<br>> <br>> > Continuing my contrarianocity: When I was taught the bubble sort, I<br>> > wish that my teacher had taken the time to explain bubble sort's place<br>> > in the pantheon of sorting algorithms (there is essentially no metric<br>> > under which it better than some other sort, with the possible<br>> > exception of ease of implementation in Fortran 70).<br>> ><br>> > Robby<br>> ><br>> > On Mon, Mar 8, 2010 at 9:39 AM, Matthias Felleisen <matthias@ccs.neu.edu <br>> > > wrote:<br>> >><br>> >> I have taken Veer's understanding to derive a relatively short<br>> >> form of bubble sort in the plain Scheme language:<br>> >><br>> >> (define (bsort v)<br>> >> (define (sweep v i clean?)<br>> >> (cond<br>> >> [(= i (- n 1)) (if clean? v (sweep v 0 true))]<br>> >> [else (if (<= (vector-ref v i) (vector-ref v (+ i 1)))<br>> >> (sweep v (+ i 1) clean?)<br>> >> (sweep (swap v i) (+ i 1) false))]))<br>> >> (define n (vector-length v))<br>> >> ;; IN<br>> >> (sweep v 0 true))<br>> >><br>> >> ;; swap: factored out as suggested by Noel,<br>> >> ;; do it with build-vector and weep! bubble<br>> >> ;; demos how bad FP's performance can be :-)<br>> >><br>> >> But I have done so in ASL just to demonstrate that it is feasible.<br>> >><br>> >> You start with the generative recursion idea of bubbling through<br>> >> the array until there are no more out of place items left. Then<br>> >> you're done. Then you 'edit' the program with two observation.<br>> >> If you were in #lang scheme, you'd come up with the above<br>> >><br>> >> The derivation is appended below. -- Matthias<br>> >><br>> >><br>> >><br>> >> ;; [Vec Number] -> [Vec Number]<br>> >> ;; create a sorted version of v<br>> >> ;; effect: modify v<br>> >> (define (bsort.v0 v)<br>> >> (local (;; Result = (list [Vec Number] Boolean)<br>> >><br>> >> ;; [Vec Number] -> [Vec Number]<br>> >> ;; (effect) create a sorted version of v<br>> >> ;; generative recursion: swap-all until nothing to swap <br>> >> anymore<br>> >> ;; termination: there are only a finite number of swaps<br>> >> (define (generative-driver v)<br>> >> (local ((define v+flag (sweep v))<br>> >> (define newv (first v+flag))<br>> >> (define clean? (second v+flag)))<br>> >> (if clean? v (generative-driver v))))<br>> >><br>> >> ;; [Vec Number] -> Result<br>> >> ;; (effect) swap all out of place neighbors throughout <br>> >> vector<br>> >> (define (sweep v)<br>> >> (local (;; [Vec Number] N Boolean -> Result<br>> >> ;; accumulators: [0,i) is bubbled -- clean? no <br>> >> swaps so<br>> >> far<br>> >> (define (sweep v i clean?)<br>> >> (cond<br>> >> [(= i (- (vector-length v) 1)) (list v <br>> >> clean?)]<br>> >> [else (if (<= (vector-ref v i) (vector-ref v <br>> >> (+ i<br>> >> 1)))<br>> >> (sweep v (+ i 1) clean?)<br>> >> (sweep (swap v i) (+ i 1) <br>> >> false))])))<br>> >> (sweep v 0 true))))<br>> >> ;; IN<br>> >> (generative-driver v)))<br>> >><br>> >> ;; fold the geneative-driver into the sweep function<br>> >> ;; observation: the base case packages up the result,<br>> >> ;; which is immediately unfolded and used for another call<br>> >><br>> >> (define (bsort.v1 v)<br>> >> (local (;; [Vec Number] -> [Vec Number]<br>> >> ;; (effect) swap all out of place neighbors throughout <br>> >> vector<br>> >> (define (sweep v)<br>> >> (local (;; [Vec Number] N Boolean -> Result<br>> >> ;; accumulators: [0,i) is bubbled -- clean? no <br>> >> swaps so<br>> >> far<br>> >> (define (sweep v i clean?)<br>> >> (cond<br>> >> [(= i (- (vector-length v) 1))<br>> >> (if clean? v (sweep v 0 true))]<br>> >> [else (if (<= (vector-ref v i) (vector-ref v <br>> >> (+ i<br>> >> 1)))<br>> >> (sweep v (+ i 1) clean?)<br>> >> (sweep (swap v i) (+ i 1) <br>> >> false))])))<br>> >> (sweep v 0 true))))<br>> >> ;; IN<br>> >> (sweep v)))<br>> >><br>> >> ;; eliminate the indirection from 1-ary sweep through 1-ary sweep<br>> >> ;; lift the vector-length computation<br>> >><br>> >> (define (bsort v)<br>> >> (local (;; [Vec Number] N Boolean -> Result<br>> >> ;; accumulators: [0,i) is bubbled -- clean? no swaps so far<br>> >> (define (sweep v i clean?)<br>> >> (cond<br>> >> [(= i (- n 1)) (if clean? v (sweep v 0 true))]<br>> >> [else (if (<= (vector-ref v i) (vector-ref v (+ i 1)))<br>> >> (sweep v (+ i 1) clean?)<br>> >> (sweep (swap v i) (+ i 1) false))]))<br>> >> ;; where<br>> >> (define n (vector-length v)))<br>> >> ;; IN<br>> >> (sweep v 0 true)))<br>> >><br>> >> ;;<br>> >> -----------------------------------------------------------------------------<br>> >> ;; auxiliaries<br>> >><br>> >> ;; [Vec Number] N -> [Vec Number]<br>> >> ;; swap items i and i+1 in v<br>> >> (define (swap v i)<br>> >> (local ((define v@i (vector-ref v i)))<br>> >> (begin<br>> >> (vector-set! v i (vector-ref v (+ i 1)))<br>> >> (vector-set! v (+ i 1) v@i)<br>> >> v)))<br>> >><br>> >> ;;<br>> >> -----------------------------------------------------------------------------<br>> >> ;; tests<br>> >><br>> >> (define (generate-test) (vector 3 1 9 8 5 6 7 0 4 2))<br>> >> (define swapped0 (vector 1 3 9 8 5 6 7 0 4 2))<br>> >> (define swapped2 (vector 3 1 8 9 5 6 7 0 4 2))<br>> >> (define sorted (vector 0 1 2 3 4 5 6 7 8 9))<br>> >><br>> >> (check-expect (swap (generate-test) 0) swapped0)<br>> >> (check-expect (swap (generate-test) 2) swapped2)<br>> >><br>> >> (check-expect (bsort (generate-test)) sorted)<br>> >><br>> >> _________________________________________________<br>> >> For list-related administrative tasks:<br>> >> http://list.cs.brown.edu/mailman/listinfo/plt-scheme<br>> >><br>> <br></div>                                            <br /><hr />Do you have a story that started on Hotmail? <a href='http://clk.atdmt.com/UKM/go/195013117/direct/01/' target='_new'>Tell us now</a></body>
</html>