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++*
Iterative implementation of a recursive algorithm executes faster than recursive implementation because no _____ needs to...
Iterative implementation of a recursive algorithm executes faster than recursive implementation because no _____ needs to be maintained. Select one: a. recursion b. iteration c. recurrence d. stack e. array
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.    
Use Recursive Algorithm to compute 5^23 Mod 8
Use Recursive Algorithm to compute 5^23 Mod 8
      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
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 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.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT