1

This a question I found in the Wikipedia site (I want to learn sort algorithms very well). Anyway, this is a question - could you explain to me how I can show it?

Exercise: Show that Algorithm Insertion Sort (A) runs in time O(n + I) given that I is the number of inversions in the array A.

Jonathan Leffler
  • 730,956
  • 141
  • 904
  • 1,278
user355002
  • 871
  • 2
  • 10
  • 22
  • I am curious! Which site is this that goes into such detail? Can you please provide a link? It will probably help future questioners like you. –  Jun 20 '10 at 15:57
  • I think you should provide much more information about the algorithm ... – TooAngel Jun 20 '10 at 15:59
  • sure this is the link : http://en.wikipedia.org/wiki/Insertion_sort – user355002 Jun 20 '10 at 16:04
  • I was talking about this and the other questions you have been posting. Your wiki link has made me suspicious. To me it now looks like you are just posting homework questions. If it is homework, just say so (i.e. tag it as such). If it is not, I apologize. –  Jun 20 '10 at 18:05
  • it is not!! i read this kinds of sites and I will have some questions and I ask them as an exercise because I need people be more helpful also this site is just one of those sites :) – user355002 Jun 20 '10 at 18:26
  • being helpful is a two way street... – RandomUs1r Jun 13 '14 at 22:46

1 Answers1

5

Look at the Implementation and Analysis sections of this page.

Consider the algorithm presented there:

private static void insertionsort()
{
    int i, j, t;
    for (i=1; i<n; i++)
    {
        j=i;
        t=a[j];
        while (j>0 && a[j-1]>t)
        {
            a[j]=a[j-1];
            j--;
        }
        a[j]=t;
    }
}

Notice that the while loop runs for v[i] iterations, where v[i] is the number of inversions caused by element i. This should make the proof there very easy to understand.

IVlad
  • 43,099
  • 13
  • 111
  • 179
  • also it seams that you know algorithms and every thing about data structure could I have your yahoo ID for more helping?? :) – user355002 Jun 20 '10 at 16:40
  • Sorry, I don't use yahoo, but you can ask your questions on this site. That way you have much better chances of getting an answer quicker. Even if I don't know something, someone else definitely will. – IVlad Jun 20 '10 at 16:44
  • also can I use this algorithm for sorting characters? – user355002 Jun 20 '10 at 18:24
  • Yes, you can. Characters are actually integers, so you can compare them just as you would compare integers. – IVlad Jun 20 '10 at 18:36
  • aha it means that the algorithm will compare their ASCII? – user355002 Jun 20 '10 at 18:40