Sorting | Sololearn: Learn to code for FREE!

0

Sorting

May I know how to make a function to sort the elements in ascending or descending order using selection sort and insertion sort

3/15/2021 2:28:11 PM

Chelsea Feleca

4 Answers

New Answer

+1

Jayakrishna🇮🇳 Can you tell be how Insertion sort is different from Selection sort?

0

Hope these helps... https://www.sololearn.com/learn/658/?ref=app https://www.sololearn.com/learn/652/?ref=app

0

C++ already has sort methods and templates. It's usually only necessary to provide a function that performs the comparison operation. Here is an example of sorting a list of objects using the list::sort() method. https://code.sololearn.com/cA13a0a11a12/#cpp See: https://www.cplusplus.com/reference/algorithm/sort/ https://www.cplusplus.com/reference/list/list/sort/ Normally we sort in ascending order and simply iterate over the container in reverse to get the descending order or use a reverse() method.

0

Atul Once go through the link, you can understand it.. Selection Sort is first finds the smallest element in the array and swaps it with the element in the first 0th position, then finds the second and swaps it with second (1st) position, and continues for entire array.. Where as insertion sort, first arrange 1st 2 elements in order. Then next 3rd element is added in ascending order with 1,2. Now 1,2,3 are in ordered. Next 4th element is placed in order with 1,2,3,4 to its needed position.. so 1,2,3,4 is ordered now. This goes on for entire list. hope it helps..