Sorting an array without sort() command

조회 수: 10 (최근 30일)
Job Gonzales
Job Gonzales 2020년 10월 23일
댓글: Steven Lord 2024년 8월 27일
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
Soumya
Soumya 2024년 8월 27일
I need to sort an array without the Sort() command and the array is user input.
Like [12, 2 , 7, 9, 10 ] need to be [2, 7, 9, 10, 12].
I need to compare the number with all the other nnumbers in the array and then If selected number is smaller than any of the next numbers, swap their location but I want to use Stateflow for this.
Steven Lord
Steven Lord 2024년 8월 27일
I need to sort an array without the Sort() command and the array is user input.
Okay.
Like [12, 2 , 7, 9, 10 ] need to be [2, 7, 9, 10, 12].
I need to compare the number with all the other nnumbers in the array and then If selected number is smaller than any of the next numbers, swap their location but I want to use Stateflow for this.
Why are you using Stateflow for this? Is this part of a homework assignment or something? Or are you trying to generate code for a hardware target or something similar? If the latter, consider the Sort block from DSP System Toolbox.
If this is homework, show us what you've done so far to try to solve the problem and ask a specific question about where you're having difficulty and we may be able to provide some guidance.

댓글을 달려면 로그인하십시오.

답변 (2개)

KSSV
KSSV 2020년 10월 23일
  댓글 수: 3
Dylan
Dylan 2024년 4월 10일
Bro it's not that deep. No need to be so passive aggressive lol
Job Gonzales
Job Gonzales 2024년 4월 10일
편집: Job Gonzales 2024년 4월 10일
@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
  댓글 수: 2
Job Gonzales
Job Gonzales 2020년 10월 23일
Oh wow thank you.
Ameer Hamza
Ameer Hamza 2020년 10월 23일
I am glad to be of help!

댓글을 달려면 로그인하십시오.

카테고리

Help CenterFile Exchange에서 MATLAB에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by