Question
please help me !!! write 2 program 1 is bubble sort and merge sort and insert timing logic Program 5 is actually two programs. The
please help me !!! write 2 program 1 is bubble sort and merge sort and insert timing logic
Program 5 is actually two programs. The first, Program 5.1 is a bubble sort (33 points). The second, 5.2 is a merge sort (67 points). You are also going to insert timing logic in to see how the two program compare.
We will discuss the algorithms in class but they are included here as pseudocode:
procedure bubbleSort( A : list of sortable items ) n = length(A) repeat swapped = false for i = 1 to n-1 inclusive do if A[i-1] > A[i] then swap(A[i-1], A[i]) swapped = true end if end for n = n - 1 until not swapped end procedure
Top-down implementation using lists
Pseudocode (Links to an external site.)Links to an external site. for top down merge sort algorithm which recursively divides the input list into smaller sublists until the sublists are trivially sorted, and then merges the sublists while returning up the call chain.
function merge_sort(list m) // Base case. A list of zero or one elements is sorted, by definition. if length of m 1 then return m // Recursive case. First, divide the list into equal-sized sublists // consisting of the first half and second half of the list. // This assumes lists start at index 0. var left := empty list var right := empty list for each x with index i in m do if i < (length of m)/2 then add x to left else add x to right // Recursively sort both sublists. left := merge_sort(left) right := merge_sort(right) // Then merge the now-sorted sublists. return merge(left, right)
In this example, the merge function merges the left and right sublists.
function merge(left, right) var result := empty list while left is not empty and right is not empty do if first(left) first(right) then append first(left) to result left := rest(left) else append first(right) to result right := rest(right) // Either left or right may have elements left; consume them. // (Only one of the following loops will actually be entered.) while left is not empty do append first(left) to result left := rest(left) while right is not empty do append first(right) to result right := rest(right) return result
Rubric
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started