displaying the output

Im having trouble displaying the output for a heap sort i coded
Note:Im displaying the relevant code.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
#include<iostream>
#include<iomanip>
#include<cstdlib>
#include<fstream>

using std::rand;
using std::srand;
using namespace std;

int BubbleSort(int numbers[],int arraysize);
int insertion_sort(int numbers[], int arraysize);
int quickSort(int numbers[], int array_size);
int q_sort(int a[], int left, int right);
int heapSort(int numbers[], int array_size);
//int siftDown(int a[], int root, int bottom);
int heapify(int numbers[],int array_size);
void siftDown(int numbers[], int start, int end);


ofstream outf("e:Proj1-Output.dat",ios::out);
ifstream inf("e:Proj1-Input.dat",ios::in);


int main()
{
    
     int arraysize = 10;
     int numbers[arraysize];
     
     srand(time(0));
       
     float start,end; 
       
     for (int i = 0; i < arraysize; i++)
     {
     numbers[i] = 1 + rand() % 100000;
     }

     int results1;
     int results2;
     int results3;
     int results4;
    
     start= clock();
     cout<<"QUICK:: ";
     results1= quickSort(numbers,arraysize);
     cout<<endl;
     end= clock();
     outf<<(end-start)/CLOCKS_PER_SEC<<" seconds"<<endl<<endl;
      
      
     start= clock();
     cout<<"INSERTION:: ";
     results2= insertion_sort(numbers,arraysize);      
     cout<<endl;
     end= clock();
     outf<<(end-start)/CLOCKS_PER_SEC<<" seconds"<<endl<<endl;
      
     start= clock();
     cout<<"BUBBLE:: ";
     results3= BubbleSort(numbers,arraysize);
     cout<<endl;
     end= clock();
     outf<<(end-start)/CLOCKS_PER_SEC<<" seconds"<<endl<<endl;
     
     start= clock();
     cout<<"HEAP:: ";
     results4= heapSort(numbers,arraysize);
     cout<<endl;
     end= clock();
     outf<<(end-start)/CLOCKS_PER_SEC<<" seconds"<<endl<<endl;
     
     cin>>results1;
     cout<<results1<<endl;
       

     cin>>results2;
     cout<<results2<<endl;
      
     
     cin>>results3;
     cout<<results3<<endl;
     
      
     cin>>results4;
     cout<<results4<<endl;
      
      return 0;
}    


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
int heapSort(int a[], int array_size)
{
      int i;
      int end;    
      heapify(a, array_size);
      end= array_size- 1;
      while (end > 0)
      {
      swap(a[end], a[0]);
      end= end - 1;
      siftDown(a, 0, end);
      for(i=0; i<array_size; i++)
     cout<<setw(7)<<a[i];
      }
}
int heapify(int a[],int array_size)
{
    int start;
    while(start>0)
    {
     start= (array_size- 2) / 2;
     siftDown(a, start, array_size-1);
     start = start - 1;
     }
}
void siftDown(int a[], int start,int end)
{
    int root;
    int child;
    root=start;
    while (root * 2 + 1 <= end)
    {
          child= root * 2 + 1;
          if ((end= child + 1) && (a[child] < a[child + 1]))
          {
             child = child + 1 ; 
          }
          if (a[root] < a[child])
          { 
             swap(a[root], a[child]);
             root = child;  
          }
          else
          {
              return;
          }  
    }
}


anyone know what im doing wrong?
I don't have enough information to help you. What was the result of your debugging effort? The program itself looks like it would be easy to debug. have you stepped into the code to verify the states of the arrays and variables during the heap sort? Also, please post more specific information about the problem. You are streaming some data both to stdout as well as a file stream. Which output are you having trouble with? Provide an example of expected output vs. actual output.
try this and see what all you have done wrong..
atleast you should initialize your variables.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
#include<iostream>
#include<iomanip>
#include<cstdlib>
#include<fstream>

//using std::rand;
//using std::srand;
using namespace std;

int BubbleSort(int numbers[],int arraysize);
int insertion_sort(int numbers[], int arraysize);
int quickSort(int numbers[], int array_size);
int q_sort(int a[], int left, int right);
int heapSort(int numbers[], int array_size);
//int siftDown(int a[], int root, int bottom);
int heapify(int numbers[],int array_size);
void siftDown(int numbers[], int start, int end);


ofstream outf("e:Proj1-Output.dat",ios::out);
ifstream inf("e:Proj1-Input.dat",ios::in);


int heapify(int a[],int array_size)
{
    int start = (array_size / 2) - 1;
	for(; start >= 0; start--)
	{
		siftDown(a, start, array_size-1);
    }

	return 0;
}


void siftDown(int a[], int start,int end)
{
    int root;
    int child;
    root=start;


    while (root * 2 <= end)
    {
		if (root*2 == end)
			child = root * 2;
		else if(a[root * 2] > a[root * 2 + 1])
			child = root * 2;		
		else
			child = root * 2 + 1;


		if(a[root] < a[child])
		{
			swap(a[root], a[child]);
			root = child;
		}
		else
			return;
			
			/**
			child= root * 2 + 1;
          if ((end= child + 1) && (a[child] < a[child + 1]))
          {
             child = child + 1 ; 
          }
          if (a[root] < a[child])
          { 
             swap(a[root], a[child]);
             root = child;  
          }
          else
          {
              return;
          } 
		  */
    }
}


int heapSort(int a[], int array_size)
{
	
	int end;    
	heapify(a, array_size);
	
	//while (end > 0)
	for (end = array_size - 1; end >= 1; end--)
	{
		swap(a[end], a[0]);
		siftDown(a, 0, end - 1);		
	}

	  return 0;
}





int main()
{
    
     int arraysize = 10;
     int numbers[10];
     
   
     for (int i = 0; i < arraysize; i++)
     {
     numbers[i] = 1 + rand() % 100000;
     }

     
     cout<<"HEAP:: ";
     heapSort(numbers,arraysize);
     cout<<endl;
      
     return 0;
} 
looks you are working on linux..otherwise these lines wont even compile on windows.

1
2
int arraysize = 10;
int numbers[arraysize];


it will be good if you do this:
const int arraysize = 10;
looks you are working on linux..otherwise these lines wont even compile on windows.
Not true. It depends on the compiler, not the OS. MinGW will compile that without complaining.
It doesn't mean it's standard, though.
ah... on linux i mean gcc actually..

i was talking about MS VS IDE.. gcc wont complain.

its not a standard

correct... that's why i wrote.. "it will be good if you do this". :)
Topic archived. No new replies allowed.