归并排序

 1 #include<stdio.h>
 2 
 3 void Merge_Sort(int *a,int l,int r)
 4 {
 5     int len=r-l;
 6     if(len<=1)  return;
 7     int mid=l+len/2;
 8     Merge_Sort(a,l,mid);
 9     Merge_Sort(a,mid,r);
10     int p1=l,p2=mid;
11     int temp[10000];
12     for(int i=l;i<r;i++)
13     {
14         if(p1==mid)
15         {
16             temp[i]=a[p2];
17             p2++;
18         }
19         else if(p2==r)
20         {
21             temp[i]=a[p1];
22             p1++;
23         }
24         else
25         {
26             if(a[p1]<a[p2])
27             {
28                 temp[i]=a[p1];
29                 p1++;
30             }
31             else
32             {
33                 temp[i]=a[p2];
34                 p2++;
35             }
36         }
37     }
38     for(int i=l;i<r;i++)
39         a[i]=temp[i];
40 }
41 
42 int main()
43 {
44     int n,i;
45     int a[10000];
46     scanf("%d",&n);
47     for(i=1;i<=n;i++)
48         scanf("%d",&a[i]);
49     Merge_Sort(a,1,n+1);
50     for(i=1;i<=n;i++)
51         printf("%d ",a[i]);
52     return 0;
53 }
View Code
                                  Ultra-QuickSort
Time Limit: 7000MS   Memory Limit: 65536K
Total Submissions: 46551   Accepted: 16969

Description

In this problem, you have to analyze a particular sorting algorithm. The algorithm processes a sequence of n distinct integers by swapping two adjacent sequence elements until the sequence is sorted in ascending order. For the input sequence 
9 1 0 5 4 ,

Ultra-QuickSort produces the output 
0 1 4 5 9 .

Your task is to determine how many swap operations Ultra-QuickSort needs to perform in order to sort a given input sequence.

Input

The input contains several test cases. Every test case begins with a line that contains a single integer n < 500,000 -- the length of the input sequence. Each of the the following n lines contains a single integer 0 ≤ a[i] ≤ 999,999,999, the i-th input sequence element. Input is terminated by a sequence of length n = 0. This sequence must not be processed.

Output

For every input sequence, your program prints a single line containing an integer number op, the minimum number of swap operations necessary to sort the given input sequence.

Sample Input

5
9
1
0
5
4
3
1
2
3
0

Sample Output

6
0

Source

 

 1 #include<stdio.h>
 2 
 3 int temp[900000];
 4 __int64 number;
 5 void Merge_Sort(int *a,int l,int r)
 6 {
 7     int len=r-l;
 8     if(len<=1)  return;
 9     int mid=l+len/2;
10     Merge_Sort(a,l,mid);
11     Merge_Sort(a,mid,r);
12     int p1=l,p2=mid;
13     for(int i=l;i<r;i++)
14     {
15         if(p1==mid)
16         {
17             temp[i]=a[p2];
18             p2++;
19         }
20         else if(p2==r)
21         {
22             temp[i]=a[p1];
23             p1++;
24         }
25         else
26         {
27             if(a[p1]<a[p2])
28             {
29                 temp[i]=a[p1];
30                 p1++;
31             }
32             else
33             {
34                 temp[i]=a[p2];
35                 p2++;
36                 number=number+mid-p1;
37             }
38         }
39     }
40     for(int i=l;i<r;i++)
41         a[i]=temp[i];
42 }
43 
44 int main()
45 {
46     int n,i,m;
47     int a[900000];
48     while(scanf("%d",&n)!=EOF && n!=0)
49     {
50         number=0;
51         for(i=1;i<=n;i++)
52             scanf("%d",&a[i]);
53         Merge_Sort(a,1,n+1);
54         printf("%I64d\n",number);
55     }
56     return 0;
57 }
View Code

 

posted @ 2015-05-21 16:57  cyd2014  阅读(140)  评论(0编辑  收藏  举报