算法学习入门之使用C语言实现各大基本的排序算法

2020-01-06 14:01:56王冬梅

堆的更详细描述请阅读参考书目。

下面是堆的数据结构,以及插入节点和删除根节点操作。你可以很方便的构建堆,并取出根节点,构成有序数组。


/* By Vamei 
  Use an big array to implement heap
  DECLARE: int heap[MAXSIZE] in calling function
  heap[0] : total nodes in the heap
  for a node i, its children are i*2 and i*2+1 (if exists)
  its parent is i/2 */

void insert(int new, int heap[]) 
{
  int childIdx, parentIdx;
  heap[0] = heap[0] + 1;
  heap[heap[0]] = new;
  
  /* recover heap property */
  percolate_up(heap);
}

static void percolate_up(int heap[]) {
  int lightIdx, parentIdx;
  lightIdx = heap[0];
  parentIdx = lightIdx/2;
  /* lightIdx is root? && swap? */
  while((parentIdx > 0) && (heap[lightIdx] < heap[parentIdx])) {
    /* swap */
    swap(heap + lightIdx, heap + parentIdx); 
    lightIdx = parentIdx;
    parentIdx = lightIdx/2;
  }
}


int delete_min(int heap[]) 
{
  int min;
  if (heap[0] < 1) {
    /* delete element from an empty heap */
    printf("Error: delete_min from an empty heap.");
    exit(1);
  }

  /* delete root 
    move the last leaf to the root */
  min = heap[1];
  swap(heap + 1, heap + heap[0]);
  heap[0] -= 1;

  /* recover heap property */
  percolate_down(heap);
 
  return min;
}

static void percolate_down(int heap[]) {
  int heavyIdx;
  int childIdx1, childIdx2, minIdx;
  int sign; /* state variable, 1: swap; 0: no swap */

  heavyIdx = 1;
  do {
    sign   = 0;
    childIdx1 = heavyIdx*2;
    childIdx2 = childIdx1 + 1;
    if (childIdx1 > heap[0]) {
      /* both children are null */
      break; 
    }
    else if (childIdx2 > heap[0]) {
      /* right children is null */
      minIdx = childIdx1;
    }
    else {
      minIdx = (heap[childIdx1] < heap[childIdx2]) ?
             childIdx1 : childIdx2;
    }

    if (heap[heavyIdx] > heap[minIdx]) {
      /* swap with child */
      swap(heap + heavyIdx, heap + minIdx);
      heavyIdx = minIdx;
      sign = 1;
    }
  } while(sign == 1);
}

 

总结

除了上面的算法,还有诸如Bucket Sorting, Radix Sorting涉及。我会在未来实现了相关算法之后,补充到这篇文章中。相关算法的时间复杂度分析可以参考书目中找到。我自己也做了粗糙的分析。如果博客 园能支持数学公式的显示,我就把自己的分析过程贴出来,用于引玉。

上面的各个代码是我自己写的,只进行了很简单的测试。如果有错漏,先谢谢你的指正。

最后,上文中用到的交换函数为:


/* By Vamei */
/* exchange the values pointed by pa and pb*/
void swap(int *pa, int *pb)
{
  int tmp;
  tmp = *pa;
  *pa = *pb;
  *pb = tmp;
}