Question

In: Computer Science

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

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) (10 points) This algorithm is known as the selection sort. Write the pseudo code for this algorithm.
b) (10 points) What loop invariant does this algorithm maintain? Note: You do not have to discuss the three individual properties. You need to only provide the loop invariant.
c) (5 points) Why does it need to run for only the first n-1 elements, rather than for
all n elements?
d) (5 points) Give the best-case and worst-case running times of selection sort
in O-notation. Explain your answers. (You will receive no credit for this question without the explanations for your answers.)

Solutions

Expert Solution

a) Pseudocode of Selection Sort

SELECTION-SORT(A):
  for i = 1 to A.length - 1
      min = i
      for j = i + 1 to A.length
          if A[j] < A[min]
              min = j
      temp = A[i]
      A[i] = A[min]
      A[min] = temp

b) LOOP INVARIANTS :

1.At the start of each iteration of the outer for loop, the subarray A[1..i−1] contains the smallest i−1 elements of the array, sorted in nondecreasing order.

2.At the start of each iteration of the inner for loop, A[min] is the smallest number in the subarray A[i..j−1].

c) Why does it need to run for only the first n-1 elements, rather than for
all n elements?

In the final step, the algorithm will be left with two elements to compare. It will store the smaller one in A[n−1] and leave the larger in A[n]. The final one will be the largest element of the array, since all the previous iteration would have sorted all but the last two elements (the outer loop invariant). If we do it n times, we will end up with a redundant step that sorts a single-element subarray.

d) Best and Worst Case Running Times :

  • In the best-case time (the array is sorted), the body of the if is never invoked. The number of operations (counting the comparison as one operation) is:(n-1)(((n+2)/2)+4)
  • In the worst-case time (the array is reversed), the body of the if is invoked on every occasion, which doubles the number of steps in the inner loop, that is:(n−1)(n+6)

So Clearly both take Θ(n2) running times.


Related Solutions

Given an array of numbers, find the index of the smallest array element (the pivot), for...
Given an array of numbers, find the index of the smallest array element (the pivot), for which the sums of all elements to the left and to the right are equal. The array may not be reordered. Example arr=[1,2,3,4,6] the sum of the first three elements, 1+2+3=6. The value of the last element is 6. Using zero based indexing, arr[3]=4 is the pivot between the two subarrays. The index of the pivot is 3. Function Description Complete the function balancedSum...
Consider the problem of sorting an array A[1, ..., n] of integers. We presented an O(n...
Consider the problem of sorting an array A[1, ..., n] of integers. We presented an O(n log n)-time algorithm in class and, also, proved a lower bound of Ω(n log n) for any comparison-based algorithm. 2. Give an efficient sorting algorithm for an array C[1, ..., n] whose elements are taken from the set {1, 2, 3, 4, 5}.
Using Java Write a method that returns the index of the smallest element in an array...
Using Java Write a method that returns the index of the smallest element in an array of integers. If the number of such elements is greater than 1, return the smallest index. Use the following header:   public static int indexOfSmallestElement (double[] array)
. Consider QuickSort on the array A[1:n] and assume that the pivot element x (used to...
. Consider QuickSort on the array A[1:n] and assume that the pivot element x (used to split the array A[lo:hi] into two portions such that all elements in the left portion A[lo:m] are ≤x and all elements in the right portion A[m:hi] are ≥x) is the second element of the array to be split (i. e., A[lo+1]). For a specific value of n at least 13 (your choice), construct an assignment of the numbers 1...n to the n array elements...
Programming Language: JAVA In this assignment you will be sorting an array of numbers using the...
Programming Language: JAVA In this assignment you will be sorting an array of numbers using the bubble sort algorithm. You must be able to sort both integers and doubles, and to do this you must overload a method. Bubble sort work by repeatedly going over the array, and when 2 numbers are found to be out of order, you swap those two numbers. This can be done by looping until there are no more swaps being made, or using a...
Consider the element uniqueness problem: check whether all the elements in a given array of n...
Consider the element uniqueness problem: check whether all the elements in a given array of n elements are distinct answer in pseudo code places
Consider QuickSort on the array A[1:n] and assume that the pivot element x (used to split...
Consider QuickSort on the array A[1:n] and assume that the pivot element x (used to split the array A[lo:hi] into two portions such that all elements in the left portion A[lo:m] are ≤x and all elements in the right portion A[m:hi] are ≥x) is the second element of the array to be split (i. e., A[lo+1]). For a specific value of n at least 13 (your choice), construct an assignment of the numbers 1...n to the n array elements that...
Consider this prime sieve in which an array of numbers of size N is created (i.e....
Consider this prime sieve in which an array of numbers of size N is created (i.e. int nums[10] = {2,3,4,5,6,7,8,9,10,11};) and initialized to contain counting numbers starting with 2. The sieve works by taking each number in the array, and “crossing out” (perhaps via a similar array of Booleans) multiples of that number. So beginning with 2, we would “cross out” 4, 6, 8, 10. Then repeat the process for 3, and so on. Numbers that are already “crossed out”...
Following is the algorithm of Quicksort for sorting an array of integers in ascending order. Partition(numbers,...
Following is the algorithm of Quicksort for sorting an array of integers in ascending order. Partition(numbers, lowIndex, highIndex) {    midpoint = lowIndex + (highIndex - lowIndex) / 2    pivot = numbers[midpoint]    done = false    while (!done) {       while (numbers[lowIndex] < pivot)          lowIndex++       while (pivot < numbers[highIndex])          highIndex--       if (lowIndex >= highIndex) {          done = true       }       else {          temp = numbers[lowIndex]          numbers[lowIndex] = numbers[highIndex]          numbers[highIndex] = temp                 lowIndex++          highIndex--       }    }    return highIndex } Quicksort(numbers, lowIndex, highIndex) {    if (lowIndex...
In Java Find the second largest and second smallest element in a given array. You can...
In Java Find the second largest and second smallest element in a given array. You can hardcode/declare the array in your program.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT