Question

Problem 2. Consider sorting n numbers stored in array A by first finding the smallest element...

Problem 2. 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 − 1 elements of A.

a. Write pseudocode for this algorithm, which is known as selection sort.
b. Why does it need to run for only the first n−1 elements, rather than for all n elements?

c. Give the best-case and worst-case running times of selection sort in Θ-notation.

0 0
Add a comment Improve this question Transcribed image text
Answer #1

a)

for i=0 to n-1:

small= i;

for j=i+1 to n:

if(a[small]<a[j]) small=j;

end if;

end for:

if(small!=i) swap(a[small],a[i])

end if;

end for;

B)

We are running the loop only till n-1. Because in selection sort we are checking for the any small elements presents than the element after it. If we use n instead of n there are no elements in n+1 as we sets j =i+1 and if i=n; So it avoid array out of bound exception.

C) How ever way the array is sorted it the outer loop runs n-1 and inner loope runs [1+2+3+4+........+n-1] times.

So worst case= O(n2 ) =(n2)

best case=O(n2 )=(n2)

Add a comment
Know the answer?
Add Answer to:
Problem 2. Consider sorting n numbers stored in array A by first finding the smallest element...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • 2.1 Searching and Sorting- 5 points each 1. Run Heapsort on the following array: A (7,3, 9, 4, 2,5, 6, 1,8) 2. Run merge sort on the same array. 3. What is the worst case for quick sort? What is...

    2.1 Searching and Sorting- 5 points each 1. Run Heapsort on the following array: A (7,3, 9, 4, 2,5, 6, 1,8) 2. Run merge sort on the same array. 3. What is the worst case for quick sort? What is the worst case time com- plexity for quick sort and why? Explain what modifications we can make to quick sort to make it run faster, and why this helps. 4. Gi pseudocode for an algorithm that will solve the following...

  • 2. Here is a sorting algorithm that I like to use. Given an unsorted list of...

    2. Here is a sorting algorithm that I like to use. Given an unsorted list of size n, let Xx represent the data in location k of the unsorted list. Compare xi to X2 and switch if necessary so that they are in sorted order, smallest first. Compare Xn-1 and Xn and switch if necessary so that they are in sorted order, smallest first. • Compare x3 with its left neighbors, switching if necessary so that the 3 first entries...

  • Devise a heap-sorting-based algorithm for finding the k smallest EVEN elements of an unsorted set of...

    Devise a heap-sorting-based algorithm for finding the k smallest EVEN elements of an unsorted set of n-element array. The corresponding average analytical time-complexity should also be provided. (Show your work; the time complexity for heap-building must be included; it is assumed that 50% of elements are even )

  • Consider the following: Algorithm 1 Smallest (A,q,r) Precondition: A[ q, ... , r] is an array...

    Consider the following: Algorithm 1 Smallest (A,q,r) Precondition: A[ q, ... , r] is an array of integers q ≤ r and q,r ∈ N. Postcondition: Returns the smallest element of A[q, ... , r]. 1: function Smallest (A , q , r) 2: if q = r then 3: return A[q] 4: else 5: mid <--- [q+r/2] 6: return min (Smallest(A, q, mid), Smallest (A, mid + 1, r)) 7: end if 8: end function (a) Write a recurrence...

  • 4) [15 points total (5 points each)] Assume you are given a sorted array A of n numbers, where A is indexed from 1 up t...

    4) [15 points total (5 points each)] Assume you are given a sorted array A of n numbers, where A is indexed from 1 up to n, anda number num which we wish to insert into A, in the proper sorted position. The function Search finds the minimum index i such that num should be inserted into Ali]. It searches the array sequentially until it finds the location i. Another function MakeRoom moves A[i], .., AIn] to Ali+1]...AIn+1] same sort...

  • Sorting Sort the following array using the quick sort algorithm: (4 Marks) a. 12 26 8...

    Sorting Sort the following array using the quick sort algorithm: (4 Marks) a. 12 26 8 9 7 0 4 Pivot selection is defined to be the first element of each sub-list. Show the array before and after each quicksort round (when the array is partitioned after placing the pivot at its correct position). Also, clearly highlight the pivot in each partition b. Consider an unsorted array of integers of size n. Write a Java program to arrange the array...

  • Consider the following sorting algorithm: How to sort an array A Step 1: copy each element...

    Consider the following sorting algorithm: How to sort an array A Step 1: copy each element from A into an AVL tree Step 2: copy each element from the AVL Tree back into the array The algorithm shown above is known as tree sort (because it is traditionally performed with AVL trees). Complete the following implementation of this algorithm. Note that you have two ways to form a loop. You can use an indexing loop, or an iterator loop. Which...

  • 2. Assume you have an input array A with entries numbered 1 through n. One intuitive...

    2. Assume you have an input array A with entries numbered 1 through n. One intuitive way to randomize A is to generate a set of n random values and then sort the array based on these arbitrary mumbers. (a) (10 pts) Write pseudocode for this PERMUTE-BY-SorTInG method (b) (10 pts) Do best-, average, and worst-case analyses for this method. (c) Extra credit (15 pts): The algorithm RANDOMIZE-In-PLACE(A) A. length for i 1 to n 1 n= 2 swap Ali...

  • Searching/sorting tasks and efficiency analysis - Big-oh For each problem given below, do the following: 1....

    Searching/sorting tasks and efficiency analysis - Big-oh For each problem given below, do the following: 1. Create an algorithm in pseudocode to solve the problem. 2. Identify the factors that would influence the running time of your algorithm. For example, if your algorithm is to search an array the factor that influences the running time is the array size. Assign names (such as n) to each factor. 3. Count the operations performed by the algorithm. Express the count as a...

  • Write pseudocode to solve the following problem: You are given an array A[1...n] whose each element...

    Write pseudocode to solve the following problem: You are given an array A[1...n] whose each element is a point of the plane (x,y). You need to sort the array so that points with lower x-coordinates come earlier, but among points with the same x-coordinate, the ones with larger y-coordinate come earlier. So, for example, if the array contains: (1,2), (1,4), (7,10), (11,3), (14,1), (7,2) The output, in this case, should be: (1,4), (1,2), (7,10), (7,2), (11,3), (14,1) Analyze the running...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT