IWETHEY v. 0.3.0 | TODO
1,095 registered users | 0 active users | 1 LpH | Statistics
Login | Create New User
IWETHEY Banner

Welcome to IWETHEY!

New Fun with complexity

My prof has a sense of humour. We're supposed to analyse the following method for complexity; it's a method to sort an array of ints, written in java.

\r\n\r\n
public static void slowSort(int theArray[]) {\r\n  // For each value of i, find the largest value in theArray[0..i]\r\n  // and move it into the last position -- kind of like selection sort,\r\n  // but the details are different.\r\n  for (int i = theArray.length-1; i > 0; i--) {\r\n    int maxIndex = 0; // the index of the largest value in theArray[0..i]\r\n    for (int j = 0; j <= i; j++) {\r\n      // Check to see if theArray[j] is the largest value in theArray[0..i]\r\n      // Do this by comparing it will all other values in this range\r\n      boolean isMax = true;\r\n      for (int k = 0; k <= i; k++) {\r\n        if (theArray[k] > theArray[j])\r\n          isMax = false;\r\n      } // end for\r\n      if (isMax)\r\n        maxIndex = j;\r\n    } // end for\r\n    // move theArray[maxIndex] into position theArray[i] by moving\r\n    // elements down\r\n    int maxElement = theArray[maxIndex];\r\n    for (int j = maxIndex; j < i; j++)\r\n      theArray[j] = theArray[j+1];\r\n    theArray[i] = maxElement;\r\n  } // end for\r\n} // end slowSort
\r\n\r\n

Now, I haven't done any math in around ten years, so I'm running at a bit of a disadvantage here. However, it seems to me that the nastiest part of the code is the k loop; it gets executed a LOT, though the methodology for slopping the array elements around at the very end of the i loop is also a complete dog.

\r\n\r\n

It looks to me that the dominant factor affecting complexity is the k loop. My quick feel for it is that this makes the complexity O(n*n), hand waving the various other bits and pieces. Anybody can tell me if I'm way off...?

--\r\n-------------------------------------------------------------------\r\n* Jack Troughton                            jake at consultron.ca *\r\n* [link|http://consultron.ca|http://consultron.ca]                   [link|irc://irc.ecomstation.ca|irc://irc.ecomstation.ca] *\r\n* Kingston Ontario Canada               [link|news://news.consultron.ca|news://news.consultron.ca] *\r\n-------------------------------------------------------------------
New Just doing a cursory glance at it...
...it looks like O(n^3). There are 3 nested for loops. Yes, they don't all have the same boundary conditions, and that's where I might be getting off a bit. But a triply-nested for-loop sounds like n-cubed to me.

Wonderfully efficient. ;-)
-YendorMike

[link|http://www.hope-ride.org/|http://www.hope-ride.org/]
New Yeah, I was wondering about that.
The i loop makes the j loop run i times, where i is going to be equal to n + (n-1) + (n-2) + ... + 1 times. In turn, each run of the j loop makes the k loop run i the same number of times. The loop at the bottom for moving the array members around should run on average n/2 times; it's not important, right? So, really you're looking at (n + (n-1) + (n-2) + ... + 1)^2. Yes...?
--\r\n-------------------------------------------------------------------\r\n* Jack Troughton                            jake at consultron.ca *\r\n* [link|http://consultron.ca|http://consultron.ca]                   [link|irc://irc.ecomstation.ca|irc://irc.ecomstation.ca] *\r\n* Kingston Ontario Canada               [link|news://news.consultron.ca|news://news.consultron.ca] *\r\n-------------------------------------------------------------------
Expand Edited by jake123 March 27, 2003, 03:30:15 PM EST
New OK, a bit less of a cursory glance...
// As always, assume n elements\nfor (int i = theArray.length-1; i > 0; i--) { // This is n\n  for (int j = 0; j <= i; j++) { // This is n\n    for (int k = 0; k <= i; k++) { // And this is n\n    }\n  }\n  for (int j = maxIndex; j < i; j++) { // This is also n, but it's not nested\n  }\n}
Essentially, that is what the loop boils down to.

The first three loops are N, and they're all nested. So that gives you N^3. There's also that second loop. That's nested at the second level. So taking that with the "i" loop, that would make the loop (ignoring the other parts) N^2. So you've got something that's O(N^3) + O(N^2). The cubed strongly overpowers the squared, so it's common just to toss that out.

-YendorMike

[link|http://www.hope-ride.org/|http://www.hope-ride.org/]
New Yes, I've come to that conclusion too
Thanks for the discussion on it; it helped me in my thinking. I'm writing the test program now to actually prove that this is the case.
--\r\n-------------------------------------------------------------------\r\n* Jack Troughton                            jake at consultron.ca *\r\n* [link|http://consultron.ca|http://consultron.ca]                   [link|irc://irc.ecomstation.ca|irc://irc.ecomstation.ca] *\r\n* Kingston Ontario Canada               [link|news://news.consultron.ca|news://news.consultron.ca] *\r\n-------------------------------------------------------------------
New Just so you know
The result of my instrumentation showed it to be n^3. The results were good to a pretty high degree of precision; the IBM JDK was really consistent once I turned of the JIT compiler. I used the 1.1.8 JVM for my test runs.

Thanks again for the help, Mike!
--\n-------------------------------------------------------------------\n* Jack Troughton                            jake at consultron.ca *\n* [link|http://consultron.ca|http://consultron.ca]                   [link|irc://irc.ecomstation.ca|irc://irc.ecomstation.ca] *\n* Kingston Ontario Canada               [link|news://news.consultron.ca|news://news.consultron.ca] *\n-------------------------------------------------------------------
New Glad to help! :)
-YendorMike

[link|http://www.hope-ride.org/|http://www.hope-ride.org/]
New my favourite sort algorithm is...
a [link|http://perlmonks.org/index.pl?node_id=96627|Random Permutation Sort]. Conceptually, it's very simple. Just scramble all the elements and check to see if they're sorted. How hard is that? Yet the order of complexity is N!. You wouldn't want to use this algorithm for sorting large arrays. Large being more than 5 elements. :-)
Have fun,
Carl Forde
New Freak



"Packed like lemmings into shiny metal boxes.
Contestants in a suicidal race."
    - Synchronicity II - The Police
     Fun with complexity - (jake123) - (8)
         Just doing a cursory glance at it... - (Yendor) - (1)
             Yeah, I was wondering about that. - (jake123)
         OK, a bit less of a cursory glance... - (Yendor) - (3)
             Yes, I've come to that conclusion too - (jake123) - (2)
                 Just so you know - (jake123) - (1)
                     Glad to help! :) -NT - (Yendor)
         my favourite sort algorithm is... - (cforde) - (1)
             Freak -NT - (tuberculosis)

Sitting member of the standing committee. Right.
107 ms