Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

1. Consider sorting n numbers stored in array A by first finding the smallest el

ID: 3747799 • Letter: 1

Question

1. Consider sorting n numbers stored in array A by first finding the smallest element of A and exchanging it with the element in AŒ1. Then find the second smallest element of A, and exchange it with AŒ2. Continue in this manner for the first n 1elements of A. Write pseudocode for this algorithm, which is known as selection sort. What loop invariant does this algorithm maintain? Why does it need to run for only the first n 1 elements, rather than for all n elements? Give the best-case and worst-case running times of selection sort in ‚-notation.

2. Consider linear search again (see Exercise 2.1-3). How many elements of the in- put sequence need to be checked on the average, assuming that the element being searched for is equally likely to be any element in the array? How about in the worst case? What are the average-case and worst-case running times of linear search in ‚-notation? Justify your answers.

Explanation / Answer

In computer Science, selection sort is an arranging calculation, particularly a set up correlation sort. It has O(n2) time unpredictability, making it wasteful on huge records, and for the most part performs more awful than the comparable inclusion sort. Determination sort is noted for its effortlessness, and it has execution points of interest over more confounded calculations in specific circumstances, especially where assistant memory is constrained.

The calculation separates the info list into two sections: the sublist of things effectively arranged, which is developed from left to comfortable front (left) of the rundown, and the sublist of things staying to be arranged that involve whatever remains of the rundown. At first, the arranged sublist is vacant and the unsorted sublist is the whole info list. The calculation continues by finding the littlest (or biggest, contingent upon arranging request) component in the unsorted sublist, trading (swapping) it with the furthest left unsorted component (placing it in arranged request), and moving the sublist limits one component to one side.

Selection Sort takes after exceptionally straightforward thought.

First it finds the littlest component in the exhibit.

Trade that littlest component with the component at the primary position.

At that point locate the second littlest component and trade that component with the component at the second position.

This procedure proceeds until the point that the entire exhibit is arranged.

Give us a chance to perceive how determination sort functions :

Pseudocode of Selection Sort

SELECTION-SORT(A)
1.     for j 1 to n-1
2.          smallest j
3.           for i j + 1 to n
4.                   if A[ i ] < A[ smallest ]
5.                          smallest i
6.            Exchange A[ j ] A[ smallest ]

Disadvantage of selection sort is : Running time of choice sort depends just somewhat on the measure of request in the record.

2.

linear search or sequential search is a technique for finding an objective incentive inside a rundown. It successively checks every component of the rundown for the objective incentive until the point when a match is found or until the point when every one of the components have been looked.

Direct pursuit keeps running in even under the least favorable conditions straight time and makes at most n examinations, where n is the length of the rundown. On the off chance that every component is similarly liable to be sought, at that point straight pursuit has a normal instance of n/2 correlations, yet the normal case can be influenced if the scan probabilities for every component change. Straight hunt is once in a while viable on the grounds that other pursuit calculations and plans, for example, the parallel inquiry calculation and hash tables, permit essentially quicker scanning for everything except short records.

Pseudocode: FUNCTION linearSearch(list, searchTerm):
                FOR index FROM 0 -> length(list):
                                IF list[index] == searchTerm THEN
                                                RETURN index
                                ENDIF
                ENDLOOP
                RETURN -1
ENDFUNCTION Complexity: Recognize that the single for circle will get to every component in the rundown once in WCS.If every component is gotten to once, at that point as rundown develops the time taken (WCS) develops in direct timeTherefore : O(n)What about best case? Normal case?Encourage examination with other known/obscure calculations