`
yaojingguo
  • 浏览: 209040 次
  • 性别: Icon_minigender_1
  • 来自: 北京
社区版块
存档分类
最新评论

Heapsort in C

阅读更多

 

#include <stdio.h>

void info(int A[], int len) {
  int i;
  for (i = 0; i < len; i++)
    printf("%d ", A[i]);
  printf("\n");
}

void swap(int *a, int *b) {
  int temp = *a;
  *a = *b;
  *b = temp;
}

void max_heapify(int A[], int i, int len) {
  int largest;
  int left = ((i+1) << 1) - 1;
  if (left < len && A[left] > A[i]) 
    largest = left;
  else
    largest = i;
  int right = (i+1) << 1;
  if (right < len && A[right] > A[largest])
    largest = right;
  if (largest != i) {
    swap(&A[i], &A[largest]);
    max_heapify(A, largest, len);
  }
}

void build_max_heap(int A[], int len) {
  int i;
  for (i = len/2 - 1; i >= 0; i--)
    max_heapify(A, i, len);
}

void heapsort(int A[], int len) {
  int i;
  build_max_heap(A, len);
  for (i = len - 1; i >= 1; i--) {
  swap(&A[0], &A[i]);
  max_heapify(A, 0, i);
  }
}

void test(int A[], int len) {
  heapsort(A, len);
  info(A, len);
}

int main(int argc, const char *argv[]) {
  int a3[] = {1, 10, 7};
  test(a3, 3);
  int a5[] = {1, 10, 6, 20, 9};
  test(a5, 5);
  int a6[] = {12, 23, 2, 20, 100, 4};
  test(a6, 6);
  return 0;
}
分享到:
评论

相关推荐

    Advanced Topics in C Core Concepts in Data Structures

    《C高级主题:数据结构核心概念》这本书深入探讨了任何有志于成为程序员的人应该掌握的概念。书中涵盖了排序、搜索、合并、递归、随机数及模拟等多种主题。当读者学会如何操作灵活且广泛应用的数据结构,例如二叉树...

    Advanced Topics in C.pdf

    Series: Expert's Voice in C Paperback: 312 pages Publisher: Apress; 1 edition (October 29, 2013) Language: English ISBN-10: 1430264004 ISBN-13: 978-1430264002 C is the most widely used programming ...

    [Advanced.Topics.in.C(2013.11)].Noel.Kalicharan.pdf

    《Advanced Topics in C》还将涉及一些高级排序方法,例如堆排序(heapsort)、快速排序(quicksort)和归并排序(mergesort)。通过学习如何使用C语言实现上述内容,读者将会对C语言有一个更加深入的理解和掌握。...

    编程珠玑全部源代码 分享

    The column also uses sort.c (Column 11) for heapsort. Column 15: Strings wordlist.cpp -- List words in the file, using STL set. wordfreq.cpp -- List words in the file, with counts, using STL map. ...

    c语言数据结构算法演示(Windows版)

     堆排序(HeapSort)  快速排序(QuickSort)  锦标赛排序(Tournament) (3)其他  快速地址排序(QkAddrst)  基数排序(RadixSort) 13. 外部排序 (1)多路平衡归并排序(K-Merge) (2)置换-选择排序...

    Data-Structures-and-Algorithms-in-C:C用C语言实现的一些著名且非常基础的算法和数据结构

    用C实现的数据结构和算法在这里,您会发现一些用C语言实现的数据结构和算法。这些算法主要基于Thomas H. Cormen撰写的《算法简介》一书。指示每个模块至少包含一个头文件(.h)和一个包含代码主体(.c)的源文件。 ...

    C语言写的各种排序算法

    根据提供的文件信息,本文将详细介绍该C语言程序中所实现的六种排序算法:直接插入排序、希尔排序、起泡排序、快速排序、直接选择排序以及堆排序。 ### 1. 直接插入排序(Insertion Sort) 直接插入排序是一种简单...

    [麻省理工学院-算法导论].Introduction.to. Algorithms,.Second.Edition

    Chapter 6 - Heapsort Chapter 7 - Quicksort Chapter 8 - Sorting in Linear Time Chapter 9 - Medians and Order Statistics Part III - Data Structures Chapter 10 - Elementary Data Structures ...

    算法导论--Introduction.to.Algorithms

    "In light of the explosive growth in the amount of data and the diversity of computing applications, efficient algorithms are needed now more than ever. This beautifully written, thoughtfully ...

    麻省理工算法导论(完整精辟版)

    Chapter 6 - Heapsort Chapter 7 - Quicksort Chapter 8 - Sorting in Linear Time Chapter 9 - Medians and Order Statistics Part III - Data Structures Chapter 10 - Elementary Data Structures ...

    【麻省理工大学】算法导论

    Chapter 6 - Heapsort Chapter 7 - Quicksort Chapter 8 - Sorting in Linear Time Chapter 9 - Medians and Order Statistics Part III - Data Structures Chapter 10 - Elementary Data Structures ...

    [麻省理工学院-算法导论](英文版).chm

    Chapter 6 - Heapsort Chapter 7 - Quicksort Chapter 8 - Sorting in Linear Time Chapter 9 - Medians and Order Statistics Part III - Data Structures Chapter 10 - Elementary Data Structures ...

    学习数据结构算法必备

     堆排序(HeapSort)  快速排序(QuickSort)  锦标赛排序(Tournament) (3)其他  快速地址排序(QkAddrst)  基数排序(RadixSort) 13. 外部排序 (1)多路平衡归并排序(K-Merge) (2)置换-选择排序...

    算法导论(英文第2版)

    Chapter 6 - Heapsort Chapter 7 - Quicksort Chapter 8 - Sorting in Linear Time Chapter 9 - Medians and Order Statistics Part III - Data Structures Chapter 10 - Elementary Data Structures ...

    算法导论英文版

    l The Role of Algorithms in Computing 5 l.l Algorithms 5 l.2 Algorithms as a technology 10 2 Getting Started I5 2.l Insertion sort 15 2.2 Analyzing algorithms 21 2.3 Designing algorithms 27 3 Growth ...

    堆排序(R语言)

    arr &lt;- c(12, 11, 13, 5, 6, 7) print("原始数组:", arr) heapSort(arr) print("排序后的数组:", arr) ``` 这个示例首先定义了`heapify`函数用于调整堆,`heapSort`函数实现了整个排序过程,`swap`函数则用于交换...

Global site tag (gtag.js) - Google Analytics