I have been looking through all of the threads here and couldn't seem to find a solution that worked. The sort works, but it's incredibly slow compared to what it should be. Here is the code (i'm working out of a header file):
#pragma once
#ifndef DataGen_h
#define DataGen_h
#include "RandomSupport.h"
void merge(long list[], long start, long mid, long end) {
long i = start;
long j = mid + 1;
while (j <= end && i <= mid) {
if (list[i] < list[j]) {
i++;
}
else {
long temp = list[j];
for (long k = j; k > i; k--) {
list[k] = list[k - 1];
}
list[i] = temp;
mid++;
i++;
j++;
}
}
}
void merge_sort(long list[], long startIndex, long endIndex)
{
if (startIndex >= endIndex){
return;
}
long midIndex = (startIndex + endIndex) / 2;
merge_sort(list , startIndex, midIndex);
merge_sort(list, midIndex + 1, endIndex);
merge(list, startIndex, midIndex, endIndex);
}
void efficientRandomSortedList(long temp[], long s) {
// Get a new random device
randomizer device = new_randomizer();
// Get a uniform distribution from 1 to 1000
uniform_distribution range = new_distribution(1, 45000);
for (long i = 0; i < s; i++) {
// At every cell of the array, insert a randomly selected number
// from distribution defined above
temp[i] = sample(range, device);
}
// Now sort the array using insertion_sort
merge_sort(temp, 0, s - 1);
}
#endif /* DataGen_h */
This is for class so unfortunately I can't change the data type that I'm working with. Any help or general critiques of my formatting would be helpful.