Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. Algorithms
  4. searching and order of aalgorithm [modified]

searching and order of aalgorithm [modified]

Scheduled Pinned Locked Moved Algorithms
algorithmsdata-structuresquestion
13 Posts 5 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • M Member 4194593

    Alan, With O(n lg(n)) time to sort, doesn't that use up all of the time allotted (overall time was to be O(n lg(n)))? Wouldn't a B+ tree sort speed up that somewhat to leave a little time for the combinatorial arithmetic? With a B+ tree (n-ary tree) you eliminate many wrong values instead of just two at each step as is done in a binary tree. Once the array of values was sorted, the B+ tree search would also find the correct value quicker than O(n lg(n)) as would be the case using a binary search for the second value. Dave.

    I Offline
    I Offline
    Ian Shlasko
    wrote on last edited by
    #4

    Big O notation is a measure of the execution time/complexity relative to the input size. Hence, O(n log(n)) means that the complexity in whatever unit is equal to C * n * log(n) where C is a constant. If you add an extra statement to your inner loop, you're increasing C without increasing the Big O complexity. We say it this way, because all we're trying to measure is how much longer it would take if, say, we doubled the input size. For an O(n) algorithm, doubling the input will double the complexity. So in essence... O(n log(n)) + O(n log(n)) = O(n log(n))

    Proud to have finally moved to the A-Ark. Which one are you in? Developer, Author (Guardians of Xen)

    L M 2 Replies Last reply
    0
    • I Ian Shlasko

      Big O notation is a measure of the execution time/complexity relative to the input size. Hence, O(n log(n)) means that the complexity in whatever unit is equal to C * n * log(n) where C is a constant. If you add an extra statement to your inner loop, you're increasing C without increasing the Big O complexity. We say it this way, because all we're trying to measure is how much longer it would take if, say, we doubled the input size. For an O(n) algorithm, doubling the input will double the complexity. So in essence... O(n log(n)) + O(n log(n)) = O(n log(n))

      Proud to have finally moved to the A-Ark. Which one are you in? Developer, Author (Guardians of Xen)

      L Offline
      L Offline
      Luc Pattyn
      wrote on last edited by
      #5

      O(IC) ;)

      Luc Pattyn


      I only read code that is properly indented, and rendered in a non-proportional font; hint: use PRE tags in forum messages


      I 1 Reply Last reply
      0
      • L Luc Pattyn

        O(IC) ;)

        Luc Pattyn


        I only read code that is properly indented, and rendered in a non-proportional font; hint: use PRE tags in forum messages


        I Offline
        I Offline
        Ian Shlasko
        wrote on last edited by
        #6

        :)

        Proud to have finally moved to the A-Ark. Which one are you in? Developer, Author (Guardians of Xen)

        1 Reply Last reply
        0
        • I Ian Shlasko

          Big O notation is a measure of the execution time/complexity relative to the input size. Hence, O(n log(n)) means that the complexity in whatever unit is equal to C * n * log(n) where C is a constant. If you add an extra statement to your inner loop, you're increasing C without increasing the Big O complexity. We say it this way, because all we're trying to measure is how much longer it would take if, say, we doubled the input size. For an O(n) algorithm, doubling the input will double the complexity. So in essence... O(n log(n)) + O(n log(n)) = O(n log(n))

          Proud to have finally moved to the A-Ark. Which one are you in? Developer, Author (Guardians of Xen)

          M Offline
          M Offline
          Member 4194593
          wrote on last edited by
          #7

          I understand Big O, but in this case, after the sort, for each item, you do a binary search on the array looking for a value that when added to the initial value equals some X. Anything you do to speed up this process will help more than just removing some instruction from the inner loop. You have just added another (n (n log(n))) timeslice to the inner loop, not just some constant. Dave.

          I 1 Reply Last reply
          0
          • M Member 4194593

            I understand Big O, but in this case, after the sort, for each item, you do a binary search on the array looking for a value that when added to the initial value equals some X. Anything you do to speed up this process will help more than just removing some instruction from the inner loop. You have just added another (n (n log(n))) timeslice to the inner loop, not just some constant. Dave.

            I Offline
            I Offline
            Ian Shlasko
            wrote on last edited by
            #8

            But you're not doing a binary search on each item. You're doing one sort and one binary search. Think of it this way... Say the sort and the next operation were each O(n)... Obviously they wouldn't be, but as an example. Then you're doing an O(n) followed by an O(n), or 2 * O(n). But in Big O, we eliminate constants, so it simplifies to just O(n)... If the two operations are of different complexity, we take the larger one... If it was O(n log(n)) followed by O(n), we'd say the Big O was O(n log n).

            Proud to have finally moved to the A-Ark. Which one are you in? Developer, Author (Guardians of Xen)

            M 1 Reply Last reply
            0
            • I Ian Shlasko

              But you're not doing a binary search on each item. You're doing one sort and one binary search. Think of it this way... Say the sort and the next operation were each O(n)... Obviously they wouldn't be, but as an example. Then you're doing an O(n) followed by an O(n), or 2 * O(n). But in Big O, we eliminate constants, so it simplifies to just O(n)... If the two operations are of different complexity, we take the larger one... If it was O(n log(n)) followed by O(n), we'd say the Big O was O(n log n).

              Proud to have finally moved to the A-Ark. Which one are you in? Developer, Author (Guardians of Xen)

              M Offline
              M Offline
              Member 4194593
              wrote on last edited by
              #9

              Ian Shlasko wrote:

              You're doing one sort and one binary search.

              But you are not doing that, you are doing the binary search n times looking for a pair that add up to the constant X. Dave.

              I 1 Reply Last reply
              0
              • A Alan Balkany

                1. Sort the array in ascending order. (This is O(n lg(n)). 2. Use two integer indexes, i & j, that point to the array elements to add. The first one (i) starts at 0 (the index of the lowest number). 3. Do a binary search in the array to find the array element that is closest to x when added to the element at index 0 (j). If array [i] + array [j] == x, we're done. 4. Loop: Advance i to the next element. 5. While array [i] + array [j] > x, decrease j. If array [i] + array [j] == x, we're done. 6. When array [i] + array [j] < x, go to Step 4. When i >= j, there's no solution and we're done.

                L Offline
                L Offline
                Luc Pattyn
                wrote on last edited by
                #10

                I agree with your algo, however I would rephrase it in a more symmetric way, making the O(n) part more clear:

                1. Sort the array in ascending order
                2. let int i=0 and int j=n-1
                3. if i>=j then search is over
                4. calculate sum=array[i]+array[j]
                5. if sum<x, increment i and goto (3)
                6. if sum>x, decrement j and goto (3)
                7. since sum==x, solution found (either stop; or increment i, decrement j and goto 3)

                (1) is O(n ln(n)) (2)-(7) is O(n) as i and j move towards each other in steps of 1 hence overall O(n ln(n)) :)

                Luc Pattyn


                I only read code that is properly indented, and rendered in a non-proportional font; hint: use PRE tags in forum messages


                K 1 Reply Last reply
                0
                • M Member 4194593

                  Ian Shlasko wrote:

                  You're doing one sort and one binary search.

                  But you are not doing that, you are doing the binary search n times looking for a pair that add up to the constant X. Dave.

                  I Offline
                  I Offline
                  Ian Shlasko
                  wrote on last edited by
                  #11

                  Sorry, mixed it up a bit, but I'm still right about the complexity. A binary search is O(log n)... And we'd be doing that n times... Hence, O(n log(n)) for all of the searches. But this is still done AFTER the sort, not inside it, so it adds to the sort instead of multiplying with it.

                  Proud to have finally moved to the A-Ark. Which one are you in? Developer, Author (Guardians of Xen)

                  A 1 Reply Last reply
                  0
                  • I Ian Shlasko

                    Sorry, mixed it up a bit, but I'm still right about the complexity. A binary search is O(log n)... And we'd be doing that n times... Hence, O(n log(n)) for all of the searches. But this is still done AFTER the sort, not inside it, so it adds to the sort instead of multiplying with it.

                    Proud to have finally moved to the A-Ark. Which one are you in? Developer, Author (Guardians of Xen)

                    A Offline
                    A Offline
                    Alan Balkany
                    wrote on last edited by
                    #12

                    You had it right the first time -- Only one binary search is done. It's done to find the upper bound of number pairs that could possibly add up to x.

                    1 Reply Last reply
                    0
                    • L Luc Pattyn

                      I agree with your algo, however I would rephrase it in a more symmetric way, making the O(n) part more clear:

                      1. Sort the array in ascending order
                      2. let int i=0 and int j=n-1
                      3. if i>=j then search is over
                      4. calculate sum=array[i]+array[j]
                      5. if sum<x, increment i and goto (3)
                      6. if sum>x, decrement j and goto (3)
                      7. since sum==x, solution found (either stop; or increment i, decrement j and goto 3)

                      (1) is O(n ln(n)) (2)-(7) is O(n) as i and j move towards each other in steps of 1 hence overall O(n ln(n)) :)

                      Luc Pattyn


                      I only read code that is properly indented, and rendered in a non-proportional font; hint: use PRE tags in forum messages


                      K Offline
                      K Offline
                      khomeyni
                      wrote on last edited by
                      #13

                      in the name of god hello and thanks for your answers. it is the code i wrote in c++: int main(){ //here we declare variables cin< modified on Friday, October 30, 2009 8:20 AM

                      1 Reply Last reply
                      0
                      Reply
                      • Reply as topic
                      Log in to reply
                      • Oldest to Newest
                      • Newest to Oldest
                      • Most Votes


                      • Login

                      • Don't have an account? Register

                      • Login or register to search.
                      • First post
                        Last post
                      0
                      • Categories
                      • Recent
                      • Tags
                      • Popular
                      • World
                      • Users
                      • Groups