Question

In: Computer Science

Implement the bubble sort in ML using pattern matching and local function definitions. Show that it...

Implement the bubble sort in ML using pattern matching and local function definitions. Show that it works with a few simple examples. A nice description of the bubble sort can be found on Wikipedia.

A helpful function for the bubble sort is:

(* General function that checks if an int list is sorted *)

fun issorted [] = true  

| issorted [x] = true

| issorted (x::y::t) = x <= y andalso issorted(y::t);

Test your solution and show that it works on the following examples:

  • bubbleSort []; → []  

  • bubbleSort [1]; → [1]  

  • bubbleSort [1,2,3]; → [1,2,3]  

  • bubbleSort [3,1,2]; → [1,2,3]  

bubbleSort [5,4,3,2,1]; → [1,2,3,4,5]

Solutions

Expert Solution

Answer:

SourceCode:

fun issorted [] = true | issorted [x] = true | issorted (x::y::t) = x <= y andalso issorted(y::t); 

(* swapping function *)

fun swap [] = [] | swap [x] = [x] | swap (x::y::t) = if (x>y) then y::(swap (x::t))

else x::(swap (y::t));

(*call swap to sort the given list*)

fun bubblesort [] = [] | bubblesort i = if (issorted i) then i else bubblesort (swap i);

val ex1 = bubblesort [1] = [1]
val ex2 = bubblesort [1,2,3] = [1,2,3]
val ex3 = bubblesort [3,1,2] = [1,2,3]
val ex4 = bubblesort [5,4,3,2,1] = [1,2,3,4,5]

The outputs are:

val ex1 = true:bool
val ex2 = true:bool
val ex3 = false:bool
val ex4 = false:bool
Let me know if you have any doubts or if you need anything to change. 

If you are satisfied with the solution, please leave a +ve feedback : ) Let me know for any help with any other questions.

Thank You!
===========================================================================

Related Solutions

Write a program to implement and analyzing the Bubble Sort. a. Write a C++ function for...
Write a program to implement and analyzing the Bubble Sort. a. Write a C++ function for Bubble Sort b. Use a dynamic array of integers in a variable size of n. c. Display the following information: 1) Total counts of comparisons 2) Total counts of shifts / moves / swaps, whichever applies d. Write a main() function to test a best, and an average cases in terms of time efficiency i. Fill out the array with random numbers for an...
Sort the following set of numbers using bubble sort, insertion sort, and selection sort. Show the...
Sort the following set of numbers using bubble sort, insertion sort, and selection sort. Show the process step-by-step, and find the time complexity in Big-O notation for each method. For sorting, use ascending order. 49, 7, 60, 44, 18, 105
1.)recursive merge sort on a list.(Python) 2.)recursive bubble sort using a list without enumerate() function.(python) Show...
1.)recursive merge sort on a list.(Python) 2.)recursive bubble sort using a list without enumerate() function.(python) Show Base case, and recursive case.
1.   Bubble Sort Implement a bubble sort program that will read from a file “pp2.txt” from...
1.   Bubble Sort Implement a bubble sort program that will read from a file “pp2.txt” from the current directory a list of intergers (10 numbers to be exact), and the sort them, and print them to the screen. You can use redirection to read data from a given file through standard input, as opposed to reading the data from the file with the read API (similar to Lab #1). You can assume the input data will only have 10 numbers...
Sort 33, 77, 22, 11, 34, 21, 88, 90, 42 using Bubble sort, show work. Write...
Sort 33, 77, 22, 11, 34, 21, 88, 90, 42 using Bubble sort, show work. Write the algorithm.
Write a MIPS program to implement the Bubble Sort algorithm, that sorts an input list of...
Write a MIPS program to implement the Bubble Sort algorithm, that sorts an input list of integers by repeatedly calling a “swap” subroutine. The original unsorted list of integers should be received from the keyboard input. Your program should first prompt the user “Please input an integer for the number of elements:”. After the user enters a number and return, your program outputs message “Now input each element and then a return:”. For example, if the user enters 8 as...
Bubble Sort Programmatically Implement in C++ the necessary program that does the following: Asks the user...
Bubble Sort Programmatically Implement in C++ the necessary program that does the following: Asks the user and gets at least 5 whole numbers as user input from the keyboard and stores them in an array Displays the numbers from the array on the screen Sorts the numbers in the array using BUBBLE SORT Algorithm Displays the sorted numbers on the screen from the array Save your code file as "yourLastname_Firstname_BubbleSort.cpp" and submit your .cpp file. NOTE: This assignment needs only...
Write and test a C program to implement Bubble Sort. . In your C program, you...
Write and test a C program to implement Bubble Sort. . In your C program, you should do: Implement the array use an integer pointer, get the size of the array from standard input and use the malloc function to allocate the required memory for it. Read the array elements from standard input. Print out the sorted array, and don’t forget to free the memory. Debug your program using Eclipse C/C++ CDT.
1. For each of the following lists, perform a bubble sort, and show the list after...
1. For each of the following lists, perform a bubble sort, and show the list after each exchange: D,B,G,F,A,C,E,H 2. Explain why the bubble sort algorithm does O (n^2) comparisons on an n-element list.
C++ Program (Using 2D array and bubble sort to sort data) A company pays its salespeople...
C++ Program (Using 2D array and bubble sort to sort data) A company pays its salespeople on a commission basis.  The salespeople each receive $250 per week plus 11 percent of their gross sales for the sales period.  For example, a salesperson who grosses $5000 in sales in the period receives $250 plus 11 percent of $5000, or a total of $812.21.  Write a program (using an array of counters) determines for each salesperson their total sales, their salary and additional data points.  There...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT