building MIN-Heap in c++ - c++

I am trying to implement min-heap just by reversing the propeties of max_heapify int the code but I am getting a segmentation fault(core dumped) during execution.What I know somewhere the code is accessing a memory to which it does not has access to. What is the problem with this code?
#include <iostream>
using namespace std;
void min_heapify(int a[], int i, int n)
{
int l,r,smallest,loc;
l=(2*i);
r=(2*i+1);
if((l<=n) && (a[l]>a[i]))
smallest=i;
else
smallest=l;
if((r<=n) && (a[smallest]>a[r]))
smallest=r;
if(smallest != i)
{
loc=a[i];
a[i]=a[smallest];
a[smallest]=loc;
min_heapify(a,smallest,n);
}
}
void build_min_heap(int a[], int n)
{
for(int k=n/2;k>=1;k--)
{
min_heapify(a,k,n);
}
}
void heapsort(int a[], int n)
{
build_min_heap(a,n);
int temp,i;
for(i=n;i>=2;i--)
{
temp=a[i];
a[i]=a[1];
a[1]=temp;
min_heapify(a,1,i-1);
}
}
main()
{
cout<<"Enter the number of elements\n";
int n;
cin>>n;
cout<<"Enter the numbers\n";
int a[n];
for(int i=1;i<=n;i++)
cin>>a[i];
heapsort(a,n);
cout<<"::::::::::::::::The Sorted List Is::::::::::::::\n";
for(int i=1;i<=n;i++)
cout<<a[i]<<endl;
return 0;
}

Related

Determinant of n*n matrix using c++ -not getting correct output

The code given below is a code for n*n matrix however on executing this code gives garbage values as output for n>=3.Kindly suggest me why?Also if possibly can somebody tell me how i can return 2D array from a function in c++.Thanks in advance.
#include<iostream>
using namespace std;
void rowcol(int[][10],int[][10],int,int);
int det(int arr[][10],int n)
{
int cofact[10][10];
if(n==2)
return (arr[0][0]*arr[1][1])-(arr[0][1]*arr[1][0]);
int val=0,flg=1;
for(int i=0;i<n;i++)
{
rowcol(arr,cofact,i,n);
val=val+(flg*det(cofact,n-1));
flg*=-1;
}
return val;
}
void rowcol(int arr[][10] ,int cofact[][10],int l,int n)
{
for(int i=0;i<n;i++)
{
for(int j=0;j<n;j++)
{
if((i!=l)&&(j!=l))
cofact[i][j]=arr[i][j];
}
}
}
int main()
{
int n,val;
int arr[10][10];
cout<<"Enter the size of matrix:";
cin>>n;
cout<<"\nEnter the array elements row by row";
for(int i=0;i<n;i++)
{
cout<<"\nEnter Row "<<i<<":";
for(int j=0;j<n;j++)
{
cin>>arr[i][j];
}
}
val=det(arr,n);
cout<<"\nThe determinant of given matrix is:"<<val;
return 0;
}

What is wrong with my code in finding the length of Maximum Bitonic Subarray

Logic used is simple if a triplet x,y,z occurs such that x>y<z then give the length of the array to m and continue and it gives right output in all the test cases I can think of
#include<iostream>
#include<algorithm>
using namespace std;
int main()
{
int t;
cin>>t;
while(t--)
{
int n;
cin>>n;
int a[n];
for(int i=0;i<n;i++)
{
cin>>a[i];
}
int l=0;
int m=0;
for(int i=0;i<n;i++)
{
l++;
if(i>0&&i<n-1)
{
if(a[i]<a[i-1]&&a[i]<a[i+1])
{
l=1;
}
}
if(m<l)
m=l;
}
if(m)
cout<<m<<endl;
else
cout<<l<<endl;
}
return 0;
}

Output is trash value

This is my code. Is any wrong? This project is about sorting in descending, I did check some samples code on internet, but it's pretty same as me. But My code always print trash value memory.
#include <iostream>
using namespace std;
void input(int a[], int n)
{
for( int i=0;i<n;i++)
{
cin>>a[i];
}
}
void sort(int a[], int n)
{
int i; int j; int temp;
for (i=0;i<n-1;i++)
for( j=i+1;i<n;j++)
{
if(a[i]<a[j])
{
temp =a[i];
a[i]=a[j];
a[j]=temp;
}
}
}
int output(int a[], int n)
{
for ( int i=0;i<n;i++)
{
cout<<a[i]<<' ';
}
}
int main()
{
int n;
cin>>n;
int a[n];
input(a,n);
sort(a,n);
output(a,n);
return 0;
}
please change i<n to j<n on your inner for loop in sort function.It might helps :)
#include <iostream>
using namespace std;
void input(int a[], int n)
{
for( int i=0;i<n;i++)
{
cin>>a[i];
}
}
void sort(int a[], int n)
{
int i; int j; int temp;
for (i=0;i<n;i++)
for(j=i+1;j<n;j++)
{
if(a[i]<a[j])
{
temp =a[i];
a[i]=a[j];
a[j]=temp;
}
}
}
int output(int a[], int n)
{
for ( int i=0;i<n;i++)
{
cout<<a[i]<<' ';
}
}
int main()
{
int n;
cin>>n;
int a[n];
input(a,n);
sort(a,n);
output(a,n);
return 0;
}
Now If you want second max value, you can get from a[n-2].

How to print an array in descending order?

Basically this is my code and it works fine. I just don't know how to print it in descending order. This code basically shows the odd numbers:1,3,5,7. I want it to be printed 7,5,3,1. I know I need use the sort function but I dont know how.
#include <iostream>
using namespace std;
void fillArray(int arr[], int &n);
void printArray(int arr[], int n);
void findSum(int arr[], int &n);
int main()
{
int n;
cin>>n;
int arr[n];
fillArray(arr,n);
printArray(arr,n);
findSum(arr,n);
return 0;
}
void fillArray(int arr[], int &n)
{
int j=1;
for(int i=0;i<n;i++)
{
if(j%2==1)
arr[i]=j;
else
i--;
j++;
}
}
void printArray(int arr[], int n)
{
for(int i=0;i<n;i++)
{
cout<<arr[i]<<", ";
}
}
void findSum(int arr[], int &n)
{
int sum=0;
for(int i=0;i<n;i++)
{
sum=sum+arr[i];
}
}
for(int i = n-1; i >= 0; i--)
{
cout << arr[i] << ", ";
}
example:
void printArray(int *tab, int size)
{
for (int i = size - 1; i >= 0; i--)
std::cout << tab[i] << std::endl;
}
int main() {
int tab[3] = { 1,2,3 };
printArray(tab, 3);
}
You should begin from last element array, and decrement iterator (i) to i == 0
You can simply use a sort function. There is one included with the algorithm header.
#include <algorithm> // this goes at the top
void printArray(int arr[], int n)
{
sort(arr, arr+n, [](int x, int y){return y<x;});
for(int i=0;i<n;i++)
cout << arr[i] << endl;
}
the [](int x, int y){return y<x;} part is just to make it descending. Normally it is y>x, at which point you can just omit the third parameter
Here is a repl:
https://repl.it/JQor/0

Trying to implement heapify procedure , could not find error where I am wrong

I am using a simple approach to convert an array into heap in bottom up manner. I could not find the mistake. Can some one please have a look and let me know.
Basically I am making out of a random array in bottom up manner.
for(int i=n/2;i>=1;i--)
{
heapify(i,n,arr);
}
There is some issue with heapify procedure ,which sometimes give correct result and most of times wrong.
`#include<iostream>
#include<conio.h>
using namespace std;
int min(i
nt x,int y,int z)
{
return ((x>y?x:y)>z?(x>y?x:y):z);
}
int left(int i)
{
return (2*i);enter code here
}
int right(int i)
{
return (2*i+1);
}
int parent(int i)
{
return (i/2);
}
int heapify(int i,int n,int arr[])
{
int temp;
int l=left(i);
int r=right(i);
temp=i;
if(l<=n&&arr[l]<arr[i])
temp=l;
else if(r<=n&&arr[temp]>arr[r])
temp=r;
if(temp!=i)
{
int x=arr[temp];
arr[temp]=arr[i];
arr[i]=x;
heapify(temp,n,arr);
}
//else return 0;
}
int deletemin(int n,int arr[])
{
cout<<arr[1]<<"\ndeLeted\n";
arr[1]=arr[n];
}
int insert(int x,int size,int arr[])
{
arr[size]=x;
int i=(size);
do
{
i=parent(i);
if(arr[i]!=min(arr[i],arr[left(i)],arr[right(i)]))
heapify(i,(size),arr);
else
return 0;
}while(i!=1);
}
int main()
{
int n,t,arr[100];
cin>>t;
while(t--)
{
cin>>n;
for(int i=1;i<=n;i++)
cin>>arr[i];
for(int i=n/2;i>=1;i--)
{
heapify(i,n,arr);
}
for(int i=1;i<=n;i++)
cout<<arr[i]<<" ";
deletemin(n,arr);
n--;
heapify(1,n,arr);
for(int i=1;i<=n;i++)
cout<<arr[i]<<" ";
int y;
cout<<"what value do you want to insert\n";
cin>>y;
n++;
insert(y,n,arr);
for(int i=1;i<=n;i++)
cout<<arr[i]<<" ";
}
getch();
return 0;
}
`
One simple observation is that your min function is wrong! It doesn't return the min of 3 numbers. Do it with ifs for the moment!