Selection, Insertion, Bubble and Heap Sort in C++:- Hello Everyone this is Shivam form codinglio. Well, today we are talking about Sorting, It is an operation, in which all the elements of a list are arranged in a predeterminate order. The element can be arranged in a sequential form, smallest to largest such that every element is less than or equal to its next neighborhood in the list, such that arrangement is called ascending order. The elements are arranged in a sequence from largest to smallest such that every element is than or equal to its neighborhood in the list such that arrangement that is called descending.
- 1 -:Types of Sorting:-
- 2 1. Selection Sort:-
- 3 2. Insertion Sort:
- 4 3. Merge Sorting:
- 5 (4) Heap Sorting:
- 6 Conclusion:
-:Types of Sorting:-
1. Selection Sort:-
Selection Sort in C++ is a simple and natural way of sorted a list in defining the smallest element in the list and exchange it with the elements present at the head of the list.
After an exchange of smallest element on the head of the list, the list is divided into two parts, one part is sorted and other is unsorted. The process of selection and exchange continues in this function until all elements in the list are sorted. The selection sort basically is two steps important, First one is a ‘selection’ and another one ‘exchanges’.
The algorithm of Selection Sort:
Step 2:- Print the sorted list.
2. Insertion Sort:
Insertion Sort in C++ is the process is similar to arranging a pair of playing cards. In the pack of cards, the first two cards are put in a correct related location or order. The 3rd card is inserted at correct location or order place related to the 1st and 2nd card. The 4th card is inserted in the correct location or an order related to the first three card and so on.
Given a list of a number it is divided into two parts.
(A) Sorted Part.
(B) Unsorted Part.
Now you pick up one element from the font of a unsorted list and insert its proper location in the sorted list. This is insertion action repeated fill the unsorted part until the end.
Pass 1:- 15,25, 30,9,99,20,26 sorted unsorted
pass 2:- 15,25,30, 9,99,20,26 sorted unsorted
pass 3:- 9,15,25,30, 99,20,26 sorted unsorted
pass 4:- 9,15,25,30,99, 20,26 sorted unsorted
pass 5:- 9,15,20,25,30,99, 26 sorted unsorted
pass 6:- 9,15,20,25,26,30,99 sorted
The algorithm of Insertion Sort:
Step 2:- Print Sorted List.
3. Merge Sorting:
This method use following two concepts –
a) if a list is empty or it contains only one element is also called “Singleton”.
b) It uses the old technique of dividing and recursively divide the list into two sublists until its left with eighter empty or “Singleton” list.
This algorithm divides a given list into two sublists. Each sublist recursively divides into further two sublists and so on until the list is empty or singleton.
The algorithm of Merge Sort:
<strong>(1) </strong>Ptr1 = lb
<strong>(2)</strong> ptr2 = ub
<strong>(3) </strong>ptr3 = lb
<strong>(4) </strong>while ((ptr1<mid)&&ptr2<=ub)
merge list[ptr3] = list[ptr1];
merge list[ptr3] = list[ptr2];
/* copy remaning first list */
<strong>(5) </strong>while (ptr<mid)
merge list[ptr3] = list[ptr1];
/* copy remaining second list */
<strong>(6) </strong>while (ptr = ub)
<strong>(7) </strong>for (i=lb; i<ptr3; lb++)
list[ptr1]=merge list [ptr2];
<strong>Step 2:- </strong>Stop.
(4) Heap Sorting:
Heap sorting is a special type of sorting technology which involves the insertion and deletion of elements in a heap tree. We know that the element of heap tree is arranged in a linear array, the heap tree can be allowed to sort the list of the element using heap tree processing. sorting an array by heap by sort technology.
1. Create a heap tree from the element of given unsorted array.
2. Deletion of root (largest in the maximum heap) element respectively (to sort in ascending order) and put from last.
Don’t forget to give your feedback, if you find this helpful. Please Hit the share button to help us grow. If we are missing something, please let us know in the comment box. Keep visiting Cheers.!!