std::sort function compile error

closed account (9hX8C542)
So, I'm supposed to use different sorting functions and and compare the times in microseconds to each other, to see which one is faster.

The problem is that std::sort keeps failing, and the error code isn't that easy to read. If someone could help me out with this, that would be great. I'll take any help that I can get.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
#include <iostream>
#include <chrono>
#include <algorithm>
#include <functional>
#include <array>

using namespace std::chrono;

void swap(int* a, int* b);

template <class T>
void selectionSort(T arr[], T n);

template <class T>
void bubbleSort(T arr[], T n);

template <class T>
void merge(T arr[], T l, T m, T r);
template <class T>
void mergeSort(T arr[], T l, T r);

template <class T>
int partition (T arr[], T low, T high);
template <class T>
void quickSort(T arr[], T low, T high);

template <class T>
void heapify(T arr[], T n, T i);
template <class T>
void heapSort(T arr[], T n);

void test(const unsigned int size);


int main()
{
    test(60000);
    
}//main

void swap(int* a, int* b)
{
    int t = *a;
    *a = *b;
    *b = t;
}

template <class T>
void selectionSort(T arr[], T n)
{
    T i, j, min_idx;
  
    // One by one move boundary of unsorted subarray
    for (i = 0; i < n-1; i++)
    {
        // Find the minimum element in unsorted array
        min_idx = i;
        for (j = i+1; j < n; j++)
        if (arr[j] < arr[min_idx])
            min_idx = j;
  
        // Swap the found minimum element with the first element
        swap(&arr[min_idx], &arr[i]);
    }
}//functin selectionSort

template <class T>
void bubbleSort(T arr[], T n)
{
    T i, j;
    for (i = 0; i < n-1; i++)
      
    // Last i elements are already in place
    for (j = 0; j < n-i-1; j++)
        if (arr[j] > arr[j+1])
            swap(&arr[j], &arr[j+1]);
}//function bubbleSort

template <class T>
void merge(T arr[], T l, T m, T r)
{
    T i, j, k;
    T n1 = m - l + 1;
    T n2 =  r - m;
  
    /* create temp arrays */
    T L[n1], R[n2];
  
    /* Copy data to temp arrays L[] and R[] */
    for (i = 0; i < n1; i++)
        L[i] = arr[l + i];
    for (j = 0; j < n2; j++)
        R[j] = arr[m + 1+ j];
  
    /* Merge the temp arrays back into arr[l..r]*/
    i = 0; // Initial index of first subarray
    j = 0; // Initial index of second subarray
    k = l; // Initial index of merged subarray
    while (i < n1 && j < n2)
    {
        if (L[i] <= R[j])
        {
            arr[k] = L[i];
            i++;
        }
        else
        {
            arr[k] = R[j];
            j++;
        }
        k++;
    }
  
    /* Copy the remaining elements of L[], if there
       are any */
    while (i < n1)
    {
        arr[k] = L[i];
        i++;
        k++;
    }
  
    /* Copy the remaining elements of R[], if there
       are any */
    while (j < n2)
    {
        arr[k] = R[j];
        j++;
        k++;
    }
}//function merge
  
/* l is for left index and r is right index of the
   sub-array of arr to be sorted */
template <class T>
void mergeSort(T arr[], T l, T r)
{
    if (l < r)
    {
        // Same as (l+r)/2, but avoids overflow for
        // large l and h
        T m = l+(r-l)/2;
  
        // Sort first and second halves
        mergeSort(arr, l, m);
        mergeSort(arr, m+1, r);
  
        merge(arr, l, m, r);
    }
}//function mergeSort

template <class T>
int partition (T arr[], T low, T high)
{
    T pivot = arr[high]; // pivot
    T i = (low - 1); // Index of smaller element
  
    for (T j = low; j <= high - 1; j++)
    {
        // If current element is smaller than the pivot
        if (arr[j] < pivot)
        {
            i++; // increment index of smaller element
            swap(&arr[i], &arr[j]);
        }
    }
    swap(&arr[i + 1], &arr[high]);
    return (i + 1);
}//function partition

template <class T>
void quickSort(T arr[], T low, T high)
{
    if (low < high)
    {
        /* pi is partitioning index, arr[p] is now
        at right place */
        T pi = partition(arr, low, high);
  
        // Separately sort elements before
        // partition and after partition
        quickSort(arr, low, pi - 1);
        quickSort(arr, pi + 1, high);
    }
}//function quickSort

template <class T>
void heapify(T arr[], T n, T i)
{
    T largest = i; // Initialize largest as root
    T l = 2*i + 1; // left = 2*i + 1
    T r = 2*i + 2; // right = 2*i + 2
  
    // If left child is larger than root
    if (l < n && arr[l] > arr[largest])
        largest = l;
  
    // If right child is larger than largest so far
    if (r < n && arr[r] > arr[largest])
        largest = r;
  
    // If largest is not root
    if (largest != i)
    {
        swap(&arr[i], &arr[largest]);
  
        // Recursively heapify the affected sub-tree
        heapify(arr, n, largest);
    }
}//function heapify

template <class T>
void heapSort(T arr[], T n)
{
    // Build heap (rearrange array)
    for (T i = n / 2 - 1; i >= 0; i--)
        heapify(arr, n, i);
  
    // One by one extract an element from heap
    for (T i=n-1; i>=0; i--)
    {
        // Move current root to end
        swap(&arr[0], &arr[i]);
  
        // call max heapify on the reduced heap
        heapify(arr, i, 0);
    }
}//function heapSort


void test(const unsigned int size)
{
    srand(time(NULL));
    
    int array[size];
    
    for (int i = 0; i < size; i++)
    {
        array[i] = rand() % size;
    }
    
    //
    auto startSS = high_resolution_clock::now();
    selectionSort <int>(array, size);
    auto stopSS = high_resolution_clock::now();
    auto durationSelection = duration_cast<microseconds>(stopSS - startSS);
    //
    
    for (int i = 0; i < size; i++)
    {
        array[i] = rand() % size;
    }
    
    //
    auto startBS = high_resolution_clock::now();
    bubbleSort <int>(array, size);
    auto stopBS = high_resolution_clock::now();
    auto durationBubble = duration_cast<microseconds>(stopBS - startBS);
    //
    
    for (int i = 0; i < size; i++)
    {
        array[i] = rand() % size;
    }
    
    //
    auto startMG = high_resolution_clock::now();
    mergeSort <int>(array, array[0], array[size-1]);
    auto stopMG = high_resolution_clock::now();
    auto durationMerge = duration_cast<microseconds>(stopMG - startMG);
    //
    
    for (int i = 0; i < size; i++)
    {
        array[i] = rand() % size;
    }
    
    //
    auto startQS = high_resolution_clock::now();
    quickSort <int>(array, array[0], array[size-1]);
    auto stopQS = high_resolution_clock::now();
    auto durationQuick = duration_cast<microseconds>(stopQS - startQS);
    //
    
    for (int i = 0; i < size; i++)
    {
        array[i] = rand() % size;
    }
    
    //
    auto startHP = high_resolution_clock::now();
    heapSort <int>(array, size);
    auto stopHP = high_resolution_clock::now();
    auto durationHeap = duration_cast<microseconds>(stopHP - startHP);
    //
    
    for (int i = 0; i < size; i++)
    {
        array[i] = rand() % size;
    }
    
    //
    auto startSort = high_resolution_clock::now();
    std::sort(array[0], array[size-1]);
    auto stopSort = high_resolution_clock::now();
    auto durationSort = duration_cast<microseconds>(stopSort - startSort);
    
    
    std::cout << size << " | "
              << durationSelection.count() << " "
              << durationBubble.count()  << " "
              << durationMerge.count() << " "
              << durationQuick.count() << " "
              << durationHeap.count() << " "
              << durationSort.count() << " "
              << std::endl;
    
}//function sixtyTest 
you need to pass pointers.

std::sort(arr, arr+size);
Last edited on
closed account (9hX8C542)
Oh shit it worked bro, thanks for that quick reply
you are welcome.

no shell :( all the profs hate shell...

To get it to run, change line 304 to
std::sort(array, array+size);

What makes you think that your sort algorithms work correctly? When I add code to check the order, it says that mergeSort and quickSort aren't working:
1
2
3
4
5
6
7
8
9
10
void check(int arr[], unsigned size, const char *name)
{
    for (unsigned i=1; i<size; ++i) {
        if (arr[i] < arr[i-1]) {
            std::cout << "array out of order on " << name << '\n';
            return;
        }
    }
    std::cout << name << " is okay" << std::endl;
}

Then change each call to a sort function to be like this:
1
2
3
4
5
    auto startSS = high_resolution_clock::now();
    selectionSort <int>(array, size);
    auto stopSS = high_resolution_clock::now();
    auto durationSelection = duration_cast<microseconds>(stopSS - startSS);
    check(array, size, "selectionSort");


Also, your templates won't work for anything except an int:
1
2
template <class T>
void selectionSort(T arr[], T n);

Suppose I have an array of class Dog:
Dog myDogs[12];
To call selectionSort on an array of Dog, I have to pass a Dog as parameter n. Which dog should I pass?

And within selection sort I see:
1
2
3
    T i, j, min_idx;
    ...
    for (i = 0; i < n-1; i++)

My Dog class won't let you assign an int to a Dog. So this won't work.

Also, I see you calling swap() but you've only defined a swap() function for ints.
Topic archived. No new replies allowed.