Memory issue implementing quick sort - c++

I am trying to implement quick sort to sort a sequence of integers.
I am getting a segmentation default with the following code:
I implemented partition and quick sort recursive calls.But for some c++ reason I am getting an access to memory or an infinite loop I cant understand why.
#include <fstream>
#include<vector>
#include<iostream>
using namespace std;
int pivotSelection(vector<int> A){
return 0;
}
int partition(vector<int> &A,int l,int r){
int i=l+1;
int p = A[l];
for(int j=0; j< A.size(); j++) {
if(A[j]<p){
swap(A[j], A[i] );
i=i+1;
}
}
swap(A[l], A[i-1]);
return i;
}
vector<int> readArray(char* file){
ifstream inFile;
vector<int> A;
inFile.open(file);
int x;
while (inFile >>x ) {
A.push_back(x);
}
return A;
}
void quickSort(vector<int> &A, int l,int r){
if(r==1) {
return ;
}
if(r>l){
int p= partition(A,l,r);
quickSort(A,l,p-1);
quickSort(A,p+1,r);
}
}
int main(){
vector<int> A;//= readArray((char*)"/home/brunoeducsantos/AlgorithmFoundation/quicksort/data.txt");
A.push_back(3);
A.push_back(5);
A.push_back(7);
A.push_back(1);
int length = A.size();
quickSort(A,0,length-1);
for(int i=0;i<length;i++) cout<<A[i]<<endl;
return 0;
};
The expected result is: 1 3 5 7

Fixes noted in comments:
int partition(vector<int> &A,int l,int r){
int i=l+1;
int p = A[l];
for(int j=i; j<=r; j++) { // fix
if(A[j]<p){
swap(A[j], A[i] );
i=i+1;
}
}
swap(A[l], A[i-1]);
return i-1; // fix
}

Related

Can anyone please explain why my Merge Sort code is not working; CPP; Normal Approach

Can anyone please explain why my Merge Sort code is not working;
CPP;
I used very Normal Approach but still its not working properly.
#include<iostream>
using namespace std;
void merge(int a[], int l, int mid, int r)
{ int p[r];
int i=l;
int j= mid+1;
int k=l;
while(i<=mid && j<=r)
{
if(a[i]<a[j])
{
p[k] = a[i];
i++;
}
else
{
p[k] = a[j];
j++;
}
k++;
}
for(k=l; k<=r; k++)
{
a[k]=p[k];
}
}
void ms(int a[], int l, int r)
{
if(l<r)
{
int mid = (l+r)/2;
ms(a,l,mid);
ms(a, mid+1, r);
merge(a,l,mid,r);
}
}
int main()
{
int a[]={1,9,4,6,2,7};
int l=0; int r=5;
ms(a,l,r);
for(int i=0; i<=r; i++)
{
cout<<a[i];
}
return 0;
}
I learned this code from a youtube tutorial: https://www.youtube.com/watch?v=aDX3MFL0tYs
Its output should be 1,2,4,6,7,9
but it is showing 102700, please help.

TIME_LIMIT_EXCEEDED: Codeforces 686D

i was trying to solve this problem on codeforces http://codeforces.com/contest/686/problem/D, but i got TLE. could you tell me what takes so much time in my solution? is it data structures that i use or is the algorithm inefficient? how can i improve it?
#include <iostream>
#include <vector>
using namespace std;
int assignweights(int index, int weights[], vector<vector<int> > &v){
if(v[index].size()==0){
return 0;
}else{
vector<int> children=v[index];
int result=0;
result+=children.size();
for(int i=0; i<children.size(); ++i){
result+=assignweights(children[i], weights, v);
}
weights[index]=result;
}
return weights[index];
}
void getcentroid(int index, vector<vector<int> > &v, int weights[], int & result, int n){
vector<int> children=v[index];
bool b=false;
for(int i=0; i<children.size(); ++i){
if(weights[children[i]]+1>n/2){
if(!result)getcentroid(children[i], v, weights, result, n);
else break;
b=true;
}
}
if(!b){
result=index+1;
}
}
int main(){
int n, q;
cin>>n>>q;
vector<vector<int> > v;
for(int i=0; i<n; ++i){
vector<int> vv;
v.push_back(vv);
}
for(int i=1; i<=n-1; ++i){
int a;
cin>>a;
v[a-1].push_back(i);
}
int weights[n];
for(int i=0; i<n; ++i){
weights[i]=0;
}
assignweights(0, weights, v);
for(int i=0; i<q; ++i){
int index;
cin>>index;
index--;
int result=0;
getcentroid(index, v, weights, result, weights[index]);
cout<<result<<endl;
}
return 0;
}
In the worse test case, your solution my have complexity of O(n*q).
I think the best solution for this problem is for each node of the tree, you create a vector of its descendants at index 1,2,4,...2^x upon it.
With that vector, for each query, it takes O(log(n)) to find the answer.

Merging two arrays in ascending order

I know the logic how to merge two arrays but the problem is how to code.
This was my code n it is giving correct ans but my sir told me that do it again,please tell me what I have to add in this code,
#include<iostream>
using namespace std;
int mergeArrays(int array1[],int size1,int array2[],int size2);
int main()
{
const int size1=8;
const int size=12;
int arrayA[size1]={10,25,37,49,50,51,55,60};
int arrayB[size]={2,5,26,27,29,32,40,45,70,80,90,95};
mergeArrays(arrayA,size1,arrayB,size);
}
int mergeArrays(int array1[],int size1,int array2[],int size2)
{
int size3=size1+size2;
int *array3=new int[size3];
int k=0;
for(int i=0;i<size1;i++)
{
array3[k]=array1[i];
cout<<" "<<array3[k];
}
int j=0;
for(int i=size1;i<size2;i++)
{
array3[k]=array2[j];
}
for(int i=size1;i<size2;i++)
{
for(int j=0;j<size2;j++)
{
array3[i]=array2[j];
cout<<" "<<array3[i];
}
cout<<endl;
delete[]array3;
return array3[k++];
}
}
I had searched this in many places but could not corrected my code
I had written this code but it is not giving correct ans.
#include<iostream>
using namespace std;
int merge(int *a,int *b,int aSize,int bSize);
int main()
{
const int aSize={8};
const int bSize={12};
int arrayA[aSize]={10,25,37,49,50,51,55,60};
int arrayB[bSize]={2,5,26,27,29,32,40,45,70,80,90,95};
merge(arrayA,arrayB,aSize,bSize);
return 0;
}
int merge(int *a,int *b,int aSize ,int bSize)
{
int cSize=aSize+bSize;
int *c=new int[cSize];
int j=0,k=0;
int i=0;
while(i<=aSize&&j<=bSize )
{
if(a[aSize ]<=b[bSize])
{
c[k]=a[aSize];
k++;
i++;
}
else
{
c[k]=b[bSize];
k++;
j++;
}
}
for(int i=0;i<k;i++)
{
cout<<c[i]<<endl;
}
delete[]c;
return c[k++];
}
your sir request you do Merging two arrays in ascending order. so i think you should return a new array, fill with array1 and array2's element, and the elements should be ascending order. here is a implement.(suppose your input arraies is already in ascending order.)
#include <iostream>
using namespace std;
int mergeArrays(int array1[],int size1,int array2[],int size2, int outArray[]);
int main()
{
const int size1=8;
const int size=12;
int arrayA[size1]={10,25,37,49,50,51,55,60};
int arrayB[size]={2,5,26,27,29,32,40,45,70,80,90,95};
int outArray[size1+size];
int len = mergeArrays(arrayA,size1,arrayB,size, outArray);
cout <<" "<< len;
for (int i = 0; i< size1+size; ++i){
cout <<" " << outArray[i];
}
}
int mergeArrays(int array1[], int size1, int array2[], int size2, int outArray[])
{
int i=0, j=0, k=0;
int retSize = size1+size2;
while (k<retSize){
if (i==size1){// only left array2, copy it
for (; j<size2; ++j){
outArray[k++] = array2[j];
}
}else if (j == size2) { // only left array1, copy it
for (; i<size1; ++i){
outArray[k++] = array1[i];
}
}
else if (array1[i] > array2[j]){ // copy the min value to outArray
outArray[k++] = array2[j++];
}else{
outArray[k++] = array1[i++];
}
}
return k;
}
now, let's look at your first code:
int mergeArrays(int array1[],int size1,int array2[],int size2)
{
int size3=size1+size2;
int *array3=new int[size3];
int k=0;
for(int i=0;i<size1;i++)
{
array3[k]=array1[i]; // k is not changed, so you just assign array1's each value to array3[0]
cout<<" "<<array3[k];
}
int j=0;
// what's the purpose of this loop?
// and in loop, you don't use i, you just repeat set array3[0] = array2[0]!!
for(int i=size1;i<size2;i++)
{
array3[k]=array2[j];
}
for(int i=size1;i<size2;i++) // if array2's length bigger than array1's, will enter this loop.
{
for(int j=0;j<size2;j++)
{
array3[i]=array2[j]; // this just repeat assign array2's each value to array3[i]!!
cout<<" "<<array3[i];
}
cout<<endl;
delete[]array3;
return array3[k++]; // you delete array3, but at here you access it!! this will crash!
// also, in this for i loop, you have return, so it will only execute once.
}
// reach function end and no return if not enter for loop.
}
I haven't looked at your second code. I think you still need to do more study.

Recursion function is not responding

# include <iostream>
using namespace std;
class mm
{
private:
int k[1000];
int n;
int i;
int a;
int b;
int f;
public:
mm ()
{
a=0;
b=1;
f=0;
i=0;
for(int i=0; i<n;i++)
k[i]=0;
};
~mm()
{
}
void fib(int n)
{
for (int i=0;i<n;i++)
{
if (i<=1)
f=i;
else
{
f=a+b;
a=b;
b=f;
}
k[i]=f;
}
for (int j=0;j<n;j++)
cout<<k[j]<<" ";
}
int se (int n, int i)
{
if (n==1)
return 1;
else
return 1/k[i] + se (n-1, i+1);
}
};
int main()
{
int n;
cout<<"Enter n:";
cin>>n;
mm p;
cout<<"fib: "<<endl;
p.fib(n);
cout<<endl;
cout<<"se: ";
cout<<p.se(n,0);
return 0;
}
Recursion function from main is not responding. Maybe the array k[i] is not working, but I cant find the reason. Can anyone help me?
k[0] is set to 0. When you then call se(n,0) in main, it computes 1/k[0] + se(n-1,1) which is a division by zero.

Infinite computation of qsort

i am realsing qsort algorithm, according on Korman. But there is infinite computation, when i trying to start it. I suppose, that problem is in partition. My programms reads from file, first number in file-line is counting of numbers to sort, and next go all numbers
#include <stdio.h>
#include <stdlib.h>
int swap(int &a, int &b);
int sorting(int *array, int &b, int &l);
int partition(int *array ,int &begin, int &last);
int main()
{
FILE* pFile=fopen("input.txt", "r");
//fopen("input.txt", "r");
//fopen("output.txt", "w");
int n;
int begin=0;
fscanf(pFile, "%d", &n);
int* array=(int*)malloc(n*sizeof(int));
for (int i=0; i<n; ++i)
fscanf(pFile,"%d", &array[i]);
int n1=n-1;
sorting(array, begin, n1);
printf("JJJJ");
for (int i=0; i<n; ++i)
printf("%d ", array[i]);
printf("\n");
fclose(pFile);
free(array);
return 0;
}
int sorting(int* array, int &b, int &l)
{
int pivot,pivot1;
if(b<l)
{
pivot=partition(array, b, l);
printf("MAXMAX321");
int a=pivot-1;
sorting(array, b, a);
printf("MAXMAX123");
pivot1=pivot+1;
sorting(array, pivot1, l);
printf("MAXMAX");
}
return 0;
}
int partition(int* array, int &b, int &l)
{
int x=array[b];
int i=b;
int j=l;
while(true)
{
while(array[j]>x){
printf("AHAH");
--j;
}
while(array[i]<x){
printf("AZAZA");
++i;
}
if(i<j)
swap(array[i],array[j]);
else
return j;
}
}
int swap(int &x, int &y)
{
x=x+y;
y=x-y;
x=x-y;
return 0;
}
Thank you in advance.
This part is very dangerous code:
while(true)
{
while(array[j]>x){
printf("AHAH");
--j;
}
while(array[i]<x){
printf("AZAZA");
++i;
}
if(i<j)
swap(array[i],array[j]);
else
return j;
}
Avoid while (true) as much as possible, except on very special case you should have a clear condition stated in the loop. Here it is the one that leads to `return. It will clarify the goal of the loop.
When testing array value like while(array[i]<x) compare i to the array size before ! You're never sure that you condition will always be met in the array, ensure you have a safety belt.
I cannot understand what you are doing in function partition but doing it like this should work:
int partition(int* array, int &b, int &l)
{
int pivot_index = l;//You can take here everything between b and l
//swap(array[l], array[pivot_index]); decomment this if your pivot is between b and l
int current_pos = b;
for(int i = b;i < l - 1; ++i)
if(array[i] <= array[pivot_index])
swap(array[i], array[current_pos++]);
swap(array[l], array[current_pos]);
return current_pos;
}