您的位置:首页 > 其它

Foundation: Heapsort

2013-09-21 18:25 281 查看
/* Heap Sorting.
* Implementation history:.
* 2013-09-20, Mars Fu, first version.
*/

/* [Heapsort Algorithm]
* Published by J.W.J.Williams[CACM 7(1964),347-348].
*
* [Heap Creation Algorithm]
* Part of Heapsort algorithm.
* Published by R.W.Floyd[CACM 7(1964),701]
*/

#include "stdafx.h"
#include "heapsort.h"

/* Rocords R(1),...,R(N) are rearranged in place.
* After sorting is complete,their keys will be in order, K(1)<=...<=K(N).
* First we rearrange the file so that it forms a heap,then we repeatedly remove
* the top of the heap and transfer it to its proper final position.
* Assume that N>=2.
*/
int
do_heap_sort(void *src, int src_sz, int n, cmp_func cmp, exc_func exchange, dbg_func dbg)
{
int L,r;
int i,j;

F_S();

if (src == NULL || cmp == NULL || exchange == NULL) return 0;
if (src_sz <= 0 || n <= 0) return 0;
if (n == 1) goto END;

H1:
/* initialize.
*/
L = n/2 + 1;
r = n;

H2:
if (dbg) dbg(src,n,L,r);

/* descrease L or r.
*/
if (L > 1) {
--L;
}
else {
exchange((char*)src + (r-1) * src_sz, (char*)src);
if(--r == 1) goto END;
}

H3:
/* prepare for siftup.
*/
j = L;

H4:
/* advance downward.
*/
i = j;
j <<= 1; /* j= 2*j. */

if (j < r) goto H5;
if (j == r) goto H6;
if (j > r) goto H8;

H5:
/* find larger child.
*/
if (cmp((char*)src + (j-1)*src_sz, (char*)src + (j+1-1)*src_sz) < 0) ++j;

H6:
/* larger than R(i)?
*/
if (cmp((char*)src + (i-1)*src_sz, (char*)src + (j-1)*src_sz) >= 0) goto H8;

H7:
/* move it up
*/
exchange((char*)src + (i-1)*src_sz, (char*)src + (j-1)*src_sz);
goto H4;

H8:
/* the end of shiftup.
*/
goto H2;

END:
if (dbg) dbg(src,n,L,r);

F_E();
return 1;
}

#ifdef HEAP_SORT_DEBUG

static int
int_increasing_cmp(void* lv, void* rv)
{
int tmp_lv, tmp_rv;

tmp_lv = *(int*)lv;
tmp_rv = *(int*)rv;

return (tmp_lv - tmp_rv);
}

static void
exchange_int_item(void* lv, void* rv)
{
int tmp_lv, tmp_rv;

tmp_lv = *(int*)lv;
tmp_rv = *(int*)rv;

*(int*)lv = *(int*)rv;
*(int*)rv = tmp_lv;
}

static void
debug_func(void*src, int n, int L, int r)
{
int i;

debug("\r\n");
for (i = 0; i < n; ++i) {
debug("% 4d ", i+1);
}
debug("L r");
debug("\r\n");

for (i = 0; i < n; ++i) {
debug("% 4d ", *((int*)src + i));
}
debug("%d %d", L, r);
debug("\r\n");
}

int
main(int argc, char* argv[])
{
int i;
int cnt;
const int int_items[] = { 503, 87, 512, 61, 908, 170, 897, 275,
653, 426, 154, 509, 612, 677, 765, 703
};
int ret;

debug("[Testing heap sort].. \r\n");

cnt = sizeof(int_items)/sizeof(int_items[0]);

debug("src database:\r\n----\r\n");
for (i = 0; i < cnt; ++i) {
debug("%d ", int_items[i]);
}
debug("\r\n----\r\n");

ret = do_heap_sort((void*)int_items, sizeof(int), cnt,
int_increasing_cmp, exchange_int_item, debug_func);
if (!ret) {
debug("failed. \r\n");
goto END;
}

debug("dst database:\r\n----\r\n");
for (i = 0; i < cnt; ++i) {
debug("%d ", int_items[i]);
}
debug("\r\n----\r\n");

debug("\r\n");

debug("[Testing heap sort].. done. \r\n");

END:
while(1);
return 0;
}

#endif /* HEAP_SORT_DEBUG */

#ifndef __HEAPSORT_H__
#define __HEAPSORT_H__

//#define HEAP_SORT_DEBUG

typedef int(*cmp_func)(void*, void*);
typedef void (*exc_func)(void*, void*);
typedef void (*dbg_func)(void*, int, int, int);

int do_heap_sort(void *src, int src_sz, int n, cmp_func cmp, exc_func exchange, dbg_func dbg);

#endif /* __HEAPSORT_H__ */

#pragma once

#include <windows.h>
#ifdef _WIN32
#define msleep(x)  Sleep(x)
#endif

#include <stdio.h>
#include <tchar.h>
#include <stdlib.h>
#include <malloc.h>
#include <string.h>
#include <math.h>

#define MY_DEBUG
#ifdef MY_DEBUG
#define debug printf
#else
#define debug(x,argc, __VA_ARGS__)	;
#endif /* MY_DEBUG */

#define F_S() debug("[%s]..\r\n", __FUNCTION__)
#define F_E() debug("[%s]..done. \r\n", __FUNCTION__)








I guess your guys may think it's a little complicated? :) so,so...

Enjoy!

Mars

Sep 21st, 2013

Any to share, e-me:
mars.fu@foxmail.com
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息