`#include <iostream>
#include <cmath>
using namespace std;
void quicksort(int *n, int length){
if(length > 1){
int *left, *right;
int leftLength = 0, rightLength = 0;
for(int i = 0; i < length; i++){
if(n[i] <= n[length - 1]){
int *tmp = left;
leftLength++;
left = new int[leftLength];
for(int j = 0; j < leftLength - 1; j++){
left[j] = tmp[j];
}
left[leftLength - 1] = n[i];
delete[] tmp;
}else{
int *tmp = right;
rightLength++;
right = new int[rightLength];
for(int j = 0; j < rightLength - 1; j++){
right[j] = tmp[j];
}
right[rightLength - 1] = n[i];
delete[] tmp;
}
}
if(leftLength > 1)quicksort(left,leftLength);
if(rightLength > 1)quicksort(right,rightLength);
for(int i = 0; i < length; i++){
if(i < leftLength)n[i] = left[i]; else n[i] = right[i - leftLength];
}
delete[] left;
delete[] right;
}
}
int main(){
//MY_CODE
}`
I thought it is because of memory and i have tried to delete all temp memory after use. But it still error. If i don’t delete tmp it will alert “Exception has occurred. Segmentation fault” but when i add delete tmp it alerts “Exception has occurred. Unknown signal”
If you find any problem in my code please let me known.
New contributor
Phạm Thiên Minh is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.