Question

In: Computer Science

1. Which sorting algorithm can be improved with the following simple optimization? During each pass, you...

1. Which sorting algorithm can be improved with the following simple optimization? During each pass, you keep track of whether a swap was made. At the end of a pass, if no swaps were made, you can assume the list is sorted.

a. Insertion sort

b. Selection sort

c. Bubble Sort

d. None of the above

2. If you want to use Java's built-in Collections.sort() method to sort an ArrayList, what do you have to ensure about the type of object stored in your ArrayList?

a. That it is a primitive

b. That the object class implements Comparable

c. That all the objects in the ArrayList are of the EXACT same class (ie not sub-class objects)

d. That the object class contains a String or int field so that it can be used for sorting

3. Insertion Sort and Selection Sort have what order of worst-case complexity?

a. O(1)

b. O(n)

c. O(n2)

d. O(log n)

Solutions

Expert Solution

1. Which sorting algorithm can be improved with the following simple optimization? During each pass, you keep track of whether a swap was made. At the end of a pass, if no swaps were made, you can assume the list is sorted.

The correct answer is option c, Bubble sort.

To optimize bubble sort, we can introduce a flag to check if elements are getting swapped inside the inner for loop. Hence, in the inner for loop, we check whether swapping of elements is taking place or not, for each time. If for a particular iteration, if there is no swapping, it means the array has been sorted and we can jump out of the for loop, instead of performing all the iterations.

2. If you want to use Java's built-in Collections.sort() method to sort an ArrayList, what do you have to ensure about the type of object stored in your ArrayList?

The correct answer is option b, The object class implements comparable.

For the objects to have a natural order they must implement the interface java.lang.Comparable. The Comparable interface has a method called compareTo(), which gives a negative, 0, a positive if the current value is less than, equal to, or greater than the value we are comparing with, respectively.


3. Insertion Sort and Selection Sort have what order of worst-case complexity?

The correct answer is option c, O(n2).

Worst case happens in insertion sort if the array to be sorted is a reverse-sorted array. For selection sort, best, average and worst case time complexity is O(n2) which is independent of distribution of data.



Related Solutions

Which Sorting algorithm are in place algorithm and which are not? Is it advantageous to design...
Which Sorting algorithm are in place algorithm and which are not? Is it advantageous to design in place sorting algorithm? How this in place term is related to the time complexity and space complexity of the program?
1.) (a) There exists a very popular sorting algorithm called Timsort, the default sorting algorithm in...
1.) (a) There exists a very popular sorting algorithm called Timsort, the default sorting algorithm in both Python and Java. This sort is a combination of two different sorting algorithms: Merge sort, and Insertion sort. Recall that the Big-O of Merge sort is O(nlogn) and the Big-O of Insertion sort is O(n 2 ). What advantage would Timsort have to combine the two algorithms if merge-sort has a better Big-O metric? (b) Consider two algorithms: f(n) and g(n). You run...
How can I determine or explain how BingoSort is a stable sorting algorithm or not?
The following submission rules apply: • For those questions requiring programs, the solutions must be implemented using JavaScript or Java. o Appropriate self-documenting comments in the source code are mandatory, consistent with good programming practices. o Solutions must be provided in plain text so that formatting is not lost. • All answers must be provided in this document. • Sources must be given accurate and complete citations sufficient for the instructor to find and confirm them.How can I determine or...
Implement the following pseudocode in Python Consider the following pseudocode for a sorting algorithm: STOOGESORT(A[0 ......
Implement the following pseudocode in Python Consider the following pseudocode for a sorting algorithm: STOOGESORT(A[0 ... n - 1]) if (n = 2) and A[0] > A[1] swap A[0] and A[1] else if n > 2 m = ceiling(2n/3) STOOGESORT(A[0 ... m - 1]) STOOGESORT(A[n - m ... n - 1]) STOOGESORT(A[0 ... m - 1])
how can the second law efficiency of a simple ideal rankine cycle can be improved
how can the second law efficiency of a simple ideal rankine cycle can be improved
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...
Question #1 (Java) - Which sorting does in-place sorting? - Which sort does the minimum swap...
Question #1 (Java) - Which sorting does in-place sorting? - Which sort does the minimum swap to order ascending/descending manner? Note: I just need a short answer to the question no code.
[after §3.23 easy] Swapping : The following pseudocode describes a simple algorithm which swaps the values...
[after §3.23 easy] Swapping : The following pseudocode describes a simple algorithm which swaps the values in two variables, x and y: 1 print “Enter initial value of x: ” 2 input x 3 print “Enter initial value of y: ” 4 input y 5 set temp to x 6 set x to y 7 set y to temp 8 print “x = ” x“, y = ” y Write a Java Program that implements this algorithm.
Write a program, using any language you want, and any sorting algorithm discussed in this unit...
Write a program, using any language you want, and any sorting algorithm discussed in this unit to sort the following : 243, 1, 4, 6, 234, 33, 674, 32, 3333 Note: Can you write it in quick sort
Please PROVE the following statement: The time complexity of any sorting algorithm that uses only comparisons...
Please PROVE the following statement: The time complexity of any sorting algorithm that uses only comparisons between elements is the following: Ωn log n in the worst case. you need to prove this , if you can please type answer.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT