2

I am using Insertion and Merge sort and trying to indicate the intersection point on the graph below. I found a possible solution on rosettacode, but a bit confusing for a newbie like me... Where should I look, do you guys know how to indicate it in Julia?

The output would is like enter image description here

I am trying to show something like this enter image description here

Shayan
  • 5,165
  • 4
  • 16
  • 45
  • 1
    Can you please specify if your question is to: 1) find the coordinates of the point, or 2) you know the coordinate, but want to add its location to the plot along with an arrow and "Point of intersection" text, or both 1) and 2)? – Bogumił Kamiński Nov 25 '22 at 21:18
  • Hi, I actually do not have the exact intersection point coordinates. I want to point the intersection point with a marker (doesn't matter the color or shape). I am also struggling to convert milliseconds to integer... I know I am a newbie....... – wrisurcuriosity Nov 26 '22 at 11:46
  • @wrisurcuriosity, So the title and the last image in your question don't match what you actually want! – Shayan Nov 26 '22 at 11:48
  • Nice to have you here @Shayan – wrisurcuriosity Nov 26 '22 at 22:32
  • Welcome to SO! Can you reframe this question with a [minimal reproducible example](https://stackoverflow.com/help/minimal-reproducible-example)? As written there are several ways to approach this. e.g.: look for the index where values in the insertion sort time equals the values in merge sort times. There may also be edge cases (what should happen if the lines intersect at multiple points?) – Alexander L. Hayes Nov 30 '22 at 03:31

1 Answers1

-1

Your data has some noise, which means there might be more than one intersection point (all close). Assuming no noise, just two monotonically increasing functions, this should work:

    using Plots
    x = 1:5
    y1 = x .+ 5
    y2 = 3x 
    idx = argmin(abs.(y1-y2))
    plot(x, [y1, y2], label=["y1", "y2"], markershape=:circle)
    scatter!([x[idx]], [y1[idx]], label="intersection", markershape=:circle, markersize=10)

See the resulting plot here.

Note that this method gives you 1/4 possible points closest to the intersection. The intersection itself is not defined for functions only defined from discrete points. By changing the order of y1 and y2 in the code above you can get all 4 points. For fine enough discretization using one of those 4 points might be good enough for your needs.

A better approximation would be to use those 4 points to fit 2 lines, for y1 and y2, and find the intersection of those two lines analytically (e.g. with the equations y1=m1*x+b1, ...). But I leave this to you ;)

Shayan
  • 5,165
  • 4
  • 16
  • 45
  • Your code doesn't lead to showing the intersection correctly, according to the plot image. – Shayan Nov 25 '22 at 18:05
  • I updated the output. There is only one intersection, your comment give me a hint, thank you for your contribution. – wrisurcuriosity Nov 25 '22 at 18:24
  • @Shayan given discrete data points there is no exact intersection. The plot lines assume linear interpolation between points, but are not part of the data. Only the markers are (ignore the line between points). I do give the next steps (not shown) if you want to find the intersection point assuming linear interpolation. – Carlos A. Michelén Ströfer Nov 25 '22 at 18:39
  • @Shayan that's why I did such a "zoomed-in" version of the plot. – Carlos A. Michelén Ströfer Nov 25 '22 at 18:41