190 likes | 359 Vues
This document presents a comprehensive overview of the bucket sort algorithm implemented in Scheme. It details the approach of distributing numbers into buckets based on specified intervals, sorting the numbers within each bucket, and then concatenating the sorted buckets for the final result. The implementation incorporates various functional programming techniques such as higher-order functions, vector manipulations, and list processing. An accompanying example demonstrates how numbers are distributed and sorted, illustrating the efficiency and practicality of the method.
E N D
( (lambda (z) (define x (lambda (x) (lambda (y z) (y x)))) ( ( (x (lambda () z))(lambda (z) z) 3 ) ) ) 2)
E1 z: 2 x:L2 L1:p: z b: (define x ...) (...) E2 x:L5 L2:p: x b: (lambda (y z) (y x))) L5:p: - b: z L4:p: z b: z E3 y: L4 z: 3 L3:p: y,z b: (y x) E5 E4 z: L5 GE
Vectors Constructors: (vector v1 v2 v3 . . .) (make-vector size init) Selector: (vector-ref vec place) Mutator: (vector-set! vec place value) Other functions: (vector-length vec)
Example - accumulating (define (accumulate-vec op base vec) (define (helper from to) (if (> from to) base (op (vector-ref vec from) (helper (+ from 1) to)))) (helper 0 (- (vector-length vec) 1)))
Bucket Sort • Problem: Sorting numbers that distribute “uniformly” across a given interval (for example, [0,1) ). • Observation: The number of elements that fall within a sub-interval is proportional to the sub-interval’s size • Idea: • Divide into sub-intervals (buckets) • Throw each number into appropriate bucket • Sort within each bucket (any sorting method) • Adjoin all sorted buckets
Example Sorting: 0.31 0.44 0.72 0.89 0.10 0.05 0.97 0.23 0.56 0.68 If we want ~3 numbers in each bucket, we need 3-4 buckets. Using 3 buckets we have: Bucket 0.000 - 0.333: 0.31 0.10 0.05 0.23 Bucket 0.333 – 0.667: 0.44 0.56 Bucket 0.667 – 1 : 0.72 0.89 0.97 0.68 0.31 0.10 0.05 0.23 0.72 0.89 0.97 0.68 0.44 0.56
0.05 0.10 0.23 0.31 0.68 0.72 0.89 0.97 0.44 0.56 Example Sorting: 0.31 0.44 0.72 0.89 0.10 0.05 0.97 0.23 0.56 0.68 Now sort! Bucket 0.000 - 0.333:0.05 0.10 0.23 0.31 Bucket 0.333 – 0.667:0.44 0.56 Bucket 0.667 – 1 : 0.68 0.72 0.89 0.97
Example Sorting: 0.31 0.44 0.72 0.89 0.10 0.05 0.97 0.23 0.56 0.68 Adjoin sorted buckets: 0.05 0.10 0.23 0.31 0.44 0.56 0.68 0.72 0.89 0.97
Analysis • Observation: If number of buckets is proportional to the number of elements, then the number of elements in each bucket is more or less the same, and bounded by a constant. • Dividing into buckets: O(n) • Sorting one bucket: O(1) • Sorting all buckets: O(n) • Adjoining sorted sequences: O(n) • All together : O(n)
Implementation • A bucket is a list • The set of buckets is a vector • The elements are sorted while inserted into the buckets(very similar so insertion sort): (define (insert x s) (cond ((null? s) (list x)) ((< x (car s)) (cons x s)) (else (cons (car s) (insert x (cdr s))))))
Implementation – for-each • Scheme primitive • Similar to map, without returning a value • Useful for side-effects (define (for-each proc lst) (if (null? lst) ‘done (begin (proc (car lst)) (for-each proc (cdr lst)))))
Implementation – cont. (define (bucket-sort s) (let* ((size 5) (n (ceiling (/ (length s) size))) (buckets (make-vector n null))) (define (insert! x) (let ((bucket (inexact->exact (floor (* n x))))) (vector-set! buckets bucket (insert x (vector-ref buckets bucket))))) (for-each insert! s) (accumulate-vec append null buckets)))
Polish Notation • PostFix Notation: operands before operators • Expressions with binary operators can be written without Parentheses • Apply operators, from left to right, on the last two numbers • 5 7 4 - 2 * + • 5 3 2 * + • 5 6 + • 11 • Stack implementation: • Init: Empty stack • Number: insert! Into stack • Operator: apply on 2 top stack elements and insert! result into stack
Read & Eval • (read) - returns an expression from the user • (eval exp) - evaluates an expression • We will use these functions in: • (perform m) - receives a symbol of an operation, applies it on the two top numbers in the stack, and returns the result to the stack • (iter) - reads an input from the user. If it is a number it is pushed to the stack, if it is an operator we call perform
perform (define (perform m) (let ((arg2 ((stk 'top)))) ((stk 'delete!)) (let ((arg1 ((stk 'top)))) ((stk 'delete!)) ((stk 'insert!) ((eval m) arg1 arg2)))))
iter (define (iter) (let ((in (read))) (if (eq? in 'exit) 'ok (begin (cond ((number? in) ((stk 'insert!) in)) (else (perform in) (display "TOP= ") (display ((stk 'top))) (newline))) (iter)))))
Calc (define (calc) (let ((stk (make-stack))) (define (perform m) ...) (define (iter) ... ) (iter)))
Simulation (calc) 5 7 4 - TOP= 3 2 * TOP= 6 + TOP= 11 exit ok