ACM (5) -- sorting

Source: Internet
Author: User
Tags element groups

Some ACM questions require some basic data structures. The following describes the sorting-related content first.
1. Basic sorting
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 waking night.
Give you some integers, your task is to sort these number ascending (ascending ).
You shoshould 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
Sort the given sequence of numbers. Use the basic Sorting Algorithm of the data structure. You can use various sorting algorithms. In addition, the sorting method is provided in Java. You can use the Arrays. sort method. refer to the following code:
Arrays. sort ();
A is the array to be sorted.
The following code is provided for reference:
/*
* Bubble Sorting
*/
Public static int [] sort (int [] ){
// Arrays. sort ();
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;
}
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 ''ast sorted. Since two strings can be equally sorted, then output them according to the orginal order.
Sample Input
10 6
Aacatga.pdf
TTTTGGCCAA
TTTGGCCAAA
GATCAGATTT
CCCGGGGGGA
ATCGATGCAT
 
Sample Output
CCCGGGGGGA
Aacatga.pdf
GATCAGATTT
ATCGATGCAT
TTTTGGCCAA
TTTGGCCAAA
 
For a sequence, you can use the number of unordered element groups. For example, the sequence of letters DAABEC is 5, D is greater than the subsequent AABC, and E is greater than the subsequent C. AACEDGG has the disorder of 1, because only the order between E and D is messy.
Question requirement: for A given number of strings (the same length, consisting of A, C, G, and T), sort the strings from the most ordered to the unordered.
Solution: Calculate the order of each string and then sort it. Refer to the following code:
/*
* DNA sorting
*/
Public static void test3 (String [] dnas ){
Map invertions = new HashMap ();
// Calculate the number of disordered strings
For (int I = 0; I <dnas. length; I ++ ){
Invertions. put (dnas [I], count (dnas [I]);
}
// Sort
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;
}
}
}
// Output
For (String temp: dnas ){
System. out. println (temp +

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.