DEV Community

abhishek
abhishek

Posted on

RAM model, Big O what's that?

Here a question I don't understand. Can anybody help with that?

Use RAM model to estimate the big-oh of the running time for the following code segment.


for(i = 1; i < n; i++){
   small_pos = i;
   smallest = Array[samll_pos];
   for(j = i + 1; J <= n; j++){
     if(Array[j] < smallest){
       small_pos = j;
       smallest = Array[small_pos]
     }
   }
}
Array[small_pos] = Array[i];
Array[i] = smallest;
Enter fullscreen mode Exit fullscreen mode

Top comments (2)

Collapse
 
kspeakman profile image
Kasey Speakman

Reference

The RAM (Random-access machine) is a set of assumptions. It assumes some basic operations exist and that they all take constant time. So you can ignore these operations and make your big-o estimations using only operations which vary based on the size of the input array, like for loops.

Collapse
 
helpbot profile image
Experimental Help Bot

The RAM model is a theoretical model of computation that is often used in computer science to analyze the running time of algorithms. In the RAM model, a computer has a single memory location for each possible value of the input size (typically represented by the variable n), and the computer can perform a limited set of basic operations on these memory locations in a single step.

Big O notation is a mathematical notation used to represent the upper bound of the running time of an algorithm. In other words, it gives an estimate of the maximum amount of time that the algorithm will take to run, as the input size grows.

To estimate the big-O of the running time for the code segment you provided, we first need to identify the basic operations that the algorithm is performing. In this case, the algorithm is performing the following basic operations:

  1. Assigning a value to the small_pos and smallest variables
  2. Comparing the value of Array[j] to the value of smallest
  3. Assigning a value to the small_pos and smallest variables
  4. Assigning a value to the Array[small_pos] and Array[i] variables

We can see that the algorithm performs these basic operations in a nested loop, with the outer loop iterating n-1 times, and the inner loop iterating n-i times for each iteration of the outer loop. This means that the total number of basic operations performed by the algorithm is equal to:

(n - 1) * (n - i)

Since the input size (n) is the variable that determines the running time of the algorithm, we can use big-O notation to represent the upper bound of this expression as n^2, which is known as "quadratic time". This means that the running time of the algorithm grows proportionally to the square of the input size, and the algorithm will take longer to run as the input size increases.

Therefore, we can say that the big-O of the running time for the code segment you provided is O(n^2), which means that the algorithm takes quadratic time to run.

I hope this helps! Let me know if you have any other questions.


I'm an experimental help bot that leverages ChatGPT. As such, the answers I provide may be incorrect, incomplete, or even nonsensical. I am not associated with OpenAI.

Please reply to my comment(s) with your own corrections and feedback.