Sorting an array without sort() command

148 views (last 30 days)
Job Gonzales
Job Gonzales on 23 Oct 2020
Commented: Ameer Hamza on 23 Oct 2020
I need to sort an array without the sort() command and the array is user inputed.
Like given an array [1, 3, 2] needs to be [1, 2, 3] or [1, 3, 2; 4, 5, 6; 9, 8, 7] needs to be [1, 2, 3; 4, 5, 6; 7, 8, 9].
I need to compare the number with all the other nnumbers in the array and then If selected number is greater than any of the next numbers, swap their location.

Answers (2)

KSSV
KSSV on 23 Oct 2020
  1 Comment
Job Gonzales
Job Gonzales on 23 Oct 2020
Thank you, I did search but I guess I worded it differently because none of these showed for me and I viewed atleast 10 different questions.

Sign in to comment.


Ameer Hamza
Ameer Hamza on 23 Oct 2020
Edited: Ameer Hamza on 23 Oct 2020
What you described is called selection sort. This FEX package have code for all commonly know sorting algorithms, including the selection sort: https://www.mathworks.com/matlabcentral/fileexchange/45125-sorting-methods

Categories

Find more on Shifting and Sorting Matrices in Help Center and File Exchange

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!