Question: I can't get my sort.h code to run with my other code ifndef _ SORT _ H _ #define _ SORT _ H _
I can't get my "sort.h code to run with my other code
ifndef SORTH
#define SORTH
#include
#include
#include
namespace myalgorithmtemplate
void swapT& a T& b
T tmp a;
a b;
b tmp;
Bubble Sort Example
template
void bubblesortRandomAccessIterator first, RandomAccessIterator last,
Compare comp
Traverse from left to right for each ith largest element positioning
forRandomAccessIterator i first; i last; i
Compare each element with its right neighbor to bubble up ith largest element
forRandomAccessIterator j first; j last; j
ifcompjj Compare j with its right neighbor
swapjj; Swap if neighbor is smaller
template
void insertionsortRandomAccessIterator first, RandomAccessIterator last, Comparator comp
for auto i first ; i last; i
set key
auto key i;
set position of j
auto j i ;
while j first && compkeyj
j j;
j; decrement j
set new key
j key;
template
void mergeRandomAccessIterator first, RandomAccessIterator mid, RandomAccessIterator last, Comparator comp
Hint: To allocate external memory to store merging of result use
std::vector
Remember to copy back the merged result
using valuetype typename std::iteratortraits::valuetype;
std::vector tempstd::distancefirst last;
set i and j variables
auto i first, j mid;
set temporary variable
auto tempit temp.begin;
while values are smaller
whilei mid && j last
if comp&ji
tempitj; increment temp value and j
set new value for temporary variable
tempit std::copyi mid, tempit;
std::copyj last, tempit;
std::copytempbegin temp.end first;
template
void mergeklong int k RandomAccessIterator first, RandomAccessIterator last, Comparator comp
std::sizet n std::distancefirst last;
if list is sorted
if n k
return;
set middle value
auto mid first n ;
Recursively merge both halves individually
mergekfirst mid, k comp;
mergekmid last, k comp;
merge halves together
mergefirst mid, last, comp;
emplate
void mergesortRandomAccessIterator first, RandomAccessIterator last, Comparator comp, long int k
std::sizet n std::distancefirst last;
if already sorted
if n
return;
k n;
Recursively sort the two halves
mergesortfirst first k comp;
mergesortfirst k last, comp;
merge the halves
mergefirst first k last, comp;
#endif
Here is the other code that it won't work with, but not sure why?
#include
#include
#include
#include
#include "sort.h
#include "unittest.h
using std::equal;
using std::string;
using std::vector;
class algorithmtest : public testclass
protected:
void test
testinsertionsortintegers;
testinsertionsortintegersalreadysorted;
testinsertionsortstrings;
testmergesortintegers;
testmergesortintegersalreadysorted;
testmergesortstrings;
testmergesortkintegers;
private:
@brief Test insertionsort using integers
void testinsertionsortintegers
vector v ;
vector sorted ;
myalgorithm::insertionsortvbegin vend std::less;
assertmsgvsize sorted.size &&
equalsortedbegin sorted.end vbegin
"Insertion sort on integers failed";
@brief Test insertionsort using integers on an already sorted list
void testinsertionsortintegersalreadysorted
vector v ;
vector sorted ;
myalgorithm::insertionsortvbegin vend std::less;
assertmsgvsize sorted.size &&
equalsortedbegin sorted.end vbegin
"Insertion sort on integers already sorted failed";
@brief Test insertionsort using strings
void testinsertionsortstrings
vector v ahbzbed;
vector sorted zhedb
Step by Step Solution
There are 3 Steps involved in it
1 Expert Approved Answer
Step: 1 Unlock
Question Has Been Solved by an Expert!
Get step-by-step solutions from verified subject matter experts
Step: 2 Unlock
Step: 3 Unlock
