     size_t N = 20;
 
     // generate three arrays of random values
-    thrust::host_vector<int> upper(N);
-    thrust::host_vector<int> middle(N);
-    thrust::host_vector<int> lower(N);
-    thrust::generate(upper.begin(),  upper.end(),  rand10);
-    thrust::generate(middle.begin(), middle.end(), rand10);
-    thrust::generate(lower.begin(),  lower.end(),  rand10);
+    thrust::device_vector<int> upper  = random_vector(N);
+    thrust::device_vector<int> middle = random_vector(N);
+    thrust::device_vector<int> lower  = random_vector(N);
     
     std::cout << "Unsorted Keys" << std::endl;
     for(size_t i = 0; i < N; i++)

#include <thrust/host_vector.h>
#include <thrust/generate.h>
#include <thrust/sequence.h>
#include <thrust/sort.h>

}


template <typename KeyVector, typename PermutationVector>
void apply_permutation(KeyVector& keys, PermutationVector& permutation)
{
    // copy keys to temporary vector
    KeyVector temp(keys.begin(), keys.end());

    // permute the keys
    thrust::gather(keys.begin(), keys.end(), permutation.begin(), temp.begin());
}


thrust::host_vector<int> random_vector(size_t N)
{
    thrust::host_vector<int> vec(N);

    for (size_t i = 0; i < N; i++)
        vec[i] = rand() % 10;

    return vec;
}


