Question

In: Computer Science

Exercises a - b refer to the recursive algorithm SelectionSort (a.) In one part of algorithm...

Exercises a - b refer to the recursive algorithm SelectionSort

(a.) In one part of algorithm SelectionSort, the index of the maximum item in a list must be found. This requires comparisons between list elements. In an n-element (unsorted) list, how many such comparisons are needed in the worst case to find the maximum element? How many such comparisons are needed in the average case?

(b.) Defining the basic operation as the comparison of list elements and ignoring the amount of work required to exchange list elements, write a recurrence relation for the amount of work done by selection sort on an n-element list.

Solutions

Expert Solution

Here is the solution to above problem . Please read the explanation for more information

a) In the selectionSort algorithm the number of comaparision for both worst and average case is O(n2) like bubble sort all elements are compared in the selection Sort in each iteration maximum value is found and is Swapper only once to it's correct location. Hence the total number of swaps is O(n) and comparision is O(n2)

b) In the selection Sort Recurrence relation is T(N) = T(N-1) + N where N-1 is the next recursive call and N is the time taken in each function call

PSUEDOCODE

SelectionSort(Array,Size,Index)
{
// Return when starting and size are same
if (Index == Size)
return;
  
//Find the minimum index value using function
int pivot = FindMinimumValueIndex(Array,Index,Size-1);
  
//Swap the values
if (pivot!= Index)
{
   temp=Array[pivot];
   Array[Index]=Array[pivot];
   Array[pivot]=temp;
   }
  
// Recursive Call
SelectionSort(Array,Size,Index+1);
}


Related Solutions

Write a version of the selection sort algorithm in a function called selectionSort that can be...
Write a version of the selection sort algorithm in a function called selectionSort that can be used to sort a string vector object. Also, write a program to test your algorithm. The program should prompt the user for a series of names. The string zzz should end the input stream. Output the sorted list to the console. *Need answer in C++*
For the following exercises, write a recursive formula for each geometric sequence.
For the following exercises, write a recursive formula for each geometric sequence.
Give a recursive algorithm to solve the following recursive function. f(0) = 0;    f(1) = 1;...
Give a recursive algorithm to solve the following recursive function. f(0) = 0;    f(1) = 1;   f(2) = 4; f(n) = 2 f(n-1) - f(n-2) + 2; n > 2 b) Solve f(n) as a function of n using the methodology used in class for Homogenous Equations. Must solve for the constants as well as the initial conditions are given.
Describe an efficient recursive algorithm for solving the element uniqueness problem
Describe an efficient recursive algorithm for solving the element uniqueness problem, which runs in time that is at most O(n2) in the worst case without using sorting.    
      Design an algorithm for each of the exercises and type it into a word document....
      Design an algorithm for each of the exercises and type it into a word document. Upload the document to PE1 assignment folder. The roots of quadratic equation: Design an algorithm to find the real roots of a quadratic equation of the form ax2+bx+c=0, where a, b, c are all real numbers and a is nonzero
Write a recursive algorithm replace (start) to replace the value of each element of A with...
Write a recursive algorithm replace (start) to replace the value of each element of A with that of the next element in A. A is a singly linked list.
For the following exercises, refer to Figure 15. Each square represents one square unit....x = 2
For the following exercises, refer to Figure 15. Each square represents one square unit. For each value of a, determine which of the three conditions of continuity are satisfied at x = a and which are not. x = 2
For the following exercises, refer to Figure 15. Each square represents one square unit....x = 4
For the following exercises, refer to Figure 15. Each square represents one square unit. For each value of a, determine which of the three conditions of continuity are satisfied at x = a and which are not.x = 4
Write a recursive algorithm in pseudo-code to compute the “power list” of a given list of...
Write a recursive algorithm in pseudo-code to compute the “power list” of a given list of integers. Assume that the List type has members: int List.length returns the length of the list. void List.push(T n) pushes an element n to the front of the list T List.pop() pops an element from the front of the list. List$$ List$$.concat(List$$ other) returns the concatenation of this list with other. Explain in plain English the reasoning behind your algorithm. Power Lists should be...
a. Design a recursive algorithm for computing 3n for any nonnegative integer n that is based...
a. Design a recursive algorithm for computing 3n for any nonnegative integer n that is based on the formula 3n = 3n−1 + 3n−1 + 3n−1 b. Set up a recurrence relation for the number of additions made by the algorithm and solve it. c. Draw a tree of recursive calls for this algorithm and count the number of calls made by the algorithm. d. Is it a good algorithm for solving this problem?
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT