Data Structures in Dart - #1 -Complexity
Tags: dart, datastructures, programming, coding
Published: 2023-09-29 01:47:02


In this series of blogs, we will talk about the data structure in Dart.
So let's know what a data structure is.


**Data structures**

>  A data structure is a way to store and organize data in order to facilitate access and modification. Using the appropriate data structure or structures is an important part of algorithm design. No single data structure works well for all purposes, and so you should know the strengths.
and the limitations of several of them.

Firstly, before we dive into data structure, we need to know what _complexity _is.


> Complexity refers to the amount of time, space, and other resources required to solve a problem or execute an algorithm.

**Complexity** is characterizing the efficiency of algorithms. Efficiency can be measured by

1. _Space Complexity_ -> Related to data structures and how information is stored

2. _Time Complexity_ -> Related to how fast the algorithm can process data and provide results


**Time Complexities Cases**: 


![Time Complexities Cases](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/1pm2fj7ac0o1j1qj1rrx.png)


- **Constant Time**

 - It takes the same time regardless of the number of inputs.
For example:


![Constant Time Example 1](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/oq2dwaf9rr4ghh75ez03.png)


or


![Constant Time Example 2](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/uwtlfh1jjm1c0apqw0ab.png)



_Note : As input data increases, the amount of time the algorithm takes does not change. The Big O notation for constant time is O(1).


![Constant Time](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/v56juk0ndxdy5nsos1xs.png)




_Note_: 
The Big O Notation is a metric for determining an algorithm's efficiency. Put simply, it gives an estimate of how long it takes your code to run on different sets of inputs. You can also see it as a way to measure how effectively your code scales as your input size increases.

![Big O analysis](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/v4ekd3m4whhyh4thx1ox.png)


- **Linear Time**



![Linear Time Example](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/ix8rkmhpz6h139nx9hs3.png)







![Linear Time](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/44u5k3neezmj8sx6zuun.png)



As you can see, as the amount of data increases, the running time increases by the same value.

The Big O notation for this type of complexity is o(n).



- **Quadratic Time**

This type of algorithm takes time proportional to the square of the size of the inputs.


![Quadratic Time Example](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/yo7dhpf7enqdyjlhlwe7.png)

As you can see in the graph below, as the input size increases, the time it takes to run this code increases drastically.


![Quadratic Time](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/5c3xdoii6dj9b2vkqaer.png)



The Big O notation for this type is O(n)^2.










##Finally

  At this point, you now know the concept of complexity and how to calculate the algorithm's efficiency.

  In this article, I just talk about three types of complexity. If you want to know about the rest of the complexity types, you can find many resources on the internet.


I'm excited to see you in the next article.

See you soon!