| | |
Sorting Algorithms
Please support our C++ advertiser: Intel Parallel Studio Home
![]() |
•
•
Join Date: Nov 2009
Posts: 1
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: Finding the highest and the lowest number out of 5 inputs...
- Next Thread: Win32 API and C++
| Thread Tools | Search this Thread |
Tag cloud for C++
api application array arrays assignment beginner binary bitmap c++ c/c++ calculator char char* class classes code coding compile compiler console conversion convert count data database delete developer dll email encryption error file format forms fstream function functions game generator getline givemetehcodez graph homeworkhelper iamthwee ifstream image input int integer java lib loop looping loops map math matrix memory multidimensional multiple newbie news node number numbertoword output parameter pointer problem program programming project proxy python random read recursion recursive reference rpg sorting string strings struct template templates text tree url variable vector video visual visualstudio win32 windows winsock word wordfrequency wxwidgets






