321 likes | 1.16k Views
This presentation covers what is time complexity analysis in data structures and algorithms. This Time Complexity tutorial aims to help beginners to get a better understanding of time complexity analysis. <br>Following topics covered in this presentation:<br>1. What is Time Complexity Analysis<br>2. How to Analyze Time Complexity<br>3. Asymptotic Notation in Time Complexity<br>4. General Rules for calculating Time Complexity<br>
E N D
Agenda Introduction 01 01 Types of Time Complexity 02 How to evaluate time complexity 03 Time complexity of algorithms 04 Conclusion 05
Introduction The time complexity of an algorithm is the amount of time it takes to run as a function of the length of the input. The length of the input determines how many operations the algorithm will do.
Introduction It will provide information about the variance (increase or decrease) in execution time as the number of operations in an algorithm increases or decreases.
Types of Time Complexity Constant Time Linear Time Logarithmic Time Quadratic Time
Types of Time Complexity 01 When an algorithm is not reliant on the input size n, it is said to have constant time with order O (1). The runtime will always be the same, regardless of the input size n. O(1) main() { int a; printf(“Enter value of a=”); scanf(“%d”,&a); printf(“a= %d”, a); } Constant Time
Types of Time Complexity 02 When the running time of an algorithm rises linearly with the length of the input, it is said to have linear time complexity. When a function checks all the values in an input data set, it is said to have Time complexity of order O(n). O(N) main() { int a[5]={2, 3, 5, 7, 9}; printf(“Elements of a=\n”); for(int i=0;i<5;i++) printf(“a= %d”, a); } Linear Time
Types of Time Complexity 03 When an algorithm lowers the amount of the input data in each step, it is said to have a logarithmic time complexity. Binary trees and binary search functions are some of the algorithms with logarithmic time complexity. O(log N) int binarySearch(int arr[], int l, int r, int x) { if (r >= l) { int mid = l + (r - l) / 2; if (arr[mid] == x) return mid; if (arr[mid] > x) return binarySearch(arr, l, mid - 1, x); return binarySearch(arr, mid + 1, r, x); }} Logarithmic Time
Types of Time Complexity 04 When the execution time of an algorithm rises non-linearly (n2) with the length of the input, it is said to have a quadratic time complexity. In general, nested loops fall into the quadratic time complexity order, where one loop takes O(n) and if the function contains loops inside loops, it takes O(n)*O(n) = O(n2). O(N2) for(int i=0; i<n; i++) { for(int j=0; j<n; j++) { printf(“%d”,arr[i][j]); } } Quadratic Time
How to Evaluate Time complexity int fib(int n) { int f[n + 2]; inti; f[0] = 0; f[1] = 1; for(i = 2; i <= n; i++) { f[i] = f[i - 1] + f[i - 2]; } return f[n]; } }; +1 +1 +1 +1 Total Time Taken= n+5 +n Time complexity = O(n+5) = O(n) +1
Time Complexity of Algorithms Insertion Sort Merge Sort Quick Sort Bubble Sort Linear Search Binary Search
Time Complexity of Algorithms 01 In the best case, the time complexity of Insertion Sort is O(n). In the worst case, the time complexity of Insertion Sort is O(n2). void insertionsort(int arr[], int n) { int i, key, j; for (i = 1; i < n; i++) { key = arr[i]; j = i - 1; while (j >= 0 && arr[j] > key) { arr[j + 1] = arr[j]; j = j - 1;} arr[j + 1] = key; } } Insertion Sort
Time Complexity of Algorithms 02 The time complexity of Merge Sort in the best case and worst case is O(nlogn). void mergeSort(int array[], int const begin, int const end) { if (begin >= end) return; // Returns recursively auto mid = begin + (end - begin) / 2; mergeSort(array, begin, mid); mergeSort(array, mid + 1, end); merge(array, begin, mid, end); } Merge Sort
Time Complexity of Algorithms 03 The time complexity of Quick Sort in the best case is O(nlogn). In the worst case, the time complexity is O(n^2). void quickSort(int arr[], int low, int high) { if (low < high) { int pi = partition(arr, low, high); // Separately sort elements before // partition and after partition quickSort(arr, low, pi - 1); quickSort(arr, pi + 1, high); } } Quick Sort
Time Complexity of Algorithms 04 In the best case, the time complexity of Bubble Sort is O(n). In the worst case, the time complexity of Bubble Sort is O(n2). void bubbleSort(int arr[], int n) { int i, j; for (i = 0; i < n-1; i++) // Last i elements are already in place for (j = 0; j < n-i-1; j++) if (arr[j] > arr[j+1]) swap(&arr[j], &arr[j+1]); } Bubble Sort
Time Complexity of Algorithms 05 The time complexity of Linear Search in the best case is O(1). The time complexity of Linear Search in the worst case is O(n). int search(int arr[], int n, int x) { int i; for (i = 0; i < n; i++) if (arr[i] == x) return i; return -1; } Linear Search
Time Complexity of Algorithms 06 The time complexity of Binary Search in the best case is O(1). In the worst case, the time complexity is O(log n). int binarySearch(int arr[], int l, int r, int x) { if (r >= l) { int mid = l + (r - l) / 2; if (arr[mid] == x) return mid; if (arr[mid] > x) return binarySearch(arr, l, mid - 1, x); return binarySearch(arr, mid + 1, r, x); }} Binary Search
Conclusion The time of execution increases with the type of operations we make using the inputs.
Conclusion The lesser the time complexity, the faster the execution.
Conclusion In case, if a code is of 1000s of lines, then it takes a toll on processor of the PC. So, it is important to check and reduce the time complexity as much as we can.