Sorting algorithm question It is often useful to have data a

Sorting algorithm question!

It is often useful to have data already sorted to perform some operations. In the questions below you need to explain whether an operation could be performed faster if the values are sorted. Make sure to take the time spent to sorting into account. If yes, explain how sorting helps performance in terms of running time. If not, describe a faster alternative algorithm without sorting along with the runtime complexity. Finding the average of a set of integers. Computing the mode of a set of integers (e.g., the value that occurs most often). Computing the median of a set of integers. Checking whether an item at least as large as some specified value exists in the array. Find the maximum value.

Solution

1) to find average of set of integers, i can be either sorting or unsorted integers, willtake same time O(n), since we have to every element to it, either may be case of sorted or unsorted.

2) to find mode, its better we have the sorted data, since with sorted data it\'s enough to scan once the data, to find most repeated value i.e. O(n). If it un sorted we have to scan every time for every value, so O(n*n)

3) As median is nothing but middle value in the list, if it is sorted it takes only constant itme to find, otherwise we have to again sort it, which takes still time.

4) for this sorted data is best as we will scan only once in worst case to find larger than given element i.e. O(n); else we have to scan where the given value exits first, for this it takes O(n), and to find number of elements more than it, it takes still more.

5) If it is sorted data we can return array[length-1], ie.e constant time, else we have to scan the entire data, i.e. O(n)

Sorting algorithm question! It is often useful to have data already sorted to perform some operations. In the questions below you need to explain whether an ope

Get Help Now

Submit a Take Down Notice

Tutor
Tutor: Dr Jack
Most rated tutor on our site