算法分析第四次作业

1. 问题

n个不同的数构成的数组A[1..n]进行排序,其中n=2^k;

2. 解析

 

 

3. 设计

    (1) 将待排序序列从中间一分为二,对左右两边再进行递归分割操作,得到n个相互独立的子序列;

    (2) n个独立的子序列递归的执行合并操作,最终得到有序的序列

4. 分析

   

 

k=lognW(1)=0带入得出复杂度为nlogn

 

5. 源码

    https://github.com/Tinkerllt/algorithm-work.git

 1 #include<stdio.h>
 2 #include<iostream>
 3 #include<cmath>
 4 #include<cstring>
 5 #include<algorithm>
 6 #include<bitset>
 7 #include<set>
 8 #include<deque>
 9 #include<queue>
10 #include<vector>
11 //#include<unordered_map>
12 #include<map>
13 #include<stack>
14 using namespace std;
15 #define ll long long
16 #define ull unsigned long long
17 #define pii pair<int,int>
18 #define Pii pair<ll,ll>
19 #define m_p make_pair
20 #define l_b lower_bound
21 #define u_b upper_bound 
22 const int inf=0x3f3f3f3f;
23 const ll linf=0x3f3f3f3f3f3f3f3f;
24 const int maxn=3e5+11;
25 const int maxm=2e3+11;
26 const int mod=1e9+7; 
27 const double eps=1e-5;
28 ll rd(){ll x = 0, f = 1; char ch = getchar();while (ch<'0' || ch>'9') { if (ch == '-')f = -1; ch = getchar(); }while (ch >= '0'&&ch <= '9') { x = x * 10 + ch - '0'; ch = getchar(); }return x * f;}
29 inline ll qpow(ll a,ll b,ll p){ll res=1;while(b){if(b&1){res*=a;res%=p;}b>>=1;a=a*a%p;}return res;}
30 inline ll gcd(ll a,ll b){if(b==0) return a;return gcd(b,a%b);}
31 //iterator 
32 //head
33 //priority_queue
34 void merge(int *data,int start,int end,int *result)
35 {
36     int left_length = (end - start + 1) / 2 + 1;//左部分区间的数据元素的个数
37     int left_index = start;
38     int right_index = start + left_length;
39     int result_index = start;
40     while(left_index < start + left_length && right_index < end+1)
41     {
42         //对分别已经排好序的左区间和右区间进行合并
43         if(data[left_index] <= data[right_index])
44             result[result_index++] = data[left_index++];
45         else
46             result[result_index++] = data[right_index++];
47     }
48     while(left_index < start + left_length)
49         result[result_index++] = data[left_index++];
50     while(right_index < end+1)
51         result[result_index++] = data[right_index++];
52 }
53 void merge_sort(int *data, int start, int end, int *result)
54 {
55     if(1 == end - start)//如果区间中只有两个元素,则对这两个元素进行排序
56     {
57         if(data[start] > data[end])
58         {
59             int temp  = data[start];
60             data[start] = data[end];
61             data[end] = temp;
62         }
63         return;
64     }
65     else if(0 == end - start)//如果只有一个元素,则不用排序
66         return;
67     else
68     {
69         //继续划分子区间,分别对左右子区间进行排序
70         merge_sort(data,start,(end-start+1)/2+start,result);
71         merge_sort(data,(end-start+1)/2+start+1,end,result);
72         //开始归并已经排好序的start到end之间的数据
73         merge(data,start,end,result);
74         //把排序后的区间数据复制到原始数据中去
75         for(int i = start;i <= end;++i)
76             data[i] = result[i];
77     }
78 }
79 int main(){
80     //std::ios::sync_with_stdio(false);
81     int n=rd();
82     int data[100];
83     int result[100];
84     for(int i=0;i<n;i++){
85         data[i]=rd();
86     }
87     cout << "Before sorted:" << endl;
88     for(int i = 0;i < n;++i)
89         cout << data[i] << "  ";
90     cout << endl;
91     cout << "After sorted:" << endl;
92     merge_sort(data,0,n-1,result);
93     for(int i = 0;i < n;++i)
94         cout << data[i] << "  ";
95     cout << endl;
96 }
View Code

 

posted @ 2020-03-24 13:41  Tinker1998  阅读(168)  评论(0编辑  收藏  举报