CFD Online Logo CFD Online URL
www.cfd-online.com
[Sponsors]
Home > Forums > Fluent UDF and Scheme Programming

MergeSort with a twist - Scheme

Register Blogs Members List Search Today's Posts Mark Forums Read

Reply
 
LinkBack Thread Tools Display Modes
Old   November 9, 2011, 19:48
Default MergeSort with a twist - Scheme
  #1
New Member
 
Daniel Yacouboff
Join Date: Nov 2011
Posts: 3
Rep Power: 5
ydan87 is on a distinguished road
Hello there,
I would like to implement MergeSort numbers given in a list, but following the following algorithm:
1) break the given list into list of lists, with a single number in each list (i.e. from (5 2 6) to ((5) (2) (6)))

2) iterate over the list of lists - merge each 2 adjacent lists and create a list of the results (example: from ((5) (2) (7) (1)) to ((5 2) (7 1)) or from ((5) (2) (7)) to ((5 2) (7)))

3) repeat step 2 until there is only one element in the list, which is the sorted list.

4) return it.

here's my suggestion code, what's wrong with it ?
Code:
(define (fasterMergeSort lst)
  (define (break lst)
    (if (null? lst) ()
        (cons (list (car lst)) (break (cdr lst)))))
  (define (merge lst1 lst2)
    (cond ((null? lst1) (car lst2))
          ((null? lst2) (car lst1))
          ((< (car lst1) (car lst2)) (list (car lst1) (merge (cdr lst1) lst2)))
          (else (list (car lst2) (merge lst1 (cdr lst2))))))
  (define (iter lst)
    (cond ((null? lst) ())
          ((= (length lst) 1) lst)
          (else (cons (merge (car lst) (cadr lst)) (iter (cddr lst))))))
  (cond ((null? lst) 'no-list)
        ((= (length lst) 1) lst)
        (else (let* ((lstBroken (break lst)) (mergeBrokenLst (merge (car lstBroken)(cdr lstBroken))))
                (if (= (length mergeBrokenLst) 1) mergeBrokenLst
                    (iter mergeBrokenLst))))))

Thanks in advance
ydan87 is offline   Reply With Quote

Reply

Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


Similar Threads
Thread Thread Starter Forum Replies Last Post
2nd order upwind scheme (Fluent and CFX) Far FLUENT 0 May 22, 2011 01:50
how to understand high resolution scheme and high order scheme iilw1314 Main CFD Forum 3 September 30, 2010 10:43
Implementation of QUICK scheme Romuald Skoda Main CFD Forum 10 August 3, 2010 12:23
AUSM scheme ? Central Scheme boling Main CFD Forum 5 September 9, 2009 19:06
extrapolation in MUSCL scheme Chandra Main CFD Forum 6 February 14, 2007 12:21


All times are GMT -4. The time now is 09:42.