码迷,mamicode.com
首页 > 编程语言 > 详细

面试必考三大排序算法C++代码(快速排序、归并排序、堆排序)

时间:2017-08-23 23:05:34      阅读:277      评论:0      收藏:0      [点我收藏+]

标签:输出   merge   for   void   print   child   middle   value   begin   

前两个为C++风格代码,后一个为C风格代码,除了输入输出,其它无差别,但C输入输出要比C++更快。

 

快速排序

 1 #include <iostream>
 2 using namespace std;
 3 
 4 void swap(int num[], int i, int j)
 5 {
 6     int temp = num[i];
 7     num[i] = num[j];
 8     num[j] = temp;
 9 }
10 
11 int partition(int num[], int left, int right)
12 {
13     int key = left;
14     int value = num[key];
15     while (left < right)
16     {
17         while (left < right && num[right] >= value)right--;
18         while (left < right && num[left] <= value)left++;
19         swap(num, right, left);
20     }
21     swap(num, key, left);
22     return left;
23 }
24 
25 void QuickSort(int num[], int begin, int end)
26 {
27     if (begin < end)
28     {
29         int middle = partition(num, begin, end);
30         QuickSort(num, begin, middle - 1);
31         QuickSort(num, middle + 1, end);
32     }
33 }
34 
35 int main()
36 {
37     int num[100];
38     int n = 0;
39     while (cin >> n)
40     {
41         for (int i = 0; i < n; i++)
42         {
43             int temp = 0;
44             cin >> temp;
45             num[i] = temp;
46         }
47         QuickSort(num, 0, n - 1);
48         for (int i = 0; i < n; i++)
49             cout << num[i] << " ";
50         cout << endl;
51     }
52     return 0;
53 }

 

归并排序

 1 #include <iostream>
 2 using namespace std;
 3 
 4 void Merge(int *from, int *to, int begin, int middle, int end)
 5 {
 6     int i = begin;
 7     int j = middle + 1;
 8     int k = i;
 9     while (i <= middle && j <= end)
10     {
11         if (from[i] < from[j])
12             to[k++] = from[i++];
13         else to[k++] = from[j++];
14     }
15     while (i <= middle) to[k++] = from[i++];
16     while (j <= end) to[k++] = from[j++];
17 }
18 
19 void MergePass(int *from, int *to, int end, int h)
20 {
21     int i = 0;
22     while (i <= end - 2 * h + 1)
23     {
24         Merge(from, to, i, i + h - 1, i + 2 * h - 1);
25         i += 2 * h;
26     }
27     if (i < end - h + 1)
28         Merge(from, to, i, i + h - 1, end);
29     else
30         for (int k = i; k <= end; k++)
31         {
32             to[k] = from[k];
33         }
34 }
35 
36 void MergeSort(int *from, int *to, int begin, int end)
37 {
38     int h = 1;
39     while (h <= end)
40     {
41         MergePass(from, to, end, h);
42         h = 2 * h;
43         MergePass(to, from, end, h);
44         h = 2 * h;
45     }
46 }
47 
48 int main() {
49     int num[100];
50     int num2[100];
51     int n = 0;
52     while (cin >> n)
53     {
54         for (int i = 0; i < n; i++)
55         {
56             int temp = 0;
57             cin >> temp;
58             num[i] = temp;
59         }
60         MergeSort(num, num2, 0, n - 1);
61         for (int i = 0; i < n; i++)
62             cout << num[i] << " ";
63         cout << endl;
64     }
65     return 0;
66 }

 

堆排序

 1 #include <stdio.h>
 2 
 3 void HeapAdjust(int *num, int s, int length)
 4 {
 5     int temp = num[s];
 6     int child = 2 * s + 1;
 7     while (child < length)
 8     {
 9         if (child + 1 < length && num[child] < num[child + 1])
10             child++;
11         if (num[s] < num[child])
12         {
13             num[s] = num[child];
14             num[child] = temp;
15             s = child;
16             child = 2 * s + 1;
17         }
18         else
19             break;
20     }
21 }
22 
23 void buildingHeap(int *num, int length)
24 {
25     for (int i = (length - 1) / 2; i >= 0; --i)
26         HeapAdjust(num, i, length);
27 }
28 
29 void HeapSort(int *num, int length)
30 {
31     buildingHeap(num, length);
32     for (int i = length - 1; i > 0; --i)
33     {
34         int temp = num[0];
35         num[0] = num[i];
36         num[i] = temp;
37         HeapAdjust(num, 0, i);
38     }
39 }
40 
41 void print(int num[], int n) {
42     for (int i = 0; i < n; i++) {
43         printf("%d ", num[i]);
44     }
45     printf("\n");
46 }
47 
48 int main()
49 {
50     int num[100];
51     int n = 0;
52     while (scanf("%d", &n) != EOF)
53     {
54         for (int i = 0; i < n; i++)
55             scanf("%d", &num[i]);
56         HeapSort(num, n);
57         print(num, n);
58     }
59     return 0;
60 }

 

验证OJ:https://www.nowcoder.com/practice/508f66c6c93d4191ab25151066cb50ef?tpId=69&&tqId=29657&rp=1&ru=/activity/oj&qru=/ta/hust-kaoyan/question-ranking

面试必考三大排序算法C++代码(快速排序、归并排序、堆排序)

标签:输出   merge   for   void   print   child   middle   value   begin   

原文地址:http://www.cnblogs.com/renjiashuo/p/7412583.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!