Deliverables: To complete this assignment you must --
1.) Write
the HTML and JavaScript page described below and demonstrate to the lab
instructor that it operates correctly.
Introduction: The main goal of this assignment is to give you an idea of how quick O(logn) algorithms are compared to O(n) algorithms. We learned in class that Unordered Search is O(n) while Ordered Search is O(logn). In this assignment you will use the html page provided you to investigate the difference. The page provided to you allows you to create an unordered list and an ordered list. It then allows you to search for elements within the list. I have provided functions to make the lists and perform the ordered search. You must program the function that performs the unordered search (use the algorithm provided below). Then create the lists and experiment with searching through them. Finally, fill in the blanks on the questions at the top of the page by editing the HTML document.
UnSortedSearch(): Searching for a key in a list of numbers.
Section | Pseudocode | Comments |
---|---|---|
INPUT: | Integer list[n]; Integer key; |
This represents a list of n numbers and a key. |
OTHER VARIABLES: | Integer i; Integer ops; String result; |
i is the loop variable. ops counts the number of operations required. result is true or false. |
INITIALIZATION: | Assume the list is given Assume the key is given result = "False"; ops = 0; |
The list is given to us by the |
COMPUTATION: | FOR i = 0 to (n - 1) DO ops = ops + 3; IF ( list[i] == key ) THEN result = "True"; break; ENDIF ENDDO |
Quite a bit going on here. The loop shouldn't surprise you. The 'ops' summation simply counts approximate number of operations performed per loop iteration. The "break;" statement terminates the loop if the key is found. |
OUTPUT: | document.lab8.uresult.value = result; document.lab8.u_ops.value = ops; |
These commands simply paste the results to the form text boxes (Note: I sometimes get a typo in the name of the box). |