Merge sort is one of the most efficient sorting algorithms. It works on the principle of Divide and Conquer. Merge sort repeatedly breaks down a list into several sublists until each sublist consists of a single element and merging those sublists in a manner that results into a sorted list.
Algorithm complexity is commonly represented with the O(f) notation, also known as asymptotic notation or “Big O notation”, where f is the function of the size of the input data. The asymptotic computational complexity O(f) measures the order of the consumed resources (CPU time, memory, etc.)
Space Complexity comparison of Sorting Algorithms
| Algorithm | Data Structure | Worst Case Auxiliary Space Complexity |
|---|
| Quicksort | Array | O(n) |
| Mergesort | Array | O(n) |
| Heapsort | Array | O(1) |
| Bubble Sort | Array | O(1) |
Algorithm is a step by step procedure, which defines a set of instructions to be executed in certain order to get the desired output. An algorithm are generally analyzed on two factors − time and space. Finiteness − Algorithms must terminate after a finite number of steps.
Discussion Forum
| Que. | The time factor when determining the efficiency of algorithm is measured by? |
|---|
| b. | Counting the number of key operations |
| c. | Counting the number of statements |
| d. | Counting the kilobytes of algorithm |
| Answer:Counting the number of key operations |
An Algorithm Development Process
- Step 1: Obtain a description of the problem. This step is much more difficult than it appears.
- Step 2: Analyze the problem.
- Step 3: Develop a high-level algorithm.
- Step 4: Refine the algorithm by adding more detail.
- Step 5: Review the algorithm.
How to write code efficiently
- Creating function.
- Eliminate unessential operations.
- Avoid declaring unnecessary variables.
- Use appropriate algorithms.
- Learn the concept of dynamic programming.
- Minimize the use of If-Else.
- Break the loops when necessary.
- Avoid declaring variables in the global scope.
Order of growth of an algorithm is a way of saying/predicting how execution time of a program and the space/memory occupied by it changes with the input size. The most famous way is the Big-Oh notation. It gives the worst case possibility for an algorithm.
An algorithm must have five properties:
- Input specified.
- Output specified.
- Definiteness.
- Effectiveness.
- Finiteness.
I have to swap numbers in an array 'd' times, so that left rotation of the array can be done. 'd' is the number of rotations of the array. Suppose if the array is 1->2->3->4->5 and if the d=1 then after one left rotation the array will be 2->3->4->5->1.
Computer scientists have defined three constructs for a structured program or algorithm. The idea is that a program must be made of a combination of only these three constructs: sequence, decision (selection) and repetition (Figure 8.6). It has been proven there is no need for any other constructs.
In computer science, bogosort (also known as permutation sort, stupid sort, or slowsort) is a highly inefficient sorting algorithm based on the generate and test paradigm. The function successively generates permutations of its input until it finds one that is sorted.
Big-O notation is the language we use for talking about how long an algorithm takes to run (time complexity) or how much memory is used by an algorithm (space complexity). Big-O notation can express the best, worst, and average-case running time of an algorithm.
Analysis of algorithms is the determination of the amount of time and space resources required to execute it. Usually, the efficiency or running time of an algorithm is stated as a function relating the input length to the number of steps, known as time complexity, or volume of memory, known as space complexity.
Given that the source entropy is H and the average codeword length is L, we can characterise the quality of a code by either its efficiency (η = H/L as above) or by its redundancy, R = L – H. Clearly, we have η = H/(H+R).
The standard way of comparing different algorithms is by comparing their complexity using Big O notation. In practice you would of course also benchmark the algorithms. As an example the sorting algorithms bubble sort and heap sort has complexity O(n2) and O(n log n) respective.
An important part of solving algorithms is efficiency. As your dataset grows so will the time it takes to run functions on it. Creating efficient algorithms is about reducing the amount of iterations needed to complete your task in relation to the size of the dataset. BigO notation is used to describe time complexity.