IB Computer Science/Abstract Data Structures and Algorithms

From Wikibooks, the open-content textbooks collection

Jump to: navigation, search
Please note: Any IB syllabus statements included here are NOT under any free license and remain property of the IBO. They are reproduced here for personal study purposes only.

Return to International Baccalaureate

Return to IB Computer Science

Contents

[edit] Abstract Data Structures and Algorithms

[edit] Fundamentals

[edit] Static data structures

[edit] Dynamic data structures

[edit] Objects in problem solutions

[edit] Recursion

Example (Java):

public static int factorial(int num) {

   int ans;
   if(num==1)
      ans=1;
   else
      ans=factorial(num-1) * num;
   return ans;

}

This is a simple code to represent the value of factorial.

[edit] Algorithm Evaluation

[edit] Efficiency of Algorithms

To optimize algorithms, one must first know whether a certain algorithm is better or worse than another one. In this case, better can mean that the algorithm processes data faster or that less RAM is used. To measure data processing speed, one can use Big O notation.

Big O notation is always in the form of O(expression). The expression inside the parentheses is different for different efficiencies. It almost always contains n, the number of items of data to be processed. The Big O notation of a linear search on an array (of size n) is O(n). This means that doubling the array size will, on average, double the time for the linear search to complete.

Big O notation does not, however, indicate exactly how efficient an algorithm is. It only indicates how much the time spent increases when n is very large and is increased. For instance, if a certain algorithm takes 0.5n + 2 milliseconds for each item of data, it will still have a Big O notation of O(n). This is because when n gets to, say, one trillion, the added 2 will have almost no effect, so it is discarded. In the same way, all coefficients of n are discarded.

Other examples of Big O notation include O(n2) for bubble and selection sort and O(logn) for binary search. Note that binary search’s Big O notation is logn instead of log2n, because logarithms of different bases increase in approximately the same way when n is large.