Algorithms and OOP are two disparate terms, which have only in common, that they are CS-terms. Simply - An algorithm is like a cooking recipe: to do x you need the following ingredients and do step 1,2,3,4,5,6... then you have your meal prepared.
That said, it seems a natural fit for algortihms to be described in a procedural way. Procedural means nothing other than: first do x and then do y.
A common problem is: »How to sort a set of x?«.
An easy to understand solution is bubble-sort:
- Iterate over the set from the last element as long as you have not reached the first element and while iterating
- start a 2nd iteration from the beginning to the current element of the first iteration and
- compare the current element of (2) with its successor 3a) If greater, swap positions
That is the algorithmic / verbal description of the bubblesort-algorithm.
Here comes a procedural / pseudocode implementation
bubbleSort(Array A)
for (n=A.size; n>1; n=n-1){
for (i=0; i<n-1; i=i+1){
if (A[i] > A[i+1]){
A.swap(i, i+1)
}
}
}
That was easy.
How does that releate to OOP? You could use this algorithm to treat collections (an object itself) of objects:
Example in Javascript (though no clean OO-Lingo, but with near to no boilerplate and easy to understand)
objects =[{"name":"Peter"}, {"name":"Paul"}, {"name":"Mary"}]
compareObjects=function(x,y){ return x.name>y.name };
sorted = objects.sort(compareObjects)
console.log(sorted)
We have a) a collection objects, b) a method common to this collection sort which contains / abstracts away the sorting algorithm and c) our objects Peter, Paul and Mary. The Specification for the sorting is found here.
What is the relation between algorithms and OOP? Are they two independent topics?
From what was said, it should be clear, the answer should be: yes, they are independend.
How OOP can help algorithms? Or in which direction it can affect it?
OOP is just one programming style. It can not help in any kind. Otherwise an algorithm could be implemented in an OO language to do something to objects (as shown)
Are there some problems which only can be presented and solved by OOP?
I can not think of one (but that doesn't mean, that it is impossible). But if you look it the other way around: OOP is useful, if you want to model some problems and solve ith with an appropriate algorithm. Say you have a record of friends you could model them as objects with properties and if you want a list of friends sorted in any way, you could use the example-code given above to do exactly that.
Why algorithms textbook are more procedure-oriented?
As said: it is more natural, since procedural is the character of algorithms.