Data structure is the scheme of organizing related


















![Example: А[10], key=16. left= 0 right= 9 mid = 4 16>A[mid] left= 5 right= Example: А[10], key=16. left= 0 right= 9 mid = 4 16>A[mid] left= 5 right=](https://slidetodoc.com/presentation_image_h2/7682de897a284f8d6643c345259f485f/image-19.jpg)
![Binary Search int search_bin(int a[], int left, int right, int key) {int mid; int Binary Search int search_bin(int a[], int left, int right, int key) {int mid; int](https://slidetodoc.com/presentation_image_h2/7682de897a284f8d6643c345259f485f/image-20.jpg)
- Slides: 20
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 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(n 2) quadratic O(n 3) cubic O(nc) polynomial O(cn) exponential (non polynomial) O(2 n) exponential
Algorithmic run time expansion
2 n n 3/2 5 n 2 100 n
N log 2 N 2 1 8 16 Nlog 2 N N 2 N 3 2 N 2 4 8 4 3 24 64 512 256 4 64 256 4096 65536
If f(n) = 8 log(n) + 5(log(n))3 + 7 n + 3 n 2 + 6 n 3 then f(n) = O(n 3).
Sequence of statements statement 1; statement 2; . . . 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 2) O(1) O(n-i)
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)
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 right= 9 mid = 7 16<A[mid] left= 5 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; }