Question

In: Computer Science

Can I get a breakdown line by line of the time complexity, I know it is...

Can I get a breakdown line by line of the time complexity, I know it is O(NlogN) but I am trying to better understand time complexity as a whole.

Input :- N , Array[N]

                               function findSmallestPair()

                               sort the Array

                               int minimumDiff := INFINITY , A := -1 , B := -1;  

                               for(int i : 1 to N-1)

                               begin for

               currentDiff = Array[i]-Array[i-1]

               if(currentDiff < minimumDiff)

               begin if

                              A := Array[i-1];

                              B := Array[i];

                              minimumDiff = currentDiff;

                   end if

              end for

           return {A,B}; end function                      

Solutions

Expert Solution

Time Complexity:

function findSmallestPair()

                               sort the Array //Using standard sort method generally it takes O(nlogn) time(eg:Merge Sort)

                               int minimumDiff := INFINITY , A := -1 , B := -1; //Constant Operation O(1) time

                               for(int i : 1 to N-1) //Loop iterate from1 to n-1 ,So Time complexity O(n)

                               begin for

               currentDiff = Array[i]-Array[i-1] // Constant Operation O(1) time

               if(currentDiff < minimumDiff) //If also take constant operation time O(1)

               begin if

                              A := Array[i-1]; //O(1)

                              B := Array[i]; //O(1)

                              minimumDiff = currentDiff; //O(1)

                   end if

              end for

           return {A,B}; end function                     

So we can easily find that the maximum time complexity is O(nogn) .

If the sort line is removed from your code then the time complexity is O(n).  


Related Solutions

I know the what the answers are but I don't know how to get them. Can...
I know the what the answers are but I don't know how to get them. Can you please explain the process? Thank you. Part VII. Discontinued Operations and Earnings per Share (11 points) Todd Corporation had pre-tax income for 2017 of $2,500,000. On December 31, 2017, Boyd disposed of a component of its business that represented a strategic shift in operation. That component had a Loss on Discontinued Operations of $450,000 (pre-tax). Boyd received $1,000,000 net cash proceeds from the...
Can I get all of the question answered in a way that I know under which...
Can I get all of the question answered in a way that I know under which sub question the answer goes please. Can I get all of the questions answered in a way that I know how it fits in the question. Some parts are explanations. Thank you. Q. When a pink aqueous solution of potassium permanganate, faintly acidified with dilute sulfuric acid was treated with 10% aq. hydrogen peroxide, the reaction took place with the evolution of gas bubbles,...
Infile and getline - how can I change this code to get the whole line from...
Infile and getline - how can I change this code to get the whole line from a text file? Right now it only gets the first word each time it reads. NUMBER can be any integer. This code gets strings that are in multiple lines from a text file. Some words are separated by whitespace and that makes the input wrong. It only works if there is only one word. infile >> arrayOne[dive] >> arrayTwo[dive]; while (infile && dive< NUMBER)...
i need to find complexity and cost and runtime for each line of the following c++...
i need to find complexity and cost and runtime for each line of the following c++ code : // A C++ program for Dijkstra's single source shortest path algorithm. // The program is for adjacency matrix representation of the graph #include <limits.h> #include <stdio.h> // Number of vertices in the graph #define V 9 // A utility function to find the vertex with minimum distance value, from // the set of vertices not yet included in shortest path tree int...
Time Complexity: Build a table in Excel and record the time complexity taken by linear and...
Time Complexity: Build a table in Excel and record the time complexity taken by linear and binary search to look for an element with the following array sizes: 10, 50, 100, 200, 500, 700, 1000. Compare the performance of both algorithms highlighting the time complexity for both algorithms when run on small input size and when run on large input size. To accomplish this, load a list with the file data and then create a program that selects at random...
"Please can I get a feedback on this discussion post below" Can I get it in...
"Please can I get a feedback on this discussion post below" Can I get it in 2 hours please .thanks Tesla is a company recently in the news for a conflict of interest between the SEC and the CEO Elon Musk due to an interest to bring the company private by the CEO who shared on Twitter his short term plans. Production issues, a number of layoffs and the increasing demand for the electric vehicles were expressed by the CEO...
I want to know why I can not get N(should contain 1000 number) N<-vector() for (k...
I want to know why I can not get N(should contain 1000 number) N<-vector() for (k in 0:999){ y=vector() for(i in 0:999){ r1=runif(1) r2=runif(1) x1=-log(r1) x2=-log(r2) k=(x1-1)^2/2 if (x2 >= k){ r<-runif(1) if (r>0.5){ y[i]= x1 }else{ y[i]= -x1 } } y.1<- y[-which(is.na(y))] number<-length(y.1) } N[k]<-number } N
How can I know when I should use line graph, histogram, and steam-leaves? Is that gonna...
How can I know when I should use line graph, histogram, and steam-leaves? Is that gonna be mansioned in the text or I have to know by myself?
I would like to know the mortgage calculation including breakdown to come up with the monthly...
I would like to know the mortgage calculation including breakdown to come up with the monthly mortgage payment for the following: Price of Home: $550,000 Interest rate: 4.25 Length of loan: 30 years 0 down payment Please provide a written response to show calculation and not an excel spreadsheet. I need to learn the actual written calculation. Thank you
Can I get some simple pseudocode (simple descriptions as exampled in top italicized line) added to...
Can I get some simple pseudocode (simple descriptions as exampled in top italicized line) added to this small section of Java code? Thumbs up always left for answers! ------------------------------------------------------------------------------------------------------- // Check to see if two bags are equals.    public boolean equals(LinkedBag<T> aBag) {        boolean result = false; // result of comparison of bags        if (this.numberOfEntries == aBag.numberOfEntries) {            if (numberOfEntries == 0) {                return true;           ...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT