1 In the SelectionSorters minimumPositionint from method exp

1. In the SelectionSorter\'s minimumPosition(int from) method, explain why the for loop starts with \"i = from + 1\" instead of \"i = 0\"?

2.Assume a LinearSearcher object named \"searchMe\" has been created with an array of 8 elements containing the numbers 1 though 8 in sorted order. In the LinearSearcher\'s search(int v) method, how many elements in the array have to be compared when \"searchMe.search(9)\" is called? Briefly explain why.

3.Assume a BinarySearcher object named \"searchMe\" has been created with an array of 8 elements containing the numbers 1 through 8 in sorted order. In the BinarySearcher\'s search(int v) method, how many elements in the array have to be compared when \"searchMe.search(9)\" is called? Show each of the numbers in BinarySearcher\'s array \"a\" that are compared to the value 9 in each step into the search() method.

Solution


1)
   minimumPosition(int from):
       for loop starts from index 1, because in selection sort we always find index of minimum element in remaining unsorted portion
       of array. When we start selection sort, we assume a[0]: subarray, with only one elemnt(1st element, 0th index element) is
       sorted and we search miniimum element in remaining part(from 1 to n-1) and then replace a[0] with minimum

2)
   searchMe.search(9) : there are 8 comparison : from first element to last element

3)
   Binary Search
       searchMe.search(9)
       mid = 4 , 1st compares with 4

       then it searchs in 5 to 8
       mid = 6, 2nd comparision, compares with 6

       then it search in 7 to 8
       mid = 7, 3rd comparision, compares with 7

       then it searches in 8 to 8
       mid = 8, 4th comparison, compares with 8

       END

   Total comparison = 4

1. In the SelectionSorter\'s minimumPosition(int from) method, explain why the for loop starts with \

Get Help Now

Submit a Take Down Notice

Tutor
Tutor: Dr Jack
Most rated tutor on our site