DNA Sorting

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”. If two or more strings are equally sorted, list them in the same order they are in the input file.

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;
using System.Collections;

namespace DNASorting
{
    class DNASorting
    {
        static void Main(string[] args)
        {
            try
            {
                //Reading the input strings from a file C:\input.txt
                StreamReader sr = new StreamReader(@"C:\input.txt");
                string str = sr.ReadLine();
                ArrayList inputString = new ArrayList();
                Console.WriteLine("Sample Input:");
                Console.WriteLine("=============");
                for (str = sr.ReadLine(); str != null; str = sr.ReadLine())
                {
                    inputString.Add(str);
                    Console.WriteLine(str);
                }
                sr.Close();
                //Sorting of the array list based on sortedness
                inputString = Sort(inputString);
                //Print the sorted array list
                Console.WriteLine("Sample Output:");
                Console.WriteLine("==============");
                for (int i = 0; i < inputString.Count; i++)
                {
                    Console.WriteLine(inputString[i]);
                }
            }
            catch (Exception e)
            {
                Console.WriteLine(e.Message);
            }
        }
        public static int Sortedness(string str)
        {
            int count = 0;
            for (int i = 0; i < str.Length; i++)
            {
                for (int j = i + 1; j < str.Length; j++)
                {
                    if (string.Compare(str[i].ToString(),str[j].ToString()) > 0)
                    {
                        count++;
                    }
                }
            }
            return count;
        }
        public static int Compare(string str1, string str2)
        {
            return (Sortedness(str1) – Sortedness(str2));
        }
        public static ArrayList Sort(ArrayList al)
        {
            for (int i = 0; i < al.Count; i++)
            {
                for (int j = i + 1; j < al.Count; j++)
                {
                    if (Compare(al[i].ToString(), al[j].ToString()) > 0)
                    {
                        string temp = al[i].ToString();
                        al[i] = al[j];
                        al[j] = temp;
                    }
                }
            }
            return al;
        }
    }
}

Sample Input:
=============
AACATGAAGG
TTTTGGCCAA
TTTGGCCAAA
GATCAGATTT
CCCGGGGGGA
ATCGATGCAT
Sample Output:
==============
CCCGGGGGGA
AACATGAAGG
GATCAGATTT
ATCGATGCAT
TTTTGGCCAA
TTTGGCCAAA
Press any key to continue . . .

Advertisements
This entry was posted in Information Technology. Bookmark the permalink.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s