Answer(a):
assuming sorting in increasing order-
Algorithm bubbleSort( arr[], n)
{
// Base case: array with one element
if (n == 1)
return;
// One pass of bubble sort. After this pass, the largest element is moved (or bubbled) to end.
//after each recursive call largest element of current array is bubbled to end
// at last one element will be left and all elements right to it will be sorted
for ( i=0; i<n-1; i++) //runs n-1 times
if (arr[i] > arr[i+1]) // if it's out of order then swap
swap(arr[i], arr[i+1]);
// Largest element is fixed,
// recur for remaining array
bubbleSort(arr, n-1);
}
Answer(2):
let's say bubbleSort algo takes T(n) time to sort n elements and T(1)=K (time to sort 1 element is constant)
then recurrence relation is:
T(n) = T(n-1) + C*(n-1) ---------------(1)
C is some constant; C*(n-1) is the time of FOR loop to bubble out largest element; T(n-1) is time to sort rem. n-1 elements.
substituting n by n-1 in eq(1):
T(n-1) = T(n-2) + C*(n-2) ----------------(2)
substituting n by n-2,n-3,n-4,....3,2 in eq(1)
T(n-2) = T(n-3) + C*(n-3) -----------------(3)
T(n-3) = T(n-4) + C*(n-4) -----------------(4)
........................................
T(2) = T(1) + C*(1) ----------------(n-1)
Adding eqns 1 through (n-1), we get:
T(n) = T(1) + C*(1+2+.....+(n-1))
T(n) = K + C*(n*(n-1)/2) = (C/2)*n2 - (C/2)*n + K
i.e. T(n) is O(n2)
hence BubbleSort is O(n2).
07-15 pts) Develop a recursive version of the Bubble Sort algorithm. (a) Write the pseudo code...
2. In class, we discussed the recursive Merge-Sort algorithm. This sorts the whole array by sorting the left side, sorting the right side, and then merging them. Write a similar recursive algorithm that finds the maximum element of an array. (Find the max of the left side, then find the maximum of the right side, then compare the two.) Write pseudo-code for this algorithm. Give the recurrence relation that describes the number of comparisons that your algorithm uses.
1. Algorithm write recurrence relation Help? Consider a version of merge sort in which an array of size n is divided into 5 segments of sizes n/5. Write the recurrence relation for the time complexity and solve it. (Show all your work.)
Q1. [10 pts] Write an algorithm for Bubble sort that sorts array of n integers. Indicate the expected time complexity of the algorithm using the big-O notation. Use the following format for an algorithm pseudocode Function header (.....) Input: Output: Algorithm steps: 1. 2.
The following algorithm (Rosen pg. 363) is a recursive version of linear search, which has access to a global list of distinct integers a_1, a_2,..., a_n. procedure search(i, j, x : i,j, x integers, 1 < i < j < n) if a_i = x then return i else if i = j then 4. return 0 else return search(i + 1, j, x) Prove that this algorithm correctly solves the searching problem when called with parameters i = 1...
When asked to describe an algorithm you are expected to give a clear pseudo-code description of the algorithm 1. (10 pts) Here is a new sorting algorithm NewSort Suppose the original call made is NewSort(A,0,n-1) where A is an array integers. == void NewSort(int A[], int i, int j){ \\ sorts the subarray Aſi..j] if (j i+1) \\when there are only 2 elements if (A[i] > A[j]) swap(A,i,j) \\swaps A[i] and A[j] else { int k = (j-i+1)/3; NewSort(A,i,j-k); \\...
7. Consider the following proposed sorting algorithm supersort (int n, int start, int end, keytype SI1)1 if(n > 1) { if (SIstart] > S[end]) swap SIstart] with Stend]; supersort(n-l, start, end-1, s) supersort (n-1, start+, end, S) a) 3 pts) Give a recurrence relation (with initial condition) that describes the complexity of this sort algorithm b) (4 pts) Solve the recurrence froma) c) (3 pts) Is supersort guaranteed to correctly sort the list of items? Justify your answer. (A formal...
Consider the following: Algorithm 1 Smallest (A,q,r) Precondition: A[ q, ... , r] is an array of integers q ≤ r and q,r ∈ N. Postcondition: Returns the smallest element of A[q, ... , r]. 1: function Smallest (A , q , r) 2: if q = r then 3: return A[q] 4: else 5: mid <--- [q+r/2] 6: return min (Smallest(A, q, mid), Smallest (A, mid + 1, r)) 7: end if 8: end function (a) Write a recurrence...
Write pseudocode for a recursive algorithm for inserting a key into a binary search tree. The following is the definition assumed. right? Write a) pto) You hadt written codes for for a algonithm for inserting a key into a binary seareh tree. The public class BST E extends comparable V prot fected bstNodeE protected class bstNode E V E key V value; ,V> right); bstNode E vright bstNode (E key ngvalue , bstNode«E·V> left . bstNode«E publle void insert (E...
Please write a c++ code If your nameArray variable is sorted using Bubble Sort Algorithm, it will go through a several pass-throughs and characters will be swapped in each pass-through, until nameArray gets completely sorted (alphabetically, in descending order). Show all these stages (Passes and swaps within Passes) for your nameArray. Here is an example, with the name JONPDOE, of how I want you to write these steps: Pass-1: [J O] N P D O E ==> [O J] N...