DEV Community

Mukilan Palanichamy
Mukilan Palanichamy

Posted on • Edited on

My journey in competitive programming

I did sorting of array concept in cpp. It consist of tow sub-concepts.

They are :

Selection sort : Selection sort algorithm to sort an array of integers in ascending order. Where we will use 2 for loops and take a third element to assign index then swamping the elements.

Real Life Simple Explanation

Imagine you have a row of books and you wish to sort them in ascending order based on their heights. Here is how you would do it using Selection Sort:

Pick the first book in the line and assume that is smallest.

  1. Look at all the books to the right and find the shortest book.
  2. Interchange the smallest book with the first book so that the smallest book is now in the correct position.
  3. Take the second book and repeat the process: find the smallest book from the rest and interchange it into the second position.
  4. Continue this until all books are sorted.

In simple words, one is picking the smallest in the unsorted part and putting it in its appropriate place in the sorted part, step by step.

Image description

Insertion sort : Insertion sort algorithm to sort an array of integers in ascending order. It contain of for and also a while loop . Where we will insert the element if the first element is greater than the next element.

Real-Life Example of Insertion Sort

Application:

You have cards. You want to sort the cards in your hand in ascending order based on their numbers.

Step-by-Step Explanation:

  1. Begin with the first card in your hand. It is already "sorted" because there is nothing else to compare it with.
  2. Choose the next card. Compare it with the first one. If it is smaller, place it in front of the first; otherwise, leave it as it is. So now, the first two cards are arranged.
  3. Choose the third card. Compare it with the arranged cards-the first two. Put it in its proper place so that all three cards are arranged.
  4. Take each card from the deck next; compare it with cards, if already sorted in your hand, and put it on a specific, right position.
  5. Keep following this process till all of the cards get sorted which are in your hand.

Image description

Top comments (0)