7-40-11-27-23.10.c

來源:互聯網
上載者:User

  這個堆排序使用了新的下濾函數.對於已耗用時間的分析,暫時進行不了,回頭計算下吧.哪怕是很粗略的依賴於時鐘計時器.

/*7-40-11-27-23.10.c -- 第七章第四十題*/<br />#include <stdio.h><br />#define SIZE 20</p><p>int main (void) ;<br />void print_array (const int * const array, const int size) ;<br />void swap (int * const vala, int * const valb) ;<br />void percolate_up (int * const array, const int index, const int size) ;<br />void percolate_down (int * const array, const int index, const int size) ;<br />void build_heap (int * const array, const int size) ;<br />void heap_sort (int * const array, const int size) ;</p><p>int main (void)<br />{<br />int array[SIZE] = {0, 3, 1, 4, 2, 6, 9, 5, 7, 8, 13, 12, 11, 14, 10, 19, 15, 17, 18, 16} ;<br />int size = SIZE ;</p><p>print_array (array, size) ;<br />heap_sort (array, size) ;<br />print_array (array, size) ;</p><p>return 0 ;<br />}</p><p>void print_array (const int * const array, const int size)<br />{<br />int i ;</p><p>for (i = 0; i < size; i++)<br />printf ("%-2d ", array[i]) ;<br />putchar ('/n') ;<br />}</p><p>void swap (int * const vala, int * const valb)<br />{<br />int temp ;</p><p>temp = *vala ;<br />*vala = *valb ;<br />*valb = temp ;<br />}</p><p>void build_heap (int * const array, const int size)<br />{<br />int i ;</p><p>for (i = size / 2; i >= 0; i--)<br />percolate_down (array, i, size) ;<br />}</p><p>void percolate_up (int * const array, const int index, const int size)<br />{<br />int i, value ;</p><p>value = array[index] ;<br />for (i = index; i >= 1; i = (i - 1) / 2)<br />{<br />if (array[(i - 1) / 2] < value)<br />array[i] = array[(i - 1) / 2] ;<br />else<br />break ;<br />}<br />array[i] = value ;<br />}</p><p>void percolate_down (int * const array, const int index, const int size)<br />{<br />int child, i, value ;</p><p>value = array[index] ;<br />for (i = index; i * 2 + 1 < size; i = child)<br />{<br />child = i * 2 + 1 ;<br />if (child != size - 1 && array[child + 1] > array[child])<br />child++ ;<br />array[i] = array[child] ;<br />}<br />array[i] = value ;<br />percolate_up (array, i, size) ;<br />}</p><p>void heap_sort (int * const array, const int size)<br />{<br />int i ;</p><p>build_heap (array, size) ;</p><p>for (i = size - 1; i > 0; i--)<br />{<br />swap (array, array + i) ;<br />percolate_down (array, 0, i) ;<br />}<br />}

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.