# std::sort problems

Page 1 of 1

## 11 Replies - 266 Views - Last Post: 28 January 2018 - 09:02 AMRate Topic: //<![CDATA[ rating = new ipb.rating( 'topic_rate_', { url: 'https://www.dreamincode.net/forums/index.php?app=forums&module=ajax&section=topics&do=rateTopic&t=408974&amp;s=e60322189d9526f1e28697fc65b4c3d4&md5check=' + ipb.vars['secure_hash'], cur_rating: 0, rated: 0, allow_rate: 0, multi_rate: 1, show_rate_text: true } ); //]]>

### #1 ben255

Reputation: 39
• Posts: 509
• Joined: 09-September 13

# std::sort problems

Posted 28 January 2018 - 06:25 AM

My issue is that i can't understand why the sorting algorithm doesn't work.

it works on this code
```void studentsTest1(){

printf("Har borde studentens testresultat synas\n\n");

int *testArray = new int[4];
testArray[0] = 2;
testArray[1] = 6;
testArray[2] = 4;
testArray[3] = 9;

for(int i = 0; i < 4; i++){
printf("...%d...", testArray[i]);

}
printf("\n");

std::sort(testArray, testArray+4);

for(int i = 0; i < 4; i++){
printf("...%d...", testArray[i]);

}
int lol = contains(testArray, testArray+4, 2);

printf("----%d----", lol);

}
```

but when i run this function it throws a error. the issue is std::sort not sure if the recursion works since i can't run it when the sorting fails, so i don't need help with the recursion if its wrong. i want to solve that part myself.
thanks

```bool contains(const int* pBegin, const int* pEnd,  int x)
{
if(*pBegin == x)
return true;

int middle = (pEnd-pBegin)/2;

std::sort(pBegin, pEnd-1);

if(*pBegin+middle > x)
return contains(pBegin+middle, pEnd, x);
else if(*pBegin+middle < x)
return contains(pBegin, pEnd-middle, x);
else return false;

}// contains
```

Is This A Good Question/Topic? 0

## Replies To: std::sort problems

### #2 jimblumberg

Reputation: 5466
• Posts: 17,018
• Joined: 25-December 09

## Re: std::sort problems

Posted 28 January 2018 - 06:40 AM

Quote

but when i run this function it throws a error.

What exactly is the error? Please post the complete error message, all of them, exactly as they appear in your development environment.

Why are you mixing C with C++ code?

You really should consider using a vector instead of the array since a vector always has iterator support, where as a pointer doesn't.

Jim

### #3 ben255

Reputation: 39
• Posts: 509
• Joined: 09-September 13

## Re: std::sort problems

Posted 28 January 2018 - 06:49 AM

```/Users/Ben255/Downloads/Chap2/RecursiveBinarySearch/studentbinsearch1.cpp:34: in instantiation of function template specialization 'std::__1::sort<const int *>' requested here
std::sort(pBegin, pEnd-1);
^

```

thats the error for the row, don't know how to copy it all.

yah but I'm not allowed to chance that function.

the teacher used printf so i did the same

### #4 jimblumberg

Reputation: 5466
• Posts: 17,018
• Joined: 25-December 09

## Re: std::sort problems

Posted 28 January 2018 - 07:02 AM

Quote

yah but I'm not allowed to chance that function.

What function are you not allowed to change?

Jim

### #5 ben255

Reputation: 39
• Posts: 509
• Joined: 09-September 13

## Re: std::sort problems

Posted 28 January 2018 - 07:04 AM

all I'm allowed to write in are those two functions. hes written test code to test that contains function out. I'm not allowed to chance the data that comes in

### #6 ben255

Reputation: 39
• Posts: 509
• Joined: 09-September 13

## Re: std::sort problems

Posted 28 January 2018 - 07:16 AM

hmm. i think i might have solved it.... gg.. added '&' to the pointers. i guess i didn't send the adress...

### #7 Skydiver

• Code herder

Reputation: 6160
• Posts: 21,239
• Joined: 05-May 12

## Re: std::sort problems

Posted 28 January 2018 - 07:48 AM

Did you test?

If what you changed was:
```std::sort(pBegin, pEnd-1);

```

To
```std::sort(&pBegin, &pEnd-1);

```

then you are headed for some undefined behavior that may lead to a crash. Instead of trying to sort the integer data between the addresses pointed to by pBegin and pEnd-1, you are now trying to sort the pointers between the location of pBegin and the location before pEnd which will be on the stack.

### #8 ben255

Reputation: 39
• Posts: 509
• Joined: 09-September 13

## Re: std::sort problems

Posted 28 January 2018 - 08:03 AM

oh, but i just saw that the array he sends in is alrdy sorted so it was my blunder all along gotta read better. anyway thanks for the help

### #9 Skydiver

• Code herder

Reputation: 6160
• Posts: 21,239
• Joined: 05-May 12

## Re: std::sort problems

Posted 28 January 2018 - 08:09 AM

To answer your question of why the call to std::sort() in your contains() won't compile, the reason is because std::sort() needs to be able to change the data within the range specified. The issue is that your contains() method declared its parameters as const. That is a promise to the caller that it will not change the data. The compiler is trying its best to hold you to that promise.

### #10 jimblumberg

Reputation: 5466
• Posts: 17,018
• Joined: 25-December 09

## Re: std::sort problems

Posted 28 January 2018 - 08:36 AM

If you're just trying to determine if the value (x) is contained within the array why not just use std::find?

```#include<iostream>
#include<map>
#include <algorithm>

using namespace std;

bool contains(const int* pBegin, const int* pEnd,  int x)
{
return std::find(pBegin, pEnd, x) != pEnd;
}

int main()
{
int testArray[]{2, 6, 4, 9};

std::cout << boolalpha << contains(testArray, testArray+sizeof(testArray), 5) << std::endl;

}

```

Jim

### #11 ben255

Reputation: 39
• Posts: 509
• Joined: 09-September 13

## Re: std::sort problems

Posted 28 January 2018 - 08:38 AM

we are working with recursion and we have to construct a binary search

### #12 baavgai

• Dreaming Coder

Reputation: 7161
• Posts: 14,926
• Joined: 16-October 07

## Re: std::sort problems

Posted 28 January 2018 - 09:02 AM

Your binary search must assume that the collection is sorted, else a binary search is not applicable. A binary "search" should NOT be doing any sorting.