| | |
Sorting Algorithms
Please support our C++ advertiser: Intel Parallel Studio Home
![]() |
•
•
Join Date: Nov 2009
Posts: 5
Reputation:
Solved Threads: 0
I need Help!
Consider this following modified version of the binary search algorithm. (Modifications are indicated by a comment highlighted asterisks.) With this new version of the binary search algorithmn work correctly for all data? If not, specify a situiation in which this version will fail.
Consider this following modified version of the binary search algorithm. (Modifications are indicated by a comment highlighted asterisks.) With this new version of the binary search algorithmn work correctly for all data? If not, specify a situiation in which this version will fail.
C++ Syntax (Toggle Plain Text)
template<class element, class Keytype> bool binarySearch(const apvector<element> &list, int n, Keytype target, element &object) { int low, middle, high; bool found = false; low = 0; high = n; while ((low <= high) && ! found) { middle = (low + high) / 2; if(list[middle] = target) found=true; else if (list[middle] < target) low = middle; // *** MODIFICATION HERE *** else high = middle; // *** MODIFICATION HERE *** } if (found) object=list[middle]; return found; }
-7
#2 Nov 6th, 2009
1) Line 13 is wrong. It should use the == boolean operator, not the = assignment operator.
>>If not, specify a situiation in which this version will fail.
I can think of three kinds of data for which that algorithm will fail. If you think about the different kinds of data then you should be able to list at least one of them too.
>>If not, specify a situiation in which this version will fail.
I can think of three kinds of data for which that algorithm will fail. If you think about the different kinds of data then you should be able to list at least one of them too.
Last edited by Ancient Dragon; Nov 6th, 2009 at 9:14 am.
Don't PM me with questions -- you might get a nasty PM in response. If you have a question then post it in one of the forums.
![]() |
Similar Threads
- Sorting in Python (Python)
- any tutorials on algorithms for beginners (Computer Science)
- Link List Sorting Problem (C++)
- Sorting Algorithms using Time (C++)
- bubble sorting in an array (C)
- counting comparisons when sorting (C++)
- re: Sorting Algorithms (C++)
Other Threads in the C++ Forum
- Previous Thread: Read text from file & Display every 24 lines -- C++
- Next Thread: Need help with assignment
Views: 326 | Replies: 2
| Thread Tools | Search this Thread |
Tag cloud for C++
6 add api array arrays beginner binary c++ c/c++ calculator char class classes code compile compiler console conversion convert count data delete desktop directshow dll download dynamic encryption error file forms fstream function functions game givemetehcodez google graph gui homeworkhelper iamthwee ifstream input int integer java lib library linkedlist linker linux loop looping loops map math matrix memory microsoft newbie news number output pointer problem program programming project python random read recursion recursive reference return sort stream string strings struct studio system template templates test text text-file tree unix url variable vector video visual visualstudio win32 windows winsock wordfrequency wxwidgets






