Unhandled Exception while trying to sort - c++

I keep getting an unhandled exception in this portion of code whenever it runs, I have looked through it and can't see a clear reason. It will run fine up to the system pause that is commented out so it has to be something past that.Or maybe it is how I am setting my pivot?
template <class elemType>
int arrayListType<elemType>::medianpartition(int p, int r)
{
int middle = list[(r+p)/2];
int pivot = 0;
if(list[p]<list[r]){
if(middle<list[r]){
if(list[p]<middle){
pivot = middle;
}else if(list[p]>middle){
pivot = list[p];
}
}else if(middle>list[r]){
pivot = list[r];
}
}else if(list[p]>list[r]){
if(middle<list[r]){
pivot = list[r];
}else if(middle>list[r]){
if(middle<list[p]){
pivot = middle;
}else if(middle>list[p]){
pivot = list[p];
}
}
}
//system("Pause>nul");
while ( p < r )
{
while ( list[p] < pivot )
p++;
while ( list[r] > pivot )
r--;
if ( list[p] == list[r] )
p++;
else if ( p < r )
{
int tmp = list[p];
list[p] = list[r];
list[r] = tmp;
}
}
return r;
}
This is the function that calls it:
template <class elemType>
void arrayListType<elemType>::medianquicksort(int p, int r)
{
if ( p < r )
{
int j = medianpartition(p, r);
medianquicksort(p, j-1);
medianquicksort(j+1, r);
}
}
Any help would be appreciated!

If list[p] == list[r] then pivot is not assigned any value from the array and stays with its default value of zero. And if all items of the array are greater than zero, or all are less than zero, then one of the while loops runs p or r out of array's bounds.

Think about this: what happens if every element in your array is the same value?
You have too many if else if statements when trying to figure out the pivot, and you didn't account for the possibility that list[p], list[r] and/or middle have the same value. I don't know if that's the root of your problem, but it's certainly a problem.

Related

Partition Sorting issue

I am trying to do a quick sorting. But I have some problem with it. It was supposed to change this array:
{ 1,9,8,7,6,5,4,3,2,10 }
When it receives the arguments left = 0, right = 4 and pivotIndex = 2, to this one:
{ 7,6,1,8,9,5,4,3,2,10 }
But it is actually changing the array to this one:
{ 1,6,7,8,9,5,4,3,2,10 }
I also get this problem if I try to partition this array:
{ 7,6,1,8,9,5,4,3,2,10 }
with arguments right = 9, left = 4 and pivotIndex = 2, to this array:
{ 7,6,1,8,9,2,3,5,4,10 }
I am actually changing to this array:
{ 7,6,1,8,9,2,3,10,5,4 }
Code:
int QS::partition(int left, int right, int pivotIndex)
{
if (my_array == NULL)
return -1;
if (elements <= 0 || capacity <= 0)
return -1;
if (left < 0 || right < 0)
return -1;
else if (right - left <= 1)
return -1;
if (right > elements - 1)
return -1;
if (!(pivotIndex <= right) || !(pivotIndex >= left))
return -1;
int first = pivotIndex;
int last;
if (first == right)
last = left;
else
last = right;
int up = first + 1;
int down = last - 1;
do{
while (!(up != last - 1) || !(my_array[up] <= my_array[first]))
up++;
while (!(my_array[down] > my_array[first]) || !(down != first))
down--;
if (up < down)
swap(up, down);
} while (up < down);
swap(first, down);
return down;
}
I was told to use this algorithm:
But I could not make it work with this algorithm, the closest I got to the result I was told to get, was with the code I am showing you guys.
Can anyone help me? Thank you.

Binary searching using const pointer

We should implement a function that uses binary search to check if a value key is in the array and give either true or false.
My goes like this:
bool binary_search(const int* begin, const int * end, int key){
if(begin < end){
int mid = (end-begin)/2;
if(mid == key){
return true;
}else if (key < mid){
int h = mid-1;
end = &h;
binary_search(begin, end, key);
}else if (mid < key){
int i = mid+1;
begin = &i;
binary_search(begin, end, key);
}
}else{
return false;
}
}
but it wouldn't give any output but instead it gives me the error.
warning: control reaches end of non-void function [-Wreturn-type]
I don't really understand what I have to do here so can someone explain me what is going wrong here?
In case of these if else statements
}else if (key < mid){
int h = mid-1;
end = &h;
binary_search(begin, end, key);
}else if (mid < key){
int i = mid+1;
begin = &i;
binary_search(begin, end, key);
}
the function returns nothing. That is these code blocks do not have return statements.
Moreover the function does not make sense because for example in these statements
int mid = (end-begin)/2;
if(mid == key){
there are compared key with an index of the array instead of comparing key with the value of the element in the array with index mid.
Or these statements
int h = mid-1;
end = &h;
also do not make sense because the variable end will store an address of the local variable h.
The function can be implemented the following way as it is shown in this demonstrative program.
#include <iostream>
#include <iomanip>
bool binary_search( const int *begin, const int *end, int key )
{
if ( begin < end )
{
const int *mid = begin + ( end - begin ) / 2;
if ( *mid < key ) return binary_search( ++mid, end, key );
else if ( key < *mid ) return binary_search( begin, mid, key );
else return true;
}
else
{
return false;
}
}
int main()
{
int a[] = { 1, 3, 5 };
const size_t N = sizeof( a ) / sizeof( *a );
for ( size_t i = 0; i <= a[N-1] + 1; i++ )
{
std::cout << i << " is present in the array - "
<< std::boolalpha << binary_search( a, a + N, i )
<< std::endl;
}
return 0;
}
Its output is
0 is present in the array - false
1 is present in the array - true
2 is present in the array - false
3 is present in the array - true
4 is present in the array - false
5 is present in the array - true
6 is present in the array - false
You should return in all the possible branches.
Change
binary_search(begin, end, key);
to
return binary_search(begin, end, key);
to return the result from the recursive invocation.

Binary Search C++, STATUS_ACCESS_VIOLATION

the thing is that we are tring to do for first time a binary search function for a vector class, but I dont really know the reason why this is not working. Does anyone know what could be wrong(Status acces violation when the number is not in the vector array)??
// size_ is de number of used elements i the array
int Vector::bsearch(int value) const
{
int first = 0;
int last = size_ - 1;
int Substraction = size_ /2;
while(last >= first)
{
Substraction = first + (last - first) / 2;
if(array_[Substraction] > value)
last = Substraction;
else if(array_[Substraction] < value)
first = Substraction;
else if(array_[Substraction] == value)
return Substraction;
}
return CS170::Vector::NO_INDEX;
}
//SOLVED
int Vector::bsearch(int value) const
{
unsigned first = 0;
unsigned last = size_ - 1;
unsigned int mid;
if(value < array_[0] || value > array_[size_ - 1])
return CS170::Vector::NO_INDEX;
while(last >= first)
{
mid = first + (last - first) / 2;
if(value < array_[mid])
last = mid - 1;
else if(array_[mid] < value)
first = mid + 1;
else
return mid;
}
return CS170::Vector::NO_INDEX;
}
You're not excluding the element you just tested before the next loop. And your choice of variable names, Subtraction is dreadful. Its a midpoint.
int first = 0;
int last = size_-1;
int mid = 0;
while(last >= first)
{
mid = first + (last - first) / 2;
if(value < array_[mid])
last = mid-1; // don't include element just tested
else if(array_[mid] < value)
first = mid+1; // don't include element just tested
else return mid;
}
return CS170::Vector::NO_INDEX
Your initialization is wrong -
int first = size_ - 1;
int last = first = 0;
should be -
int last = size_ - 1;
int first = 0;
Also, in while you should use the following condition -
while (last>first)

Having trouble inserting into heap

Below is my C++ code for inserting into a heap. The value for k is inserted but it's inserted at the bottom. I expected it to heapify with that while loop.
void Insert(heap1* myHeap, int k)
{
(myHeap->size)++;
int i = myHeap->size;
while (i > 1 && myHeap->H[i/2].key < k)
{
myHeap->H[i].key = myHeap->H[i/2].key;
i = i/2;
}
myHeap->H[i].key = k;
}
I do have a heapify procedure that I tried to use for this before this attempt that I know works within my other heap procedures. I just can't get it to work within Insert so I went with the above route. Below is heapify just in case its useful:
void heapify(heap1* myHeap, int i)
{
int l = 2 * i;
int r = 2 * i + 1;
int largest;
if (l <= myHeap->size && myHeap->H[l].key > myHeap->H[i].key)
largest = l;
else
largest = i;
if (r <= myHeap->size && myHeap->H[r].key > myHeap->H[largest].key)
largest = r;
if (largest != i)
{
myHeap->H[i].key = myHeap->H[i].key + myHeap->H[largest].key;
myHeap->H[largest].key = myHeap->H[i].key - myHeap->H[largest].key;
myHeap->H[i].key = myHeap->H[i].key - myHeap->H[largest].key;
heapify(myHeap, largest);
}
}
If someone could lead me in the right direction on how to get it to restore its heap properties, I would largely appreciate it.
try using this code:
void insert(int heap[], int *n, int item){
(*n)++;
heap[*n] = item;
reheapify_upward(heap, *n);
}
void reheapify_upward(int heap[],int start){
int temp,parent;
if(start>1){
parent=start/2;
if(heap[parent]<heap[start]){
temp=heap[start];
heap[start]=heap[parent];
heap[parent]=temp;
reheapify_upward(heap,parent);
}
}
}

Algorithm to return the maximum possible sum of subsequences in a sequence

int maxSumRec(const vector<int> &a, int left, int right){
if (left == right){
if (a[left] > 0){
return a[left];
}
else
return 0;
}
int center = (left + right)/2;
int maxLeftSum = maxSumRec(a, left, center);
int maxRightSum = maxSumRec(a, center+1, right);
int leftBorderSum = 0; int leftBorderMax = 0;
for (int i = center; i >= left; i--){
leftBorderSum += a[i];
if (leftBorderSum > leftBorderMax){
leftBorderMax = leftBorderSum;
}
}
int rightBorderSum = 0; int rightBorderMax = 0;
for (int i = center+1; i <= right; i++){
rightBorderSum += a[i];
if (rightBorderSum > rightBorderMax){
rightBorderMax = rightBorderSum;
}
}
int crossSum = rightBorderMax + leftBorderMax;
return max3(maxLeftSum, maxRightSum, crossSum);
}
This is a O(NlogN) algorithm, I know it is not the best one. But just have several questions on it:
in the first if statement, why return 0 if a[left] < 0?
why need the 2 for loops? isn't the logic like this, find the max of the first half and the second half, and then add them to see if the addition is bigger than either.
if it is this case, then we can directly jump to the last 2 lines?
Thanks very much,
Yue Harriet
in the first if statement, why return 0 if a[left] < 0?
Because then the empty subsequence has the maximum sum, which is 0.
OK, I figured out the 2nd question, because we have to take care of consecutive terms instead of jumping terms.