Alternative to the sort function

Looking for an alternative way to sort values in either decending or acsending order without using the sort function

2 Comments

Why? Your reason for not using sort() will influence the recommendations.
I am in a beginner programming class (only a couple weeks in) one of the homework tasks asks to create a program that sorts user inputted numbers in ascending order without the sort function. I am struggling with this.

Sign in to comment.

 Accepted Answer

You would have to adapt it slightly to give a choice of ascending or descending, and you would have to adapt for non-vectors.

7 Comments

I appreciate the answer and the time it took for you to write it, and have no doubt it is correct. However I am way too new to this for that to make any sense to me, I do truly appreciate the effort though, thank you.
Wikipedia sort methods. For example Bubble Sort
Bubble Sort is one of the worst sorting algorithms (apart from those that were designed to be bad). However, it is easy to understand and to implement. It is often the first real algorithm people are taught to write in many languages.
I would second Walter's advice to look at the Wikipedia page for Bubble Sort and implement it. Then never actually use it. There are many sorting algorithm that are better, and Mathworks engineers have already implemented them.
I've got the sorting part of the code working, (thankyou for your help), I am now stuggling with putting the using input into a vector without a predetermined length. The code i've got will only sort the last number entered into order. What I have is below
**Command Window**
Input a number: 5
Input a number: 4
Input a number: 5
Input a number:
Input a number: 6
Input a number: 6
Input a number: 54
Input a number: 0
Converting input into ascending order
The input numbers to be arranged:
0
The ascending matrix is
0
>>
**Editor**
% clear comand window
clc;
clear;
% Prompt input of numbers to be arranged from user until input = <= 0 is entered
x = [];
while 1
x = input('Input a number: ');
if x <= 0; break; end
end
disp('Converting input into ascending order ')
% The vector of numbers
disp('The input numbers to be arranged:')
A=[x];
disp(A)
% Number of entries, n
n=length(A);
% making (n-1) passes
for j=1:1:n-1
% comparing each number with the next and swapping
for i=1:1:n-1
if A(i)>A(i+1);
% temp is a variable where the numbers are kept
% temporarily for the switch
temp=A(i);
A(i)=A(i+1);
A(i+1)=temp;
end
end
end
disp(' ')
disp ('The ascending matrix is')
disp(A)
Hint:
count = 0;
values = [] ;
while true
do something to get a value
if value indicates termination
break;
end
increase count
values(count) = value;
end
Mate you are a legend, thank you heaps. This is what I ended up with, not sure if it was quite what you were getting at but definitely based off your suggestions.
clc;
clear;
count = 0;
values = [] ;
while true
value = input('Input numbers to be arranged') ;
if value <= 0
break;
end
count= count +1
values(count) = value;
end
disp('Converting input into ascending order ')
% The vector of numbers
disp('The input numbers to be arranged:')
A=[values];
disp(A)
% Number of entries, n
n=length(A);
% making (n-1) passes
for j=1:1:n-1
% comparing each number with the next and swapping
for i=1:1:n-1
if A(i)>A(i+1);
% temp is a variable where the numbers are kept
% temporarily for the switch
temp=A(i);
A(i)=A(i+1);
A(i+1)=temp;
end
end
end
disp(' ')
disp ('The ascending matrix is')
disp(A)
One suggestion that will eliminate the need for your count variable: the end function when used as an index refers to the position of the last element / row / column / etc. in an array. You can use this in assignment or referencing statements, including using it in some simple mathematical expressions. Let's start off with an empty array x.
x = []
x = []
We can assign to an element one past the end to make the vector longer:
x(end+1) = 1
x = 1
We can do that again.
x(end+1) = 3
x = 1×2
1 3
Let's reset the last value.
x(end) = 42
x = 1×2
1 42
In this next statement, x has 2 elements so end+2 is 4. Therefore the next command sets x(4) to 10 with x(3) filling in with the default value of 0.
x(end+2) = 10
x = 1×4
1 42 0 10
We don't have to add to end, we could also subtract. x has 4 elements so end-3 is 1 and y is x(1).
y = x(end-3)
y = 1

Sign in to comment.

More Answers (0)

Categories

Community Treasure Hunt

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

Start Hunting!