solutions24 - cin >> size; for (int...

Info iconThis preview shows pages 1–2. Sign up to view the full content.

View Full Document Right Arrow Icon
CSCI-1100 Computer Science I Rensselaer Polytechnic Institute Spring 2004 Studio 24 -- Solutions Part 1 In function BinSearch(), change the line: if (item < data[middle]) to the following: if (item > data[middle]) and change the line: else if (item > data[middle]) to the following else if (item < data[middle]) In other words, change the < sign to a > sign and the > sign to a < sign. Part 2 #include <iostream> #include "slistlin.h" #include "time.h" using namespace std; int main () { SortedList list; int size; clock_t start, finish; cout << "Enter size of list: ";
Background image of page 1

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
Background image of page 2
This is the end of the preview. Sign up to access the rest of the document.

Unformatted text preview: cin &gt;&gt; size; for (int i=1; i&lt;=size; i++) { list.Insert(i); } start = clock(); for (int j=1; j&lt;=size; j++) { list.IsPresent(j); } finish = clock(); cout &lt;&lt; &quot;Time required for binary search: &quot; &lt;&lt; (double)(finish-start)/CLOCKS_PER_SEC &lt;&lt; &quot; seconds.&quot; &lt;&lt; endl; } You should observe that the times for linear search grow quickly and are substantially greater than the times for binary search when the list gets large. The times for binary search should be small, even for the largest size list....
View Full Document

Page1 / 2

solutions24 - cin &amp;amp;gt;&amp;amp;gt; size; for (int...

This preview shows document pages 1 - 2. Sign up to view the full document.

View Full Document Right Arrow Icon
Ask a homework question - tutors are online