快速排序和归并排序

 

快速排序的归并排序:

 1 void quickSort( int *array, int left, int right )
 2 {
 3     int key = array[left]; // 取左边的第一个数为标准排序
 4     int i = left;
 5     int j = right;
 6     while( i < j )
 7     {
 8         while( i<j && array[j]>key )
 9             j--;
10         array[i] = array[j];
11         while( i<j && array[i]<=key )
12             i++;
13         array[j] = array[i];
14     }
15     array[i] = key;
16 
17     if( left < i-1 )
18         quickSort( left, i-1 );
19     if( i+1 < right )
20         quickSort( i+1, right );
21 }


归并排序:

 1 #define N 1000
 2 #define INF 0x7fffffff // 无穷大
 3 int array[N];
 4 int u[N/2];
 5 int v[N/2];
 6 // 先分开
 7 void Merge_sort( int left, int right )
 8 {
 9     if( left >= right )
10         return;
11     int mid = (left+right) / 2;
12     Merge_sort( left, mid );
13     Merge_sort( mid+1, right );
14     Merge( left, mid, right );
15 }
16 
17 // 再合并
18 void Merge( int left, int mid, int right )
19 {
20     int i;
21     int m = mid-left+1, n = right-mid; // 分别用m, n记住左边和右边有多少个数字
22     int p = 0, q = 0; // 用来类似指针的作用
23 
24     // 先用一个for循环, 把array数组中的数字放入u, v中
25     for( i=0; i<m; i++ )
26         u[i] = array[left+i];
27     u[m] = INF; // 赋值无穷大,不被访问。。。
28     for( i=0; i<n; i++ )
29         v[i] = array[mid+i+1];
30     v[n] = INF;
31 
32     for( i=left; i<=right; i++ )
33         if( v[p] <= u[p] )
34             array[i] = u[p], p++;
35         else
36             array[i] = v[q], q++; 
37 }


SZU     Problem(A52):Books

Judge Info

  • Memory Limit: 10240KB
  • Case Time Limit: 5000MS
  • Time Limit: 5000MS
  • Judger: Number Only Judger

Description

I like to arrange my books on my long book shelf. I will rank my books by its height, but i have too many books, it is troubling me when I try to order all my books in one time. Finally, i figure out a solution, each time i will rank some of them by its height from higher to shorter only.

Task

The question for you is, after K(0 \leq K \leq 10) times arrangement. What’s the order of my books on my book shelf will be?(The height of my books in order from left to right.)

Input

The first line of input contains T(1 \leq T \leq 10), the number of test cases. For each test case , the first line will have an integer N(1 \leq N \leq 1, 000)which means N books on my book shelf. On the second line, N integers followed, all of them are \leq 1, 000 which denote the height of my books. On the third line, an integer K(1 \leq K \leq 10). Then, K pairs of integer follow, it records the range of my books i arranged each time (0-index base, the first book on my book shelf is 0).

Output

For each test case, output a line contains N integers, the final states of my book shelf.

Sample Input

2
10
3 3 2 4 7 5 6 8 9 1
2
1 3
2 4
10
3 3 2 4 7 5 6 8 9 1
2
1 3
2 4

Sample Output

3 4 7 3 2 5 6 8 9 1
3 4 7 3 2 5 6 8 9 1

 1 #include <stdio.h>
 2  
 3 int array[250000];
 4  
 5 void quickSort( int left, int right );
 6  
 7 int main()
 8 {
 9     int t;
10     scanf( "%d", &t );
11  
12     while( t-- )
13     {
14         int n;
15         scanf( "%d", &n );
16         int i;
17         for( i=0; i<n; i++ )
18             scanf( "%d", &array[i] );
19         int m;
20         scanf( "%d", &m );
21         while( m-- )
22         {
23             int a;
24             int b;
25             scanf( "%d%d", &a, &b );
26             quickSort( a, b );
27         }
28  
29  
30         for( i=0; i<n-1; i++ )
31             printf( "%d ", array[i] );
32         printf( "%d\n", array[i] );
33     }
34  
35     return 0;
36 }
37  
38 void quickSort( int left, int right )
39 {
40     int key = array[left]; //取每次左边的第一个数为key
41     int i = left;
42     int j = right;
43  
44     while( i < j )
45     {
46         while( array[j] < key && i < j )
47             j--;
48         array[i] = array[j];
49         while( array[i] >= key && i < j )
50             i++;
51         array[j] = array[i];
52     }
53     array[j] = key;
54  
55     if( left < i-1 )
56         quickSort( left, i-1 );
57     if( i+1 < right )
58         quickSort( i+1, right );
59 }

SZU  Problem(A74):Who's the boss?

Judge Info

  • Memory Limit: 32768KB
  • Case Time Limit: 10000MS
  • Time Limit: 10000MS
  • Judger: Number Only Judger

Description

Several surveys indicate that the taller you are, the higher you can climb the corporate ladder. At TALL Enterprises Inc. this “de facto standard” has been properly formalized:

your boss is always at least as tall as you are.

Furthermore, you can safely assume that your boss earns a bit more than you do.

In fact, you can be absolutely sure that your immediate boss is the person who earns the least among all the employees that earn more than you and are at least as tall as you are.

Furthermore, if you are the immediate boss of someone, that person is your subordinate, and all his subordinates are your subordinates as well. If you are nobody’s boss, then you have no subordinates.

As simple as these rules are, many people working for TALL are unsure of to whom they should be turning in their weekly progress report and how many subordinates they have.

Write a program that will help in determining for any employee who the immediate boss of that employee is and how many subordinates they have. Quality Assurance at TALL have devised a series of tests to ensure that your program is correct. These test are described below.

Input

On the first line of the input is a single positive integer n, telling the number of test scenarios to follow. Each scenario begins with a line containing two positive integers m and q, where m (at most 30000) is the number of employees and q (at most 200) is the number of queries. The following m lines each list an employee by three integers on the same line: employee ID number (six decimal digits, the first one of which is not zero), yearly salary in Euros and finally height in mm (1 mm = 10 − 6 meters – accuracy is important at TALL). The chairperson is the employee that earns more than anyone else and is also the tallest person in the company. Then there are q lines listing queries. Each query is a single legal employee ID.

The salary is a positive integer which is at most 10,000,000. No two employees have the same ID, and no two employees have the same salary. The height of an employee is at least 1,000,000 mm and at most 2,500,000 mm.

Output

For each employee ID x in a query output a single line with two integers y k, separated by one space character, where y is the ID of x’s boss, and k is the number of subordinates of x. If the query is the ID of the chairperson, then you should output 0 as the ID of his or her boss (since the chairperson has no immediate boss except, possibly, God).

Sample Input

2
3 3
123456 14323 1700000
123458 41412 1900000
123457 15221 1800000
123456
123458
123457
4 4
200002 12234 1832001
200003 15002 1745201
200004 18745 1883410
200001 24834 1921313
200004
200002
200003
200001

Sample Output

123457 0
0 2
123458 1
200001 2
200004 0
200004 0
0 3

 1  1 #include <stdio.h>
 2  2  
 3  3 int ID[30000];
 4  4 int Money[30000];
 5  5 int Height[30000];
 6  6  
 7  7 void quickSort( int left, int right );
 8  8 int main()
 9  9 {
10 10     int t, a, b, i, key, n, q;
11 11     scanf( "%d", &t );
12 12  
13 13     while( t-- )
14 14     {
15 15         scanf( "%d%d", &a, &b );
16 16  
17 17         for( i=0; i<a; i++ )
18 18         {
19 19             scanf( "%d", &ID[i] );
20 20             scanf( "%d", &Money[i] );
21 21             scanf( "%d", &Height[i] );
22 22         }
23 23         // 按照收入的多少进行快排
24 24         quickSort( 0, a-1 );
25 25  
26 26         while( b-- )
27 27         {
28 28             n = 0;
29 29             scanf( "%d", &q );
30 30             for( i=0; i<a; i++ )
31 31                 if( ID[i] == q )
32 32                     break;
33 33             key = i; // 记住q的下标
34 34             // 这是为了求他有多少个下属,注意有可能不是我的下属是别人的下属
35 35             for( i=key-1; i>=0; i-- )
36 36             {
37 37                 if( Height[i] <= Height[key] )
38 38                     n++;
39 39                 else
40 40                     break;
41 41             }
42 42             if( key+1 == a ) // 说明他已经是顶头上司啦~
43 43                 printf( "0 " );
44 44             else
45 45             {
46 46                 for( i=key+1; i<a; i++ )
47 47                     if( Money[key] < Money[i] && Height[key] <= Height[i] )
48 48                     {
49 49                         printf( "%d ", ID[i] );
50 50                         break;
51 51                     }
52 52             }
53 53             printf( "%d\n", n );
54 54         }
55 55     }
56 56     return 0;
57 57 }
58 58  
59 59 void quickSort( int left, int right )
60 60 {
61 61     int key1 = ID[left];
62 62     int key2 = Money[left];
63 63     int key3 = Height[left];
64 64     int i = left;
65 65     int j = right;
66 66  
67 67     while( i < j )
68 68     {
69 69         while( i < j && Money[j] > key2 )
70 70             j--;
71 71         Money[i] = Money[j];
72 72         ID[i] = ID[j];
73 73         Height[i] = Height[j];
74 74         while( i < j && Money[i] <= key2 )
75 75             i++;
76 76         Money[j] = Money[i];
77 77         ID[j] = ID[i];
78 78         Height[j] = Height[i];
79 79     }
80 80     Money[i] = key2;
81 81     ID[i] = key1;
82 82     Height[i] = key3;
83 83  
84 84     if( left < i-1 )
85 85         quickSort( left, i-1 );
86 86     if( i+1 < right )
87 87         quickSort( i+1, right );
88 88 }

SZU  Problem(B21):Sorting Algorithm

Judge Info

  • Memory Limit: 32768KB
  • Case Time Limit: 10000MS
  • Time Limit: 10000MS
  • Judger: Normal

Description

One of the fundamental problems of computer science is ordering a list of items. There are a plethora of solutions to this problem, known as soring algorithms. Some sorting algorithm are simple and intuitive, such as the bubble sort. Others, such as the heap sort are not so simple, but produce lightening-fast results.

In the following is a list of some sorting algorithms. Of course, I can't tell you how to implement them here. You must use your own knowledge.

  • Bubble sort
  • Heap sort
  • Insertion sort
  • Merge sort
  • Quick sort
  • Selection sort
  • Shell sort

My business here is to give you some numbers, and to sort them is your business. Attention, I want the smallest number at the top of the sorted list.

Input

The input will consist of series data sets. Each data set has two parts. The first part contains two non-negative integers, n(1\leq n \leq 100,000) and m(1 \leq m \leq n),representing the total of numbers you will get an interval of the output sorted list. The second part contains n positive integers. I am sure that each integer in this part will be less than 2,000,000,000.

The input is terminated by a line with two zeros.

Output

For one data set, you should output several numbers in ONE line. After you get the sorted list, you should output the first number of each m numbers, and you should print exact ONE space between two adjacent numbers. And please make sure that there should NOT be any blank line between outputs of two adjacent data sets.

Sample Input

8 2
3
5
7
1
8
6
4
2
0 0

Sample Output

1 3 5 7
 1 #include <stdio.h>
 2  
 3 int array[100000];
 4  
 5 void quickSort( int left, int right );
 6  
 7 int main()
 8 {
 9     int a;
10     int b;
11     scanf( "%d%d", &a, &b );
12  
13     while( a != 0 && b != 0 )
14     {
15         int i;
16         for( i=0; i<a; i++ )
17             scanf( "%d", &array[i] );
18  
19         quickSort( 0, a-1 );
20  
21         for( i=0; i<a-b; i+=b )
22             printf( "%d ", array[i] );
23         printf( "%d\n", array[i] );
24  
25         scanf( "%d%d", &a, &b );
26     }
27  
28     return 0;
29 }
30  
31 void quickSort( int left, int right )
32 {
33     int key = array[left]; //取每次左边的第一个数为key
34     int i = left;
35     int j = right;
36  
37     while( i < j )
38     {
39         while( array[j] > key && i < j )
40             j--;
41         array[i] = array[j];
42         while( array[i] <= key && i < j )
43             i++;
44         array[j] = array[i];
45     }
46     array[j] = key;
47  
48     if( left < i-1 )
49         quickSort( left, i-1 );
50     if( i+1 < right )
51         quickSort( i+1, right );
52 }


SZU  Problem(B58):Alec's Eggs

Judge Info

  • Memory Limit: 32768KB
  • Case Time Limit: 10000MS
  • Time Limit: 10000MS
  • Judger: Normal

Description

Eggs

Alec has a lot of eggs. One day, he want to sort them in a ascending sequence by weight. But he only can switch two eggs which are adjoining by each other because he has two hands only. Now he ask for your help, and you are enthusiastic. You decide help him calculate the total numbers of switch he need at least.

Attention: the weight of each egg less than 100,000,000 units.

Input

There are multiply test case.The first line describe the number of test case T(T \leq 10). For each test case, the first line describe the number of eggs that Alec has, N(1 \leq N \leq 100,000). The second line describe the weight of each eggs splited by a space.

Output

Output the total number of switch that Alec need at least. One line for each test case. The total number may be very very large but fited in 64-bit integer.

Sample Input

2
2
2 1
2
2 3

Sample Output

1
0
 1 #include <iostream>
 2 using namespace std;
 3  
 4 const int NAME = 100500;
 5 int array[NAME];
 6 int temp1[NAME/2];
 7 int temp2[NAME/2];
 8  
 9 void Merge_sort( int left, int right );
10 void Merge( int left, int mid, int right );
11 long long sum = 0;
12  
13 int main()
14 {
15    int t;
16     cin >> t;
17     while( t-- )
18     {
19         sum = 0;
20         int m;
21         cin >> m;
22         int i;
23         for( i=0; i<m; i++ )
24             cin >> array[i];
25         Merge_sort( 0, m-1 );
26         cout << sum << endl;
27     }
28     return 0;
29 }
30  
31 void Merge_sort( int left, int right )
32 {
33     if( left >= right )
34         return;
35  
36     int mid = (left+right) / 2;
37     Merge_sort( left, mid );
38     Merge_sort( mid+1, right );
39     Merge( left, mid, right );
40 }
41  
42 void Merge( int left, int mid, int right )
43 {
44     int i;
45     int m = mid - left + 1;
46     int n = right - mid;
47     int p = 0;
48     int q = 0;
49  
50     for( i=0; i<m; i++ )
51         temp1[i] = array[left+i];
52     temp1[m] = 200000000;
53     for( i=0; i<n; i++ )
54         temp2[i] = array[mid+i+1];
55     temp2[n] = 200000000;
56  
57     for( i=left; i<=right; i++)
58         if( temp1[p] <= temp2[q] )
59         {
60             array[i] = temp1[p];
61             p++;
62         }
63         else
64         {
65             sum += mid - left + 1 - p;
66             array[i] = temp2[q];
67             q++;
68         }
69  
70 }

SZU   Problem(J34):no larger than k

Judge Info

  • Memory Limit: 32768KB
  • Case Time Limit: 5000MS
  • Time Limit: 10000MS
  • Judger: Normal

Description

I know the problem "larger than k" is too simple, so here comes a not that simple problem. Giving you a list of number, your job is to find a smallest number k, such that there are m numbers in the list that are no larger than k.

Input

The first line is a number t, indicating the number of test cases. For each case, there are two line: the first line contains two number n,m(1<=n<=8000,000, 1<=m<=n), indicating the size of the list, and the number of numbers that shuold not be larger than k.

Output

For each case, output the number k.

Sample Input

3
6 3
1 2 3 4 5 6
6 2
6 9 5 2 3 1
3 2
1 1 1

Sample Output

3
2
1
 1 #include <stdio.h>
 2  
 3 int array[8000000];
 4 void quickSort( int left, int right, int lim );
 5 int main()
 6 {
 7     int t;
 8     scanf( "%d", &t );
 9  
10     while( t-- )
11     {
12         int n;
13         int lim;
14         int i;
15  
16         scanf( "%d%d", &n, &lim );
17         for( i=0; i<n; i++ )
18             scanf( "%d", &array[i] );
19  
20         quickSort( 0, n-1, lim );
21  
22         printf( "%d\n", array[lim-1] );
23     }
24  
25     return 0;
26 }
27  
28 void quickSort( int left, int right, int lim )
29 {
30     int key = array[left]; //取每次左边的第一个数为key
31     int i = left;
32     int j = right;
33  
34     while( i < j )
35     {
36         while( array[j] > key && i < j )
37             j--;
38         array[i] = array[j];
39         while( array[i] <= key && i < j )
40             i++;
41         array[j] = array[i];
42     }
43     array[j] = key;
44  
45     if( i == j == lim )
46         return;
47  
48     if( left < i-1 && i >= lim )
49         quickSort( left, i-1, lim );
50     if( i+1 < right && i < lim )
51         quickSort( i+1, right, lim );
52 }

 

转载于:https://www.cnblogs.com/zhongshuxin/p/3262134.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值