D堆的實現,D堆實現
實現上一篇部落格(http://blog.csdn.net/buleriver/article/details/38469977)說的D堆,如果把mD設定成2,D堆就退化成二元堆積,也就是說,二元堆積是D堆的一種情況。
public class DHeap { public static final int INIT_CAPACITY = 10; private int[] mArray; private int mLength; private final int mD; public DHeap(int d) { mArray = new int[INIT_CAPACITY + 1]; mLength = 0; mD = d; } public int getParentIndex(int index) { return (index - 2 + mD) / mD; } public int getChildIndex(int pIndex, int childIndex) { return mD * (pIndex - 1) + 2 + childIndex; } private void expandArray(int length) { int[] arr = new int[length]; System.arraycopy(mArray, 1, arr, 1, mLength); mArray = arr; } /** * 自下而上,把value放到最後一個,然後一直和其父節點交換,找到合適的位置 */ public void insert(int value) { if (mLength >= mArray.length - 1) { expandArray(mArray.length * 2); } mArray[++mLength] = value; int index = mLength; int parentIndex = getParentIndex(index); while (parentIndex > 0) { int currentValue = mArray[index]; int parentValue = mArray[parentIndex]; if (currentValue < parentValue) { mArray[parentIndex] = currentValue; mArray[index] = parentValue; index = parentIndex; parentIndex = getParentIndex(index); } else { break; } } } public void deleteMin() { if (mLength <= 0) { return; } else if (mLength == 1) { mLength--; } else { mArray[1] = mArray[mLength]; int index = 1; mLength--; while (true) { int value = mArray[index]; int minIndex = -1; // 最小孩子的數組索引 boolean lastLevel = false; // 是否已經到了最底層 int firstChildIndex = getChildIndex(index, 0); int lastChildIndex = firstChildIndex + mD; for (int childIndex = firstChildIndex; childIndex < lastChildIndex; childIndex++) { // 找到最小的孩子 if (childIndex > mLength) { // 已經到了最後一個 lastLevel = true; break; } int childValue = mArray[childIndex]; if (value > childValue) { value = childValue; minIndex = childIndex; } } if (minIndex < 0) { // 已經符合d堆的性質,不需要置換了 break; } else { // 需要置換 mArray[minIndex] = mArray[index]; mArray[index] = value; index = minIndex; } if (lastLevel) { // 已經到了最底層 break; } } } } }
對於入棧順序為A,B,C,D,輸出其所有可可以的出棧序列(用堆棧來實現)
a,b,c,d
d,c,b,a
b,c,d,a
c,d,b,a
a,d,c,b
a,c,b,d
a,b,d,c
c,b,d,a
c,b,a,d
b,a,c,d
b,a,d,c
堆排序演算法的實現
#include<stdio.h>
#include<malloc.h>
#include<time.h>
#define LISTSIZE 100
#define MORESIZE 100
#define overflow -1
typedef struct
{
int data;
int fre;
}Cell;
typedef struct {
Cell *elem;
long int length;
unsigned long int count1;
unsigned long int count2;
long int listsize;
}SqList;
SqList L1;
clock_t start,end;
FILE *p,*w;
int main (void)
{
void assign(Cell *a,Cell *b);
int LT(int a,int b);
void HeapSort (SqList &H);
void HeapAdjust (SqList &H,int s , int m);
void exchange(Cell *a,Cell *b);
//讀入
int time=0;
while(time<4)
{
switch (time)
{
case 0:
p=fopen("data01.txt","r");
w=fopen("sorted01.txt","w");
break;
case 1:
p=fopen("data02.txt","r");
w=fopen("sorted02.txt","w");
break;
case 2:
p=fopen("data03.txt","r");
w=fopen("sorted03.txt","w");
break;
case 3:
p=fopen("data04.txt","r");
w=fopen("sorted04.txt","w");
break;
}
L1.count1=0;
L1.count2=0;
time++;
L1.elem=(Cell *)malloc((LISTSIZE+1)*sizeof(Cell));
L1.listsize=LISTSIZE;
L1.length=1;
Cell *newbase;
while(!feof(p))
{
if (L1.length>L1.listsize){
newbase=(Cell *)realloc(L1.elem,(L1.listsize+MORESIZE+1)*sizeof(Cell));
if (!newbase)
return overflow;
L1.elem=newbase;
L1.listsize+=MORESIZE;}
fscanf (p,"%d (%d)\n&qu......餘下全文>>