Sorting numbers in an array without sort function

조회 수: 53 (최근 30일)
Takashi Fukushima
Takashi Fukushima 2019년 11월 27일
댓글: Takashi Fukushima 2019년 11월 28일
Hello,
I want to sort numbers in an arrey without using sort function, but with bubblesort (swapping number next each other until all numbers are sorted)
Here is what I have...
X=[10 1 29 89 5 56 76 8];
finished=0;
while finished==0
if finished==0
for i=1:size(X,2)-1
if X(i)>X(i+1)
c=X(i);
X(i)=X(i+1);
X(i+1)=c;
end
end
end
if X(i)<X(i+1) || X(i)==X(i+1)
finished=1;
end
end
disp(X)
I understand that this loop works by comparing numbers next each other (like comparing 10 with 1). Then, if the front number (10) is bigger than the next number (1), the numbers are swaped. However, I want to break the loop once all numbers are sorted from the minimum number to the maximum number.
The code above only sorts the numbers until [1,10,29,5,56,76,8,89], so I want to loop several more to complete the sorting, but I want the code stops when the sorting is done. Otherwise, the code keeps on looping again and again.
I really appreciate if I get some help with this problem.

채택된 답변

James Tursa
James Tursa 2019년 11월 27일
Simply set your "finished" flag depending on whether a swap was done or not. E.g.,
while true
finished = 1;
for i=1:size(X,2)-1
if X(i)>X(i+1)
c=X(i);
X(i)=X(i+1);
X(i+1)=c;
finished = 0;
end
end
if finished
break
end
end
The for-loop can also be shortened by realizing that after each pass you are guaranteed to have one more element sorted at the bottom. I.e., the for-loop doesn't have to examine the entire array each time ... at each iteration you only have to examine one fewer elements.
  댓글 수: 4
the cyclist
the cyclist 2019년 11월 27일
편집: the cyclist 2019년 11월 27일
In case you were asking something different ... the syntax
if finished
works because when the variable finished takes the value "1", that value will be converted to the boolean "true", and the if statement will be entered. In general, the condition always has to be evaluated to true/false.
I would argue that slightly better programming practice would have been to define the variable finished as a boolean false in the first place. But that's fairly precious. It's more important to get the code to work correctly first, before worrying about such nuances.
Takashi Fukushima
Takashi Fukushima 2019년 11월 28일
Thank you so much for your answer James and the cyclist !
I figured it out and think I understand it!

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

추가 답변 (1개)

the cyclist
the cyclist 2019년 11월 27일
편집: the cyclist 2019년 11월 27일
if all(diff(X)) >= 0
Also, rather than defining the finished variable, you could have just made that the condition for the while loop:
while not(all(diff(X)) >= 0)
or, mathematically equivalently but a bit more elegantly
while any(diff(X)) < 0
  댓글 수: 1
Takashi Fukushima
Takashi Fukushima 2019년 11월 27일
Thank you so much! I did not know diff function would be useful here!

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

카테고리

Help CenterFile Exchange에서 Loops and Conditional Statements에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by