Answered step by step
Verified Expert Solution
Link Copied!

Question

00
1 Approved Answer

IN SML NO OTHER LANGS IF IN OTHER LANG WILL REPORT SML ONLY MAKE SURE TO USE (op >) AS WELL AS HELPER FUNCTIONS Implement

IN SML NO OTHER LANGS IF IN OTHER LANG WILL REPORT SML ONLY MAKE SURE TO USE (op >) AS WELL AS HELPER FUNCTIONS

Implement insertion_sort: (a * a -> bool) -> a list -> a list that takes a comparison function and then a list, and return a sorted list in the order specified by the comparison function. Insertion sort orders a list from left to right by inserting an element from unsorted portion of the list into the correct position in the sorted portion of the list. For example, insertion_sort (op >) [0, 5, 1, ~4, 9, 11] evaluates to [11,9,5,1,0,~4]. The computation has the following steps:

nil [0, 5, 1, ~4, 9, 11] insert 0 -> [0] [5, 1, ~4, 9, 11] insert 5 -> [5, 0] [1, ~4, 9, 11] insert 1 -> [5, 1, 0] [~4, 9, 11] insert ~4 -> [5, 1, 0, ~4] [9, 11] insert 9 -> [9, 5, 1, 0, ~4] [11] insert 11 -> [11, 9, 5, 1, 0, ~4] nil

You should implement two helper functions as inner functions. The first function insert:a list * a -> a list takes a sorted list l and an element x and insert x into the right position of l so that the list remains sorted after insertion. For example, insert ([5,1,0,~4], 9) should evaluates to [9,5,1,0,~4]. Again the order depends on the comparision function passed to insertion_sort. The second helper function sort:a list * a list -> a list takes a sorted list sofar and an unsorted list l and inserts the elements of l into sofar.

SML ONLY SHOW OUTPUT PLS

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access with AI-Powered Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Entrepreneurship

Authors: Andrew Zacharakis, William D Bygrave

5th Edition

9781119563099

Students also viewed these Programming questions