How to design better algorithms? This is one of the basic questions that haunt every programmer. Even software designers want better algorithms. But how do we say that one algorithm is better than the other? Is it not sufficient that the work is done, and the problem is solved? Not always. Suppose I can solve the problem in 5 years and somebody else comes with a solution in 5 minutes, whom will you prefer? It is not a question of fast computers, but fast algorithms.
The fastness of an algorithm is measured in terms of its complexity. An algorithm with logarithmic time complexity is considered better than an algorithm with polynomial time complexity and so on. We will not dwell in the forests of complexity here, may be in another article.
Here the point is, for designing better algorithms, powerful data structures are needed. They organize the data in optimal ways and allow access to this data in easy ways.
Most commonly used data structures are arrays, stacks, queues, trees and linked lists.
They appear in various other applications as basic components.
The study of data structures is a very demanding and challenging field.
Another important thing you should remember is that data structures and algorithms can be thought of independent of languages. That is you can implement a data structure, or an algorithm in C, C++ or java, subject to the restrictions put forward by that language.
Let us see some of the data structures in a bit more detail.
An array is used to store items of the same type. You can access items randomly. But insertion or deletion from the middle is a problem. Also you have to know the size of the array in advance.
2. Linked lists
Linked lists offer a much flexible method of storing and retrieving data.
You are free to delete or add anywhere, also it you can dynamically add items without knowing the number of items you need in advance. The only disadvantage ii that random access is not possible.
Stacks are implemented using either arrays or linked lists. They allow only addition and removal of items in the LIFO order. Stacks have many applications including search algorithms, recursion and many others.
A Queue is a FIFO structure. Queues are also implemented using arrays or linked lists. Queues allow deletion from one end and addition from other.
There are various types of queues like circular queues. Double ended queues, input restricted queues etc.
Trees are used to organize data in a hierarchical manner to facilitate easy deletion and addition.
In short, Data structure is the glue between algorithms and data.