常见排序算法:堆排序
1
2 /// <summary>
3 /// Heap Sorting implemented in C# and tested
4 /// </summary>
5 public class HeapSortAsending
6 {
7 public static void Sort(int[] array)
8 {
9 BuildMaxHeap(array);
10
11 for (int heapSize = array.Length - 1; heapSize >= 1; heapSize--)
12 {
13 Swap(ref array[0], ref array[heapSize]);
14 Heapify(array, 0, heapSize);
15 }
16 }
17
18 private static void Heapify(int[] array, int i, int heapsize)
19 {
20 int l = Left(i);
21 int r = Right(i);
22
23 int largest = i;
24 if ((l < heapsize) && (array[l] > array[i]))
25 largest = l;
26
27 if ((r < heapsize) && (array[r] > array[largest]))
28 largest = r;
29
30 if (largest != i)
31 {
32 Swap(ref array[i], ref array[largest]);
33 Heapify(array, largest, heapsize);
34 }
35 }
36
37 private static void BuildMaxHeap(int[] array)
38 {
39 for (int i = array.Length / 2; i >= 0; i--)
40 Heapify(array, i, array.Length);
41 }
42
43 private static void Swap(ref int a, ref int b)
44 {
45 int tmp = a;
46 a = b;
47 b = tmp;
48 }
49
50 private static int Left(int i)
51 {
52 return 2 * i;
53 }
54
55 private static int Right(int i)
56 {
57 return 2 * i + 1;
58 }
59 }
3 /// Heap Sorting implemented in C# and tested
4 /// </summary>
5 public class HeapSortAsending
6 {
7 public static void Sort(int[] array)
8 {
9 BuildMaxHeap(array);
10
11 for (int heapSize = array.Length - 1; heapSize >= 1; heapSize--)
12 {
13 Swap(ref array[0], ref array[heapSize]);
14 Heapify(array, 0, heapSize);
15 }
16 }
17
18 private static void Heapify(int[] array, int i, int heapsize)
19 {
20 int l = Left(i);
21 int r = Right(i);
22
23 int largest = i;
24 if ((l < heapsize) && (array[l] > array[i]))
25 largest = l;
26
27 if ((r < heapsize) && (array[r] > array[largest]))
28 largest = r;
29
30 if (largest != i)
31 {
32 Swap(ref array[i], ref array[largest]);
33 Heapify(array, largest, heapsize);
34 }
35 }
36
37 private static void BuildMaxHeap(int[] array)
38 {
39 for (int i = array.Length / 2; i >= 0; i--)
40 Heapify(array, i, array.Length);
41 }
42
43 private static void Swap(ref int a, ref int b)
44 {
45 int tmp = a;
46 a = b;
47 b = tmp;
48 }
49
50 private static int Left(int i)
51 {
52 return 2 * i;
53 }
54
55 private static int Right(int i)
56 {
57 return 2 * i + 1;
58 }
59 }
1 /*
2 ============================================================================
3 Name : HeapSort.c
4 Author : Gump Yin
5 Version : 1.0
6 Copyright : http://www.cnblogs.com/BpLoveGcy;http://zh.wikipedia.org/zh-cn/%E5%A0%86%E7%A9%8D%E6%8E%92%E5%BA%8F
7 Description : Heapsort Implemented by C
8 ============================================================================
9 */
10
11 #include<stdio.h>
12 #include<stdlib.h>
13 #include<math.h>
14 #define HEAP_SIZE 13
15 int left(int i);
16 int right(int i);
17
18 void Max_Heapify(int A[], int i, int heap_size);
19 void Build_Max_Heap(int A[]);
20 void HeapSort(int A[], int heap_size);
21 void Swap(int *a, int *b);
22
23 void print(int A[]);
24
25 // left child
26 int left(int i) {
27 return 2 * i;
28 }
29
30 // right child
31 int right(int i) {
32 return 2 * i + 1;
33 }
34
35 void Swap(int *a, int *b) {
36 int tmp = *a;
37 *a = *b;
38 *b = tmp;
39 }
40
41 void Max_Heapify(int A[], int i, int heap_size) {
42 int l = left(i);
43 int r = right(i);
44
45 int largest = i;
46 if (l < heap_size && A[l] > A[i]) {
47 largest = l;
48 }
49
50 if (r < heap_size && A[r] > A[largest]) {
51 largest = r;
52 }
53 if (largest != i) {
54 Swap(&A[i], &A[largest]);
55 Max_Heapify(A, largest, heap_size);
56 }
57 }
58
59 void Build_Max_Heap(int A[]) {
60 int i;
61 for (i = HEAP_SIZE; i >= 0; i--) {
62 Max_Heapify(A, i, HEAP_SIZE);
63 }
64 }
65
66 void print(int A[]) {
67 int i;
68 for (i = 0; i < HEAP_SIZE; i++) {
69 printf("%d ", A[i]);
70 }
71 printf("\n");
72 }
73
74 void HeapSort(int A[], int heap_size) {
75 Build_Max_Heap(A);
76
77 int i;
78 for (i = heap_size - 1; i >= 1; i--) {
79 Swap(&A[0], &A[i]);
80 heap_size = heap_size - 1;
81 Max_Heapify(A, 0, heap_size);
82 }
83 print(A);
84 }
85
86 int main() {
87 int A[HEAP_SIZE] = { 0, 1, 10, 14, 16, 4, 7, 9, 3, 2, 8, 5, 11 };
88 //int A[HEAP_SIZE] = { 19, 1, 10, 14, 16, 4, 7, 9, 3, 2, 8, 5, 11 };
89 print(A);
90 HeapSort(A, HEAP_SIZE);
91 print(A);
92
93 return 0;
94 }
95
2 ============================================================================
3 Name : HeapSort.c
4 Author : Gump Yin
5 Version : 1.0
6 Copyright : http://www.cnblogs.com/BpLoveGcy;http://zh.wikipedia.org/zh-cn/%E5%A0%86%E7%A9%8D%E6%8E%92%E5%BA%8F
7 Description : Heapsort Implemented by C
8 ============================================================================
9 */
10
11 #include<stdio.h>
12 #include<stdlib.h>
13 #include<math.h>
14 #define HEAP_SIZE 13
15 int left(int i);
16 int right(int i);
17
18 void Max_Heapify(int A[], int i, int heap_size);
19 void Build_Max_Heap(int A[]);
20 void HeapSort(int A[], int heap_size);
21 void Swap(int *a, int *b);
22
23 void print(int A[]);
24
25 // left child
26 int left(int i) {
27 return 2 * i;
28 }
29
30 // right child
31 int right(int i) {
32 return 2 * i + 1;
33 }
34
35 void Swap(int *a, int *b) {
36 int tmp = *a;
37 *a = *b;
38 *b = tmp;
39 }
40
41 void Max_Heapify(int A[], int i, int heap_size) {
42 int l = left(i);
43 int r = right(i);
44
45 int largest = i;
46 if (l < heap_size && A[l] > A[i]) {
47 largest = l;
48 }
49
50 if (r < heap_size && A[r] > A[largest]) {
51 largest = r;
52 }
53 if (largest != i) {
54 Swap(&A[i], &A[largest]);
55 Max_Heapify(A, largest, heap_size);
56 }
57 }
58
59 void Build_Max_Heap(int A[]) {
60 int i;
61 for (i = HEAP_SIZE; i >= 0; i--) {
62 Max_Heapify(A, i, HEAP_SIZE);
63 }
64 }
65
66 void print(int A[]) {
67 int i;
68 for (i = 0; i < HEAP_SIZE; i++) {
69 printf("%d ", A[i]);
70 }
71 printf("\n");
72 }
73
74 void HeapSort(int A[], int heap_size) {
75 Build_Max_Heap(A);
76
77 int i;
78 for (i = heap_size - 1; i >= 1; i--) {
79 Swap(&A[0], &A[i]);
80 heap_size = heap_size - 1;
81 Max_Heapify(A, 0, heap_size);
82 }
83 print(A);
84 }
85
86 int main() {
87 int A[HEAP_SIZE] = { 0, 1, 10, 14, 16, 4, 7, 9, 3, 2, 8, 5, 11 };
88 //int A[HEAP_SIZE] = { 19, 1, 10, 14, 16, 4, 7, 9, 3, 2, 8, 5, 11 };
89 print(A);
90 HeapSort(A, HEAP_SIZE);
91 print(A);
92
93 return 0;
94 }
95
ref:http://student.zjzk.cn/course_ware/data_structure/web/paixu/paixu8.4.2.1.htm
堆排序可通过树形结构保存部分比较结果,可减少比较次数。