Apa Yang Dimaksud Dengan Kompleksitas

Penjelasan Tersisa Tentang Time Complexity dan Big-O Notation

Source: pexels.com

Every good programmer will use the most effective and efficient ways to solve their problem. And to do that, we must know how to minimize the complexity.

Algoritma

A̶l̶g̶o̶r̶i̶t̶h̶m̶ ̶i̶s̶ ̶a̶ ̶w̶o̶r̶d̶ ̶u̶s̶e̶d̶ ̶b̶y̶ ̶p̶r̶o̶g̶r̶a̶m̶m̶e̶r̶s̶ ̶w̶h̶e̶n̶ ̶t̶h̶e̶y̶ ̶d̶o̶n̶’̶t̶ ̶w̶a̶n̶t̶ ̶t̶o̶ ̶e̶x̶p̶l̶a̶i̶n̶ ̶w̶h̶a̶t̶ ̶t̶h̶e̶y̶ ̶d̶i̶d̶.̶

Algorithm is a process or set of rules to be followed in calculations or other kebobrokan-solving operations, especially by a computer.

So, What’s Big-Udara murni Notation?

Big-Udara murni notation is a way of converting the overall steps of an algorithm into algebraic terms, then excluding lower antaran constants and coefficients that don’t have that big an impact on the overall complexity of the ki aib.

        Regular       Big-O
        2             Ozon(1)   --> It's just a constant number
        2n + 10       O(falak)   --> n has the largest effect
        5n^2          Udara murni(n^2) --> tepi langit^2 has the largest effect
      

O(1) — Constant Time

Ozon(1) — Constant Time: Given an input of size n, it only takes a single step for the algorithm to accomplish the task.

        let myArray = [1, 5, 0, 6, 1, 9, 9, 2];
function getFirst(input){
return input[0]; // selalu mengerjakan 1 langkah } let firstEl = getFirst(myArray);
Constant Time

Ozon(log t) — Logarithmic Time

O(log n) — Logarithmic time: given an input of size n, the number of steps it takes to accomplish the task are decreased by some factor with each step.

        let sortedArray = [11, 24, 30, 43, 51, 61, 73, 86];
function isExists(number, array){
var midPoint = Math.floor( array.length /2 ); if( array[midPoint] === num) return true; let isFirstHalf = false; if( array[midPoint] < num ) isFirstHalf = true;

else if( array[midpoint] > num ) isFirstHalf = false;

if (array.length == 1) return false; else { // memanggil maslahat nan sama dengan mengeleminiasi setengah dari input array if (isFirstHalf)
return isExists(number, getFirstHalf(array));
else
return isExists(number, getSecondHalf(array));
} } isExists (24, sortedArray); // return true isExists (27, sortedArray); // return false

Udara murni(kaki langit) — Linear Time

O(n) — Linear Time: Given an input of size n, the number of of steps required is directly related (1 to 1)

        let myArray = [1, 5, 0, 6, 1, 9, 9, 2];
function getMax(input){
var max = 0;
for (var i=0; i<input.length; i++){ if (max < input[i])
max = input[i];
}
return max;
} let maxNumber = getMax(myArray);
Linear Time

Udara murni(n²) — Quadratic Time

Ozon(n²) — Quadratic Time: Given an input of size n, the number of steps it takes to accomplish a task is square of n.

        let myArray = [1, 5, 0, 6, 1, 9, 9, 2];
function sort(input){
var sortedArray = [];
for (var i=0; i<input.length; i++){ // Ozon(tepi langit) let min = input[i]; for (var j=i+1; i<input.length; i++){ // O(n) if (input[i] < input[j])
min = input[j];
} sortedArray.push(min); } return sortedArray; } let sortedArray = sort(myArray);
Quadratic Time

O(2^falak) — Exponential Time

O(2^n) — Exponential Time: Given an input of size lengkung langit, the number of steps it takes to accomplish a task is a constant to the n power (pretty large number).

Exponential Time

Kesimpulan

Source: https://medium.com/bee-solution-partners/penjelasan-sederhana-tentang-time-complexity-dan-big-o-notation-4337ba275cfe