Sorting an array without sort() command

98 次查看(过去 30 天)
Job Gonzales
Job Gonzales 2020-10-23
编辑: Job Gonzales 2024-4-10,15:26
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.

回答(2 个)

KSSV
KSSV 2020-10-23
  3 个评论
Dylan
Dylan 2024-4-10,1:08
Bro it's not that deep. No need to be so passive aggressive lol
Job Gonzales
Job Gonzales 2024-4-10,15:25
编辑:Job Gonzales 2024-4-10,15:26
@Dylan Ah my bad on sounding passive agressive. I was new to MATLAB so I didn't know any of the syntax or technical language. Normally when I'm learning something new I do tend to look through YT vids and forums but when I looked up this question the answers people had given were to complex or had added certain functions that I hadn't learned yet.

请先登录,再进行评论。


Ameer Hamza
Ameer Hamza 2020-10-23
编辑:Ameer Hamza 2020-10-23
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

类别

Help CenterFile Exchange 中查找有关 Get Started with MATLAB 的更多信息

Community Treasure Hunt

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

Start Hunting!

Translated by