# 杰拉斯的博客

## [整理]ACM详解（5）——排序

1、基本排序
Problem Description
These days, I am thinking about a question, how can I get a problem as easy as A+B? It is fairly difficulty to do such a thing. Of course, I got it after many waking nights.
Give you some integers, your task is to sort these number ascending (升序).
You should know how easy the problem is now!
Good luck!
Input
Input contains multiple test cases. The first line of the input is a single integer T which is the number of test cases. T test cases follow. Each test case contains an integer N (1<=N<=1000 the number of integers to be sorted) and then N integers follow in the same line.
It is guarantied that all integers are in the range of 32-int.
Output
For each case, print the sorting result, and one line one case.
Sample Input
2
3 2 1 3
9 1 4 7 2 5 8 3 6 9
Sample Output
1 2 3
1 2 3 4 5 6 7 8 9

Arrays.sort(a);

```/*
* 冒泡排序
*/
public static int[] sort(int[] a){
// Arrays.sort(a);
for(int i=0;i<a.length-1;i++){
for(int j=a.length-1;j>i;j--){
if(a[j]<a[j-1]){
int temp = a[j];
a[j] = a[j-1];
a[j-1] = temp;
}
}
}
return a;
}
```
2、DNA Sorting
Description
One measure of ``unsortedness'' in a sequence is the number of pairs of entries that are out of order with respect to each other. For instance, in the letter sequence ``DAABEC'', this measure is 5, since D is greater than four letters to its right and E is greater than one letter to its right. This measure is called the number of inversions in the sequence. The sequence ``AACEDGG'' has only one inversion (E and D)---it is nearly sorted---while the sequence ``ZWQM'' has 6 inversions (it is as unsorted as can be---exactly the reverse of sorted).
You are responsible for cataloguing a sequence of DNA strings (sequences containing only the four letters A, C, G, and T). However, you want to catalog them, not in alphabetical order, but rather in order of ``sortedness'', from ``most sorted'' to ``least sorted''. All the strings are of the same length.
Input
The first line contains two integers: a positive integer n (0 < n <= 50) giving the length of the strings; and a positive integer m (0 < m <= 100) giving the number of strings. These are followed by m lines, each containing a string of length n.
Output
Output the list of input strings, arranged from ``most sorted'' to ``least sorted''. Since two strings can be equally sorted, then output them according to the orginal order.
Sample Input
10 6
AACATGAAGG
TTTTGGCCAA
TTTGGCCAAA
GATCAGATTT
CCCGGGGGGA
ATCGATGCAT
Sample Output
CCCGGGGGGA
AACATGAAGG
GATCAGATTT
ATCGATGCAT
TTTTGGCCAA
TTTGGCCAAA

```/*
* DNA sorting
*/
public static void test3(String[] dnas){
Map invertions = new HashMap();
// 计算每个字符串的乱序数量
for(int i=0;i<dnas.length;i++){
invertions.put(dnas[i], count(dnas[i]));
}
// 排序
for(int i=0;i<dnas.length-1;i++){
for(int j=dnas.length-1;j>i;j--){
if((Integer)(invertions.get(dnas[j]))<(Integer)(invertions.get(dnas[j-1]))){
String temp = dnas[j];
dnas[j] = dnas[j-1];
dnas[j-1] = temp;
}
}
}
// 输出
for(String temp:dnas){
System.out.println(temp+invertions.get(temp));
}

}
// 计算无序性
public static int count(String dna){
int sum=0;
for(int i=0;i<dna.length()-1;i++){
char temp = dna.charAt(i);
for(int j=i+1;j<dna.length();j++){
if(temp>dna.charAt(j))
sum++;
}
}
return sum;
}
```

```/*
* 计算无序性（改进算法）
*/
publicstaticint count2(String dna){
int count[] = newint[4];
Arrays.fill(count,0);
int sum=0;
for(int i=dna.length()-1;i>=0;i--){
char temp = dna.charAt(i);
switch(temp){
case'A':
count[0]++;
break;
case'C':
count[1]++;
sum = sum+count[0]; // 加上C后面的A的个数
break;
case'G':
count[2]++;
sum = sum+count[0]+count[1]; // 加上G后面的A和C的数量
break;
case'T':
count[3]++;
sum = sum+count[0]+count[1]+count[2]; // 加上G后面的A、C和G的数量
break;
}
}
return sum;
}
```

```class DNA implements Comparable{
String content;
int unsortness;
DNA(String content,int unsortness){
this.content = content;
this.unsortness = unsortness;
}
@Override
public int compareTo(Object o) {
return unsortness-((DNA)o).unsortness;
}
}
```

```/*
* DNA sorting
*/
publicstaticvoid test3(String[] dnas){
DNA newDnas[] = new DNA[dnas.length];
for(int i=0;i<dnas.length;i++){
newDnas[i] = new DNA(dnas[i],count(dnas[i]));
}
Arrays.sort(newDnas); // 替换了原来的排序算法
// 输出
for(DNA temp:newDnas){
System.out.println(temp.content+temp.unsortness);
}
}
```