Question

In: Computer Science

Hello. Please answer the following question in Scheme. Not Python, not any form of C, but...

Hello. Please answer the following question in Scheme. Not Python, not any form of C, but in the language Scheme. If you do not know Scheme, please do not answer the question. I've had to upload it multiple times now. Thank you.

4 Write a recursive function called mergesort that sorts a list by doing the following:
(a) Use split to split the list into two roughly equal-sized partitions.
(b) Recursively sort both partitions.
(c) Use merge to merge the sorted partitions together.
Once again you will need two base cases, one for the empty list and the other for a single-element list.
> (mergesort '())

()

> (mergesort '(9))

(9)

> (mergesort '(8 6 7 5 3 0 9))

(0 3 5 6 7 8 9)

Solutions

Expert Solution

Algorithm Expalined:

;; Merge two sorted lists into a single sorted list
(define (merge list1 list2)
  (cond
    ((null? list1)
     list2)
    ((null? list2)
     list1)
    (else
      (let ((head1 (car list1))
            (head2 (car list2)))
        ; Add the smaller element to the front of the merge list
        (if (<= head1 head2)
          (cons
            head1
            ; Recursively merge
            (merge (cdr list1) list2))
          (cons
            head2
            ; Recursively merge
            (merge list1 (cdr list2))))))))

(define (split-list lst)
  (let ((half (quotient (length lst) 2)))
    ; Create a pair of the first and second halves of the list
    (cons
      (take lst half)
      (drop lst half))))

(define (merge-sort lst)
  (cond
    ((or (null? lst) ; empty list is sorted, so merge up
         (null? (cdr lst))) ; single-element list is sorted, so merge up
     lst)
    (else
      (let ((halves (split-list lst)))
        ; Recursively split until the bottom, then merge back up to sort
        (merge (merge-sort (car halves))
               (merge-sort (cdr halves)))))))

Solution With your example

;; Exp. (mergesort '(8 6 7 5 3 0 9)) ==> (0 3 5 6 7 8 9)
(define (mergesort L)
        (if (null? L) L
                (if (null? (cdr L)) L
                        (merge
                                (mergesort (car (split L)))
                                (mergesort (cadr (split L)))))))

Related Solutions

Hello. Please answer the following question in Scheme. Not Python, not any form of C, but...
Hello. Please answer the following question in Scheme. Not Python, not any form of C, but in the language Scheme. If you do not know Scheme, please do not answer the question. I've had to upload it multiple times now. Thank you. 1.2 Write a function called countdown that takes a positive integer and uses a do expression to display a sequence of numbers counting down to 1, each on its own line, then displaying the string "Blastoff!". > (countdown...
Hello there , can any one please find an answer for this question Q. a single...
Hello there , can any one please find an answer for this question Q. a single lossless dielectric slab of a thickness “d” and permittivity “ε2” is inserted between two regions. the first region is the free space and the second one is a lossless dielectric material of permittivity “ε3”. a uniform plane wave propagating in y- direction and polarized in z-direction is normally incident on the first region. derive an analytical expression for the total reflection coefficient and the...
HELLO, PLEASE ANSWER THE FOLLOWING QUESTION. IT WAS ASSIGNED WITH 2 PARTS. THANKS 42A) A company...
HELLO, PLEASE ANSWER THE FOLLOWING QUESTION. IT WAS ASSIGNED WITH 2 PARTS. THANKS 42A) A company uses the periodic inventory system and had the following activity during the current monthly period. November 1: Beginning inventory 112 Units @ $20 November 5: Purchased 112 Units @ $22 November 8: Purchased 62 Units @ $23 November 16: Sold 174 Units @ $105 November 19: Purchased 75 Units @ $25 Using the weighted-average inventory method, the company's ending inventory would be 42B. A...
Please answer the following question in a paragraph form address the issue of the stakeholder perspective...
Please answer the following question in a paragraph form address the issue of the stakeholder perspective and provide recommendations on how this industry should proceed in regards to self-driving trucks. 1. Apply the Three Levels of Stakeholder framework, from Business Ethics A Managerial Approach. book to this case. Discuss how the stakeholders involved should consider this innovation based on this framework. 2. Include recommendations on how decisions that regard the use of innovations such as the self-driving truck have on...
Hello, Please follow and answer the requirement on these question based on the case company which...
Hello, Please follow and answer the requirement on these question based on the case company which is Samsung. a) What is the organizational structure for Samsung Company (centralized or decentralized)? specific case information. (250 words)
Hello, please answer this question The nurse wants to search for information about a new medication...
Hello, please answer this question The nurse wants to search for information about a new medication that is in the testing phase of production. Which action should the nurse take when conducting a literature search about this medication? Select all that apply. A) Formulate a definition of the problem B) Ask a nurse colleague for help C) Identify the information D) Conduct a search of the most recent literature E) Skimming journal articles during lunch break
hello , could you please answer this question for me in details , my teacher want...
hello , could you please answer this question for me in details , my teacher want more than 400 word ?. it is an essay . 1) Discuss the application of Classical conditioning in reducing Anxiety.!!
Hello, could you please provide a detailed answer to below question? Question: Given all time low...
Hello, could you please provide a detailed answer to below question? Question: Given all time low interest rates, companies should borrow long term and use the borrowed money to takeover other firms. Discuss with suitable reasons, citing empirical evidence, whether you agree or disagree with this statement.                
Answer please the Question below - in WRITTEN FORM ONLY PLEASE 2) In a 1953 study...
Answer please the Question below - in WRITTEN FORM ONLY PLEASE 2) In a 1953 study of stock prices, what did Maurice Kendall find and what does it mean in terms of the EMH? Explain 2.1) Even if the markets are efficient, you can find a job as a professional portfolio manager. How is that possible?
hello, can you please answer this question. thank you Choose 2 signs or symptoms that are...
hello, can you please answer this question. thank you Choose 2 signs or symptoms that are characteristic of Hilda’s respiratory disease and link them to the pathophysiology of her condition (i.e., explain how the pathophysiological changes cause the signs and symptoms you specified). below is given case study for this question. hope this help Hilda Wilde is a 45-year-old woman who was diagnosed with asthma as a child. She recalls her first asthma attack being horrendous; chest tightness, difficulty breathing,...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT