ALDS1_1_D-MaximumProfit.
Codes:
//#define LOCAL

#include <cstdio>
#include <algorithm>
using namespace std; #define maxSize 200010
int a[maxSize]; int main()
{
#ifdef LOCAL
freopen("E:\\Temp\\input.txt", "r", stdin);
freopen("E:\\Temp\\output.txt", "w", stdout);
#endif int n;
scanf("%d", &n);
for(int i=0; i<n; ++i) scanf("%d", &a[i]); int maxv = -1e9, minv = a[0];
for(int i=1; i<n; ++i) {
maxv = max(maxv, a[i]-minv);
minv = min(minv, a[i]);
} printf("%d\n", maxv); return 0;
}
ALDS1_1_A-InsertionSort.
Description:

Write a program of the Insertion Sort algorithm which sorts a sequence A in ascending order. The algorithm should be based on the following pseudocode:

for i = 1 to A.length-1

key = A[i]

/* insert A[i] into the sorted sequence A[0,...,j-1] */

j = i - 1

while j >= 0 and A[j] > key

A[j+1] = A[j]

j--

A[j+1] = key

Note that, indices for array elements are based on 0-origin.

To illustrate the algorithms, your program should trace intermediate result for each step.

Input:

The first line of the input includes an integer N, the number of elements in the sequence.

In the second line, N elements of the sequence are given separated by a single space.

Output:

The output consists of N lines. Please output the intermediate sequence in a line for each step. Elements of the sequence should be separated by single space.

Constraints:

1 ≤ N ≤ 100

Sample Input 1:

6

5 2 4 6 1 3

Sample Output 1:

5 2 4 6 1 3

2 5 4 6 1 3

2 4 5 6 1 3

2 4 5 6 1 3

1 2 4 5 6 3

1 2 3 4 5 6

Sample Input 2:

3

1 2 3

Sample Output 2:

1 2 3

1 2 3

1 2 3

Codes:
/*#define LOCAL*/

#include <cstdio>

void trace(int a[], int n) {
for(int i=0; i<n; ++i) {
if(i) printf(" ");
printf("%d", a[i]);
}
printf("\n");
} void insertionSort(int a[], int n) {
for(int i=1; i<n; ++i) {
int j = i-1, v = a[i];
while(j>=0 && a[j]>v) {
a[j+1] = a[j];
--j;
}
a[j+1] = v;
trace(a, n);
}
} int main()
{
#ifdef LOCAL
freopen("E:\\Temp\\input.txt", "r", stdin);
freopen("E:\\Temp\\output.txt", "w", stdout);
#endif int n, a[110];
scanf("%d", &n);
for(int i=0; i<n; ++i) scanf("%d", &a[i]); trace(a, n);
insertionSort(a, n); return 0;
}
ALDS1_2_A-BubbleSort.
Description:

Write a program of the Bubble Sort algorithm which sorts a sequence A in ascending order. The algorithm should be based on the following pseudocode:

BubbleSort(A)

1 for i = 0 to A.length-1

2 for j = A.length-1 downto i+1

3 if A[j] < A[j-1]

4 swap A[j] and A[j-1]

Note that, indices for array elements are based on 0-origin.

Your program should also print the number of swap operations defined in line 4 of the pseudocode.

Input:

The first line of the input includes an integer N, the number of elements in the sequence.

In the second line, N elements of the sequence are given separated by spaces characters.

Output:

The output consists of 2 lines.

In the first line, please print the sorted sequence. Two contiguous elements of the sequence should be separated by a space character.

In the second line, please print the number of swap operations.

Constraints:

1 ≤ N ≤ 100

Sample Input 1:

5

5 3 2 4 1

Sample Output 1:

1 2 3 4 5

8

Sample Input 2:

6

5 2 4 6 1 3

Sample Output 2:

1 2 3 4 5 6

9

Codes:
//#define LOCAL

#include <cstdio>

int bubbleSort(int a[], int n) {
int counter = 0;
bool flag = 1;
for(int i=0; flag; ++i) {
flag = 0;
for(int j=n-1; j>i; --j) {
if(a[j] < a[j-1]) {
int temp = a[j]; a[j] = a[j-1]; a[j-1] = temp;
flag = 1; ++counter;
}
}
}
return counter;
} int main()
{
#ifdef LOCAL
freopen("E:\\Temp\\input.txt", "r", stdin);
freopen("E:\\Temp\\output.txt", "w", stdout);
#endif int n, counter, a[110];
scanf("%d", &n);
for(int i=0; i<n; ++i) scanf("%d", &a[i]);
counter = bubbleSort(a, n); for(int i=0; i<n; ++i) {
if(i) printf(" ");
printf("%d", a[i]);
}
printf("\n%d\n", counter); return 0;
}
ALDS1_2_B-SelectionSort.
Description:

Write a program of the Selection Sort algorithm which sorts a sequence A in ascending order. The algorithm should be based on the following pseudocode:

SelectionSort(A)

1 for i = 0 to A.length-1

2 mini = i

3 for j = i to A.length-1

4 if A[j] < A[mini]

5 mini = j

6 swap A[i] and A[mini]

Note that, indices for array elements are based on 0-origin.

Your program should also print the number of swap operations defined in line 6 of the pseudocode in the case where i ≠ mini.

Input:

The first line of the input includes an integer N, the number of elements in the sequence.

In the second line, N elements of the sequence are given separated by space characters.

Output:

he output consists of 2 lines.

In the first line, please print the sorted sequence. Two contiguous elements of the sequence should be separated by a space character.

In the second line, please print the number of swap operations.

Constraints:

1 ≤ N ≤ 100

Sample Input 1:

6

5 6 4 2 1 3

Sample Output 1:

1 2 3 4 5 6

4

Sample Input 2:

6

5 2 4 6 1 3

Sample Output 2:

1 2 3 4 5 6

3

Codes:
//#define LOCAL

#include <cstdio>

int selectionSort(int a[], int n) {
int minv, counter = 0;
for(int i=0; i<n-1; ++i) {
minv = i;
for(int j=i+1; j<n; ++j)
if(a[j] < a[minv]) minv = j;
if(i != minv) {
int t = a[minv]; a[minv] = a[i];
a[i] = t; ++counter;
}
}
return counter;
} int main()
{
#ifdef LOCAL
freopen("E:\\Temp\\input.txt", "r", stdin);
freopen("E:\\Temp\\output.txt", "w", stdout);
#endif int n, counter, a[110];
scanf("%d", &n);
for(int i=0; i<n; ++i) scanf("%d", &a[i]); counter = selectionSort(a, n);
for(int i=0; i<n; ++i) {
if(i) printf(" ");
printf("%d", a[i]);
}
printf("\n%d\n", counter); return 0;
}
ALDS1_2_C-StableSort.
Description:

Let's arrange a deck of cards. There are totally 36 cards of 4 suits(S, H, C, D) and 9 values (1, 2, ... 9). For example, 'eight of heart' is represented by H8 and 'one of diamonds' is represented by D1.

Your task is to write a program which sorts a given set of cards in ascending order by their values using the Bubble Sort algorithms and the Selection Sort algorithm respectively. These algorithms should be based on the following pseudocode:

BubbleSort(C)

1 for i = 0 to C.length-1

2 for j = C.length-1 downto i+1

3 if C[j].value < C[j-1].value

4 swap C[j] and C[j-1]

SelectionSort(C)

1 for i = 0 to C.length-1

2 mini = i

3 for j = i to C.length-1

4 if C[j].value < C[mini].value

5 mini = j

6 swap C[i] and C[mini]

Note that, indices for array elements are based on 0-origin.

Input:

The first line contains an integer N, the number of cards.

N cards are given in the following line. Each card is represented by two characters. Two consecutive cards are separated by a space character.

Output:

In the first line, print the arranged cards provided by the Bubble Sort algorithm. Two consecutive cards should be separated by a space character.

In the second line, print the stability ("Stable" or "Not stable") of this output.

In the third line, print the arranged cards provided by the Selection Sort algorithm. Two consecutive cards should be separated by a space character.

In the fourth line, print the stability ("Stable" or "Not stable") of this output.

Constraints:

1 ≤ N ≤ 36

Sample Input 1:

5

H4 C9 S4 D2 C3

Sample Output 1:

D2 C3 H4 S4 C9

Stable

D2 C3 S4 H4 C9

Not stable

Sample Input 2:

2

S1 H1

Sample Output 2:

S1 H1

Stable

S1 H1

Stable

Codes:
//#define LOCAL

#include <cstdio>

struct Card {char suit, value;};

void bubble(struct Card a[], int n) {
for(int i=0; i<n; ++i) {
for(int j=n-1; j>=i+1; --j)
if(a[j].value < a[j-1].value) {
Card t = a[j]; a[j] = a[j-1]; a[j-1] = t;
}
}
} void selection(struct Card a[], int n) {
for(int i=0; i<n; ++i) {
int minv = i;
for(int j=i+1; j<n; ++j)
if(a[j].value < a[minv].value) minv = j;
Card t = a[minv]; a[minv] = a[i]; a[i] = t;
}
} void print(struct Card a[], int n) {
for(int i=0; i<n; ++i) {
if(i) printf(" ");
printf("%c%c", a[i].suit, a[i].value);
}
printf("\n");
} bool isStable(struct Card C1[], struct Card C2[], int n) {
for(int i=0; i<n; ++i)
if(C1[i].suit != C2[i].suit) return false;
return true;
} int main()
{
#ifdef LOCAL
freopen("E:\\Temp\\input.txt", "r", stdin);
freopen("E:\\Temp\\output.txt", "w", stdout);
#endif char str[5];
int n, j = 0;
Card C1[100], C2[100]; scanf("%d", &n);
for(int i=0; i<n; ++i) {
scanf("%s", str);
C1[i].suit = C2[i].suit = str[0];
C1[i].value = C2[i].value = str[1];
} bubble(C1, n);
selection(C2, n); print(C1, n); printf("Stable\n");
print(C2, n);
if(isStable(C1, C2, n)) printf("Stable\n");
else printf("Not stable\n"); return 0;
}
ALDS1_2_D-ShellSort.
Codes:
//#define LOCAL

#include <iostream>
#include <cstdio>
#include <algorithm>
#include <cmath>
#include <vector>
using namespace std; int l, n, A[1000000];
long long cnt;
vector<int> G; void insertionSort(int A[], int n, int g) {
for(int i=g; i<n; ++i) {
int v = A[i], j = i-g;
while(j>=0 && A[j]>v) {
A[j+g] = A[j];
j -= g; ++cnt;
}
A[j+g] = v;
}
} void shellSort(int A[], int n) {
for(int h=1;;) {
if(h > n) break;
G.push_back(h);
h = 3*h+1;
}
for(int i=G.size()-1; i>=0; --i) {
insertionSort(A, n, G[i]);
}
} int main()
{
#ifdef LOCAL
freopen("E:\\Temp\\input.txt", "r", stdin);
freopen("E:\\Temp\\output.txt", "w", stdout);
#endif scanf("%d", &n);
for(int i=0; i<n; ++i) scanf("%d", &A[i]);
cnt = 0; shellSort(A, n);
printf("%d\n", G.size());
for(int i=G.size()-1; i>=0; --i) {
printf("%d", G[i]);
if(i) printf(" ");
}
printf("\n%d\n", cnt);
for(int i=0; i<n; ++i) printf("%d\n", A[i]); return 0;
}

AOJ/初等排序习题集的更多相关文章

  1. AOJ/高等排序习题集

    ALDS1_5_B-MergeSort. Description: Write a program of a Merge Sort algorithm implemented by the follo ...

  2. 贪心+拓扑排序 AOJ 2456 Usoperanto

    题目传送门 题意:给出一条链,比如x连到y,x一定要在y的左边,且代价是这条链经过的点的权值和,问如何排序使得代价最小 分析:类似拓扑排序,先把入度为0的点入队,把指向该点的所有点按照权值排序,保证这 ...

  3. AOJ/数据结构习题集

    ALDS1_3_A-Stack. Description: Write a program which reads an expression in the Reverse Polish notati ...

  4. AOJ/搜索递归分治法习题集

    ALDS1_4_A-LinearSearch. Description: You are given a sequence of n integers S and a sequence of diff ...

  5. AOJ/树二叉搜索树习题集

    ALDS1_7_A-RootedTree. Description: A graph G = (V, E) is a data structure where V is a finite set of ...

  6. AOJ/堆与动态规划习题集

    ALDS1_9_A-CompleteBinaryTree. Codes: //#define LOCAL #include <cstdio> int parent(int i) { ret ...

  7. AOJ/树与二叉搜索树习题集

    ALDS1_7_A-RootedTree. Description: A graph G = (V, E) is a data structure where V is a finite set of ...

  8. AOJ/搜索与递归及分治法习题集

    ALDS1_4_A-LinearSearch. Description: You are given a sequence of n integers S and a sequence of diff ...

  9. 数据结构与算法之PHP排序算法(归并排序)

    一.基本思想 归并排序算法是将两个(或两个以上)有序表合并成一个新的有序表,即把待排序序列分为若干个子序列,使每个子序列有序,再将已有序的子序列合并,得到完全有序的序列.该算法是采用分治法(Divid ...

随机推荐

  1. Linux学习之Vim使用

    一 为何要学Vim 所有的Unix Like系统都有自带vi编辑器 一些软件的编辑接口会自动调起vi 作为vi的升级版,vim具有程序编辑功能,而且具有代码颜色高亮显示.辨别代码的正确性等功能 以上优 ...

  2. GitHub开源:升讯威ADO.NET增强组件 sheng.ADO.NET.Plus V1.3

    GitHub: https://github.com/iccb1013/sheng.ADO.NET.Plus 早前分享过,当时没有把代码上传到Github,只是通过邮件的形式分享给了部分需要的朋友,最 ...

  3. ubuntu16.10下安装erlang和RabbitMQ

    Ubuntu系统下安装RabbitMQ(我选择的是Ubuntu Server 16.10) 1.首先必须要有Erlang环境支持 --安装之前要装一些必要的库(Erlang开发环境同样)(参考:duq ...

  4. 微信JS分享功能--微信JS系列文章(二)

    概述 在上一篇文章微信JS初始化-- 微信JS系列文章(一)中已经介绍了微信JS初始化的相关工作,接下来本文继续就微信JS的分享功能进行描述,供大家参考. 代码 $(document).ready(f ...

  5. 用Java实现将多级文件夹下的所有文件统一放到一个文件夹中

    每次下了电影(男生懂得呦),每部电影都放在一个单独的文件夹里,看的时候很是不方便啊,一直重复着进入文件夹.后退,再进.再退的操作,而手动把这些电影全部复制出来又太繁琐.因此为了解决这个问题,用IO写了 ...

  6. WebService从服务端到客户端的用例

    1.首先编写Wsdl(基于契约优先的方式),要注意的是命名空间(若是使用include或import)最好使用一致的,代码如下: <?xml version="1.0" en ...

  7. JavaScript贪食蛇游戏制作详解

    之前闲时开发过一个简单的网页版贪食蛇游戏程序,现在把程序的实现思路写下来,供有兴趣同学参考阅读. 代码的实现比较简单,整个程序由三个类,一组常量和一些游戏逻辑以外的初始化和控制代码组成,总共400多行 ...

  8. div的优缺点

    div+css优缺点   产生背景 HTML语言自HTML4.01以来,不再发布新版本,原因就在于HTML语言正变得越来越复杂化.专用化.即标记越来越多,甚至各个浏览器生产商也开发出只适合于其特定浏览 ...

  9. Linux shell指令运行的原理

    shell是一种命令行解释器 对于一般用户,我们不能直接使用操作系统(kernel).而是通过 kernel的"外壳"程序,也就是所谓的shell,来与kernel沟通.    为 ...

  10. 01.Nodejs入门之Helloworld

    说明:本文章可供有一定js基础的朋友参考nodejs入门,本文未讲解nodejs的安装,如有需要的同学可以加QQ3382260752找我,进行交流学习. 1.新建文件夹helloworld demo, ...