Data structure – is the scheme of organizing related information
A linear data structure traverses the data elements sequentially, in which only one data element can directly be reached. (Ex: Arrays, Linked Lists) Non-Linear data structure: The data items are not arranged in a sequential structure. (Ex: Trees, Graphs) Hierarchical structure - a structure of data having several levels arranged in a treelike structure.
Data structure – is the scheme of organizing related information
Direct access: called “Random access” is faster than serial access Serial access: called “sequential access” from the begin to the required information
Big O notation Big O notation describes the asymptotic behavior of functions. Big O notation tells you how fast a function grows or declines. CPU (time) usage memory usage disk usage network usage Time complexity
Basic operations: one arithmetic operation (e.g., +, *). one assignment (e.g. x := 0) one test (e.g., x = 0) one read one write
Notation names O(1) constant O(log(n)) logarithmic O(n log n) O((log(n))c) polylogarithmic O(n) linear O(n2) quadratic O(n3) cubic O(nc) polynomial O(cn) exponential (non polynomial) O(2n) exponential
Algorithmic run time expansion
2n n3/2 5n2 100n
N log2N Nlog2N N2 N3 2N 2 1 4 8 3 24 64 512 256 16 4096 65536
If f(n) = 8 log(n) + 5(log(n))3 + 7n + 3 n2 + 6 n3 then f(n) = O(n3).
Sequence of statements ... statement k; total time = time(statement 1) + time(statement 2) + ... + time(statement k)
If-Then-Else if (cond) then block 1 (sequence of statements) else block 2 (sequence of statements) end if; max(time(block 1), time(block 2))
Loops for I in 1 .. N loop sequence of statements end loop; O(N)
Nested loops for I in 1 .. N loop for J in 1 .. M loop sequence of statements end loop; O(N * M).
(1) for (int i=0; i<n-1; i++) (2) for (int j=n-1; j>i; j--) (3) if (a[j-1]>a[j]) { (4) temp=a[j-1]; (5) a[j-1]=a[j]; (6) a[j]=temp; } O(n-i) O(1) O(n2)
Linear search A linear search sequentially moves through your list looking for a matching value. 1. The element is found, i.e. a[i] = x. 2. The entire array has been scanned, and no match was found. i=0; while (i<n && a[i]≠x) i++; O(n)
There are three conditions: А[mid]==key A[mid]<key A[mid]>key Binary Search The key idea is to inspect a middle element and to compare it with the search argument key. There are three conditions: А[mid]==key A[mid]<key A[mid]>key
Example: А[10], key=16. left= 0 right= 9 mid = 4 16>A[mid] left= 5 mid = 7 16<A[mid] right= 6 mid = 5 16=A[mid]
Binary Search int search_bin(int a[],int left, int right,int key) {int mid; int midval; while (left <=right) { mid=( left +right)/2; midval=a[mid]; if (midval==key) return mid; else if (key<midval) right=mid-1; else left =mid+1; } return -1;