need help on 2 and 5. pleases help asap!!!!!! Given an already sorted list of 6
ID: 3815841 • Letter: N
Question
need help on 2 and 5. pleases help asap!!!!!!
Given an already sorted list of 6 elements, how many comparisons of list elements would each of the following algorithms perform? Explain each answer briefly. selection sort insertion sort mergesort bubble sort Let's say that you want to implement a method findMode (alist) that takes a list of integers and returns the mode of the values in the list - i.e., the value that appears most often in the list. For example, the mode of the list {10, 8, 12, 8, 10, 5, 8} is 8. If two or more values are tied for the mode, the method should return the smallest of the most frequently occurring values. For example, in the list {7, 5, 3, 5, 7, 11, 11}, three values (5, 7, and 11) each appear twice, and no value appears more than twice; the method should return 5 because it is the smallest of the three values that appear twice. One possible implementation of this method is: def findMode(alist): mode = -1 modeFreq =0 # number of times that the mode appears for i in range(len(alist)): freq =1 # number of times that alist [i] appears from # position i to the end of the list for j in range(i + 1, len(alist)): if alist [j] == alist [i]: # how many times is this executed? freq = freq + 1 if freq > modeFreq or (freq == modeFreq and alist [i]Explanation / Answer
Question 2:
1.)
Our list is 2 3 12 13 24 27
a)
Selection sort: So selection sort is a simple comparison based algorithm which contains the elements in the array in two parts one part of the array will be sorted part and another is unsorted. When the algorithm starts at first part smallest element in the array will come at index 0 after swapping previous sitting element at index 0 with our smallest value (or the biggest element, that completely depends on whether you want sorted array in increasing or non-increasing order). and then second part starts, when it finishes the next smallest element will come at index 1 in order to form sorted array this will run for n passes and we will have sorted array in the end.
So for the given example.
After first pass our array will look like,
2 3 12 13 24 27
number of comparisons in first pass= 5
number of comparisons in second pass= 4
number of comparisons in third pass= 3
number of comparisons in fourth pass= 2
number of comparisons in fifth pass= 1
Total= 5+4+3+2+1= 15
insertion sort:
Insertion sort will perform only 5 comparison if the array is already sorted
Merge sort:
Merge sort will perform total 7 comparisons.
Bubble sort:
Bubble sort will do 25 comparisons.
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.