0

Let's say I wish to compare the performance of bubble sort and selection sort and I have the time taken for the code to run for different number of inputs. The graph for each came out as follows.

The Bubble sort:

n=[]
for i in range(13):
    n.append(i*1000)
t=[0,1,5,12,21,31,45,61,81,102,125,160,191]
plt.plot(n,t,'s')
plt.plot(n,t)
plt.title('Time Complexity Analysis of Bubble Sort')
plt.xlabel('Number of Inputs')
plt.ylabel('Time Taken')

Graph for Bubble Sort generated

The Selection Sort:

n=[]
for i in range(13):
    n.append(i*1000)
t=[0,1,3,6,11,17,24,33,43,54,66,80,94]
plt.plot(n,t,'s')
plt.plot(n,t)
plt.title('Time Complexity Analysis of Selection Sort')
plt.xlabel('Number of Inputs')
plt.ylabel('Time Taken')

Graph for Selection sort generated

The Graph look pretty similar because the y upper bound for selection sort is 100 while that for Bubble sort is 200. I wish the graph for the selection sort to have the y-upperbound be 200 as well.

I googled and found the following method:

ax.set_ylim(top=200)

But this only works within the data entered extremes. If the data has the highest value less than 200 then it will show until the highest value of the data.

I wish to show the yaxis ticks until the 200 mark.

JohanC
  • 71,591
  • 8
  • 33
  • 66
  • 1
    You didn't assign a value to `ax`, so `ax.set_ylim()` doesn't affect your current plot. You can use `ax = plt.gca()` to make `ax` point to your current plot. Or you can use `plt.ylim(top=200)`, which also works on the current plot. – JohanC Jan 09 '23 at 19:07
  • @JohanC Thank you, it works now, I just had to use the `ax` and the `ax.set_ylim(top=200)` works as intended now. – Sukumar Rastogi Jan 16 '23 at 05:13

0 Answers0