Difference between sorting methods Selection Dort, Insertion Sort and Bubble Sort

Asked

Viewed 4,379 times

4

I’m studying the discipline data structure and I’m developing C programs++.

I know basically these three types of ordination (Selection Sort, Insertion Sort and Bubble Sort), but I don’t know in detail the difference between the three types.

What are these differences and where to use each sort?

If it is also possible, I would like an example of each sort of ordering.

1 answer

5

  • Selection Sort: At each iteration, it searches the entire unordered portion of the vector for the smallest (or largest) element and places it in the correct position. Thus, in the ith iteration, the ith-smallest element goes to the position i, and so on.

  • Insertion Sort: Iterates increasingly between positions. For each position, take its element and regress it into the vector until it fits into the correct position (when you find the first element smaller than it).

  • Bubble Sort: It takes two adjacent elements and changes them from position if, and only if, they are out of order. It does this with all pairs of elements at each iteration. At the end of the same, the smallest (or largest) element not yet ordered will be in the correct position. Repeat this until the end.

You can see them working visually here.

All of these are elementary algorithms and easy to implement. Both the Bubble as to the Selection are complex O(n²) in all cases. The Insertion, on the other hand, turns into O(n) in the best case (for example, if the vector is already in ascending order). Thus, it turns out to be a better algorithm for general purposes.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.