7/14/09

c samples program

In this c sample program compile the following codes and see the output

/*
PROG: butter
LANG: C++
*/

download c_heap_sort
//In the name of God
#include
#include
#include
using namespace std;

const int P = 800 + 10, N = 500 + 10, inf = P * N * 225;
int h[P], nh = 0, ver[P], ind[P], t[30], hh;
vector v[P];

inline void my_swap (int x, int y) {
swap (ver[x], ver[y]);
swap (ind[ ver[x] ], ind[ ver[y] ]);
swap (h[x], h[y]);
}

//x == index
void bobble_up (int x) {
if (x != 1 && h[x] < x ="="" mc =" x;"> h[x*2] && x*2<=nh) mc = x*2; if (h[mc] > h[x*2 + 1] && x*2+1 <= nh) mc = x*2+1; if (mc != x) { my_swap (x, mc); bobble_down (mc); } } //x != index && x == value inline void insert_key (int x) { h[++nh] = x; bobble_up (nh); } inline int extract_min () { int r = h[1]; my_swap(1, nh--); bobble_down(1); return r; } //x == index inline void modifi_key (int x, int val) { h[x] = val; bobble_up (x); bobble_down (x); } int main () { int x; while (cin >> x) {
insert_key (x);
t[hh++] = x;
cerr << x << endl;
}
cerr << "hello\n";
sort (t, t+hh);
/*for (int i=1; i
cerr << h[i] << " ";
cerr << endl;*/
for (int i=0; i
cerr << t[i] << " ";
cerr << endl;
while(nh)
cerr << extract_min () << " ";
cerr << endl;
return 0;
}

copy the code above
The end of the c sample program
for more samples see other posts in the blog
please leave messages and comment below

5 comments:

  1. This comment has been removed by a blog administrator.

    ReplyDelete

leave me messege