i have following code for heapsort
#include <iostream>
using namespace std;
void exch(int a[],int i,int j){
int s=a[i];
a[i]=a[j];
a[j]=s;
}
void sink(int a[],int k,int n){
//int n=sizeof(a)/sizeof(int);
while(2*k<=n){
int j=2*k;
if (j<n && (a[j]<a[j+1])) j++;
if (a[k]>=a[j]) break;
exch(a,k,j);
k=j;
}
}
void heapsort(int a[]){
int n=sizeof(a)/sizeof(int);
for (int k=n/2;k>=1;k--)
sink(a,k,n);
while(n>1){
exch(a,1,n--);
sink(a,1,n);
}
}
int main(){
int a[]={12,3,5,1,67,10,9.20};
int n=sizeof(a)/sizeof(int);
heapsort(a);
for (int i=0;i<n;i++){
cout<<a[i]<<" ";
}
return 0;
}
but result it shows me looks like this
12 3 5 1 67 10 9 Press any key to continue . . .
also look that in my array total number is 8 and here it shows me 7 as output, i think core of this problem should be this
1>c:\users\datuashvili\documents\visual studio 2010\projects\heap\heap\heap.cpp(36): warning C4244: 'initializing' : conversion from 'double' to 'int', possible loss of data
it seams that possible loss of data while converting from double to int force code works incorrectly,am i correct or wrong?please help me
In addition to all the other comments (
sizeof(a), passing[]as arguments, having a.in the init-list), you want to write C++-code, right? But it looks quite C’ish, except for theiostream. Thereore:std::vector<int>from#include <vector>(add elements withpush_backfor example)void heapsort(std::vector<int> &a)std::swap( a[i], a[j] );instead ofexch