;; The first three lines of this file were inserted by DrRacket. They record metadata ;; about the language level of this file in a form that our tools can easily process. #reader(lib "htdp-intermediate-lambda-reader.ss" "lang")((modname week8_m_full) (read-case-sensitive #t) (teachpacks ((lib "image.ss" "teachpack" "2htdp"))) (htdp-settings #(#t constructor repeating-decimal #f #t none #f ((lib "image.ss" "teachpack" "2htdp"))))) ; sort: list-of-numbers -> list-of-numbers ; sorts the provided list in ascending order ;(define (mysort numlist) ;(cond [(empty? numlist) empty] ; [(cons? numlist) (insert (first numlist) ; (mysort (rest numlist))) ]) ;) (define (mysort numlist) (foldr insert empty numlist)) (check-expect (mysort (list 2 1)) (list 1 2)) (check-expect (mysort empty) empty) ; insert: number list-of-numbers -> list-of-numbers ; inserts number into list, assuming list is sorted ; (ascending order) (define (insert n numlist) (cond [(empty? numlist) (cons n empty)] [(cons? numlist) (cond [(< n (first numlist)) (cons n numlist)] [(> n (first numlist)) (cons (first numlist) (insert n (rest numlist)))] [(= n (first numlist)) (cons n numlist)])]) ) (check-expect (insert 2 empty) (list 2)) (check-expect (insert 2 (list 3)) (list 2 3)) (check-expect (insert 2 (list 1) ) (list 1 2)) ; myquicksort : list-of-numbers -> list-of-numbers ; sorts the list using quicksort (define (myquicksort l) (cond [(empty? l) empty] [else (append (myquicksort (smaller-than (first l) (rest l))) (list (first l)) (myquicksort (larger-than (first l) (rest l))))])) (define (smaller-than x l) (filter (lambda (y) (< y x)) l)) (define (larger-than x l) (filter (lambda (y) (>= y x)) l)) (check-expect (myquicksort (list 3 3 2 12 11)) (list 2 3 3 11 12)) ; get-random-list : number number -> list-of-numbers ; returns random list of length n, containing numbers between 0 and m-1 inclusive (define (get-random-list m n) (cond [(= n 0) empty] [else (cons (random m) (get-random-list m (- n 1)))])) (define mylist (get-random-list 10000 1000))