Why data structures matter
The fact is that programs are all about processing data. Data structures are referred to how data is organized which affects the time of executing a program.How to measure the performance of a data structure
In order to measure "how fast"/efficiency/performance of a data structure, we measure the performance of its operations. There are four basic operations including reading, searching, insertion, and deletion. A pure time consuming is not used for the measuring because it is not reliable depending on the hardware that it is run on. But instead, we use the term time complexity which refers to how many steps an operation takes.An example of how a single rule can affect efficiency
Let's compare two data structures: Array and Set (with N elements).1. Array
- Reading: 1 step (because the computer has the ability to jump to any particular index in the array)- Searching: N steps (the worst case with linear search)
- Insertion: N + 1 steps (the worst case inserting at index 0: N steps of right-shifts + 1 step of insertion)
- Deletion: N steps (the worst case deleting at index 0: N-1 steps of left-shifts + 1 step of deletion)
2. Set
- Reading: 1 step- Searching: N steps
- Insertion: best case: N + 1 steps; worst case: 2N + 1 steps (every insert first requires a search)
- Deletion: N steps
NOTE: I mentioned the set by setting up from an array. There is another way to set up a set by a hash table.
Reference:
[1]. Jay Wengrow | A Common-Sense Guide to Data Structures and Algorithms: Level Up Your Core Programming Skills