tags:

views:

47

answers:

1

Hi this is my question and I write an algorithm for it I want to know that this algorithm is correct?? thanks!!

Question: *a numbers of arrays are given to me and I should sort them based on "they are ordered" , for example an array that is in the right order comes at first, and an array that it elements are in the reverse order comes at the end. Suppose that the elements of arrays are made of letters A, C, G, T . Including the number of input arrays (n), length (m) and the name of arrays . *

my algorithm:

Algorithm Sort(n,arr(One)[1,…,m],arr(Two)[1,…m],…arr(n)[1,…,m])

for i<--1 to n
         m<-- SumTheNumberOfInversions(arr(i))
         v[i] = m
Arrays.sort(v[i])
j <--i
for j<-- 1 to n
     return arr(j)
//end of Sort algorithm
---------------------------------------------
Algorithm SumTheNumberOfInversions(arr)
Output: sum of the numbers of inversions 
{
    int i, j, t,numberOfInversions=0;
    for (i=1; i<n; i++)
    {
        j=i;
        t=arr[j];
        while (j>0 && arr[j-1]>t)
        {
            arr[j]=arr[j-1];
            numberOfInversions++;
            j--;
        }
        arr[j]=t;
    }
     return numberOfInversions;
}
+1  A: 

Sounds like you need an abstract data type with two members one to hold an array and the other for a measure of its unsortedness. Create a list of these types, one for each array. Iterate through and populate the unsortedness member (there's lots of ways you could measure this) Sort the ADT by the unsortedness field.

Daniel