Question

Linda creates an algorithm that takes an input sequence S and produces an output sequence T...

Linda creates an algorithm that takes an input sequence S and produces an output sequence T that is a sorting of the n elements of S.
a) In Java, give an algorithm, isSorted, for testing in O(n) time if T is sorted.
b) Explain why the algorithm isSorted is not sufficient to prove a particular output T of Linda's algorithm is a sorting of S.
c) Describe what additional information Linda's algorithm could output so that her algorithm's correctness could be established on any given S and T in O(n) time.

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

a) The following Java code is a simple test to check whether a string array sort function works as expected:

@Test
public void testSort() {
String[] arr = {"c", "a", "b", "c", "d"};
String[] expected = {"a", "b", "c", "c", "d"};
sort(arr); // this is the name of Linda's sorting function
assertArrayEquals(expected, arr);
}

b) The problem with the above function, testSort, is that it does not test whether Linda's sort() function is a stable sorting algorithm or not. A stable sort algorithm is one that retains the order of objects with the same keys. That is, objects with the same key appear in the same order in the output collection as in the input collection. We do not care about this much in simple integer arrays, but this is especially important for map-like data structures.

c) If Linda's algorithm is capable of stable sorting elements, it should be able to output a collection with mapped values where the order of the objects with same values in the output list is the same as that in the input list. If we know that this is possible, we can test this using a simple assertion that compares the two collections (input and output) in O(n) time, where n is the size of the input collection.

All the best!

Add a comment
Know the answer?
Add Answer to:
Linda creates an algorithm that takes an input sequence S and produces an output sequence T...
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
  • 3. Give an efficient algorithm that takes as input a directed graph G-(V,E) with edges labeled wi...

    Please show your work 3. Give an efficient algorithm that takes as input a directed graph G-(V,E) with edges labeled with either 0 or 1, and vertices s and t that ouputs TRUE if and only if there is a path (not necessarily simple) that goes from s to t such that the binary sequence of edges in the path avoids the substring "11" and outputs FALSE otherwise. (For example, the string 10100010 avoids 11 but the string 00101101110 does...

  • Suppose that an algorithm takes 30 seconds for an input of 224 elements (with some particular,...

    Suppose that an algorithm takes 30 seconds for an input of 224 elements (with some particular, but unspecified speed in instructions per second). Estimate how long the same algorithm, running on the same hardware, would take if the input contained 230 elements, and that the algorithm's complexity function is: Big theta not Big O a) Big theta(N) b) Big theta (log N) c) Big theta (N log N) d) Big theta(N^2) Assume that the low-order terms of the complexity functions...

  • 1) Design a greedy algorithm that solves the problem; describe your algorithm with clear pseudocode; and...

    1) Design a greedy algorithm that solves the problem; describe your algorithm with clear pseudocode; and prove the time efficiency class of your algorithm: If x, y are two adjacent elements in a sequence, with x before y, we say that the pair x, y is in order when x <= y and the pair is out of order when x > y. For example, in the string “BEGGAR” the pair G, A are out of order, but all the...

  • the two problems are related. Please explain your answer in full detail Problem 1: On input...

    the two problems are related. Please explain your answer in full detail Problem 1: On input a Binary Search Tree T show how to output an array that contains all the elements in T in sorted order. What's the running time of your algorithm? Does it perform any comparisons? Problem 2: Your classmate claims that they have an algorithm that on input n elements, constructs a Binary Search Tree T with those n elements using only O(n) comparisons. Can you...

  • In this project, you will work on the algorithm (discussed in Module 1) to determine the...

    In this project, you will work on the algorithm (discussed in Module 1) to determine the length of the longest sub sequence of consecutive integers in an array You will implement the algorithm using Hash tables. You are provided with sample code (in C++ and Java) representing the linked list-based implementation of Hash tables (as an array of Linked Lists). You could go through the code to understand the implementation of a Hash table and the functions that can be...

  • Given image Sorting Algorithm handout – the column on the far left contains an input array...

    Given image Sorting Algorithm handout – the column on the far left contains an input array of 24 strings to be sorted or shuffled; the column on the far right contains the strings in sorted order. Each of the other numbered columns contains the contents at some intermediate step during one of the 6 sorting algorithms we have studied:           Input SKIP SKIP 1      2      3      4      5      6      Sorted 0        left     hash   left     flow   byte   byte   lifo     byte   byte  ...

  • I need the report like this (idea) *Sorting Algorithms: A sorting algorithm is an algorithm that...

    I need the report like this (idea) *Sorting Algorithms: A sorting algorithm is an algorithm that puts elements of a list in a certain order. The most-used orders are numerical order and lexicographical order. Efficient sorting is important for optimizing the use of other algorithms (such as search and merge algorithms) which require input data to be in sorted lists; it is also often useful for canonical zing data and for producing human-readable output. More formally, the output must satisfy...

  • Puodace a char showing the number of moves required to solve the Towers of Hanoi puzle using t 30...

    Puodace a char showing the number of moves required to solve the Towers of Hanoi puzle using t 30. What is an execution frame? What is an activation record? What is contained in it? 31. Write a recursive Java method that computes the factorial of a number 2. Linear search may require up to comparisons while binary search will only require roughly comparisons 33. Sort sorts a list of values by repetitively putting a particular value into its final, sorted,...

  • The INORDER traversal output of a binary tree is U,N,I,V,E,R,S,I,T,Y and the POSTORDER traversal output of the same tree is N,U,V,R,E,T,I,S,I,Y

     a. The INORDER traversal output of a binary tree is U,N,I,V,E,R,S,I,T,Y and the POSTORDER traversal output of the same tree is N,U,V,R,E,T,I,S,I,Y. Construct the tree and determine the output of the PREORDER traversal output.   b. One main difference between a binary search tree (BST) and an AVL (Adelson-Velski and Landis) tree is that an AVL tree has a balance condition, that is, for every node in the AVL tree, the height of the left and right subtrees differ by at most 1....

  • This lab serves as an intro to Java Interfaces and an Object-Oriented design strategy towards implementing...

    This lab serves as an intro to Java Interfaces and an Object-Oriented design strategy towards implementing data structures. We will be using Entry.java objects which are key,value pairs, with key being an integer and value being a generic parameter. We will be sorting these Entries according to their key value. The file Entry.java is given to you and does not require any modification. As you should know by now, bucket sort works by placing items into buckets and each bucket...

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
ADVERTISEMENT