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 _SORT_H_
#define _SORT_H_
#include
#include
#include
namespace my_algorithm{template
void swap(T& a, T& b){
T tmp = a;
a = b;
b = tmp;
}
/// Bubble Sort - Example
template
void bubble_sort(RandomAccessIterator first, RandomAccessIterator last,
Compare comp){
// Traverse from left to right for each ith largest element positioning
for(RandomAccessIterator i = first; i != last; ++i)
// Compare each element with its right neighbor to bubble up ith largest element
for(RandomAccessIterator j = first; j+1!= last; ++j)
if(comp(*(j+1),*j))// Compare j with its right neighbor
swap(*(j+1),*j); // Swap if neighbor is smaller
}
template
void insertion_sort(RandomAccessIterator first, RandomAccessIterator last, Comparator comp){
for (auto i = first +1; i != last; ++i){
//set key
auto key =*i;
//set position of j
auto j = i -1;
while( j >= first && comp(key,*j)){
*(j +1)=*j;
--j; //decrement j
}
//set new key
*(j+1)= key;
}
}
template
void merge(RandomAccessIterator 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 value_type = typename std::iterator_traits::value_type;
std::vector temp(std::distance(first, last));
//set i and j variables
auto i = first, j = mid;
//set temporary variable
auto temp_it = temp.begin();
//while values are smaller
while(i < mid && j < last){
if (comp(&j,*i)){
*temp_it++=*j++; //increment temp value and j
}
}
//set new value for temporary variable
temp_it = std::copy(i, mid, temp_it);
std::copy(j, last, temp_it);
std::copy(temp.begin(), temp.end(), first);
}
template
void merge_k(long int k, RandomAccessIterator first, RandomAccessIterator last, Comparator comp){
std::size_t n = std::distance(first, last);
//if list is sorted
if (n <= k){
return;
}
//set middle value
auto mid = first +(n /2);
//Recursively merge both halves individually
merge_k(first, mid, k, comp);
merge_k(mid, last, k, comp);
//merge halves together
merge(first, mid, last, comp);
}
emplate
void merge_sort(RandomAccessIterator first, RandomAccessIterator last, Comparator comp, long int k =1){
std::size_t n = std::distance(first, last);
//if already sorted
if (n <=1){
return;
}
k = n/2;
//Recursively sort the two halves
merge_sort(first, first + k, comp);
merge_sort(first + k, last, comp);
//merge the halves
merge(first, 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 "unit_test.h"
using std::equal;
using std::string;
using std::vector;
class algorithm_test : public test_class {
protected:
void test(){
test_insertion_sort_integers();
test_insertion_sort_integers_already_sorted();
test_insertion_sort_strings();
test_merge_sort_integers();
test_merge_sort_integers_already_sorted();
test_merge_sort_strings();
test_merge_sort_k_integers();
}
private:
///@brief Test insertion_sort using integers
void test_insertion_sort_integers(){
vector v ={3,2,5,6,8,4,3,1};
vector sorted ={1,2,3,3,4,5,6,8};
my_algorithm::insertion_sort(v.begin(), v.end(), std::less());
assert_msg(v.size()== sorted.size() &&
equal(sorted.begin(), sorted.end(), v.begin()),
"Insertion sort on integers failed");
}
///@brief Test insertion_sort using integers on an already sorted list
void test_insertion_sort_integers_already_sorted(){
vector v ={1,2,3,3,4,5,6,8};
vector sorted ={1,2,3,3,4,5,6,8};
my_algorithm::insertion_sort(v.begin(), v.end(), std::less());
assert_msg(v.size()== sorted.size() &&
equal(sorted.begin(), sorted.end(), v.begin()),
"Insertion sort on integers already sorted failed");
}
///@brief Test insertion_sort using strings
void test_insertion_sort_strings(){
vector v ={"a","h","b","z","b","e","d"};
vector sorted ={"z","h","e","d","b"

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!