Thread Subject: sorting algorithm

Subject: sorting algorithm

From: Evren

Date: 3 Dec, 2008 15:48:02

Message: 1 of 6

Hi I am in a matlab course and the teacher wants us to create a function in MATLAB 7.5.0(R2007b) that does the following
A. Using the input command, you enter a set of number to be sorted as a row vector. the length of the vector is arbitrary.
B. You program should then sort the numbers from the most negative to the most positive.
C. You program should then print out the unsorted list and the sorted list.
D. You can't use the built in matlab sort function to complete this assignmen. You must develop your own sorting algorithm.

I tried for hours and just can't seem to get it and its do tomorrow. If anyone can help, thank you very much.

Here is what I got so far with some help, but it still doesn't work.

for i=1:(n-1) % n is the length of the vector V
            if V(i)>V(i+1)
                    s = V(i); V(i) = V(i+1); V(i+1) = s;
            end
end

Subject: sorting algorithm

From: Steve Amphlett

Date: 3 Dec, 2008 15:58:01

Message: 2 of 6

"Evren " <evrenbrs@gmail.com> wrote in message <gh69ni$6gv$1@fred.mathworks.com>...
> Hi I am in a matlab course and the teacher wants us to create a function in MATLAB 7.5.0(R2007b) that does the following
> A. Using the input command, you enter a set of number to be sorted as a row vector. the length of the vector is arbitrary.
> B. You program should then sort the numbers from the most negative to the most positive.
> C. You program should then print out the unsorted list and the sorted list.
> D. You can't use the built in matlab sort function to complete this assignmen. You must develop your own sorting algorithm.
>
> I tried for hours and just can't seem to get it and its do tomorrow. If anyone can help, thank you very much.
>
> Here is what I got so far with some help, but it still doesn't work.
>
> for i=1:(n-1) % n is the length of the vector V
> if V(i)>V(i+1)
> s = V(i); V(i) = V(i+1); V(i+1) = s;
> end
> end

"develop your own sorting algorithm". Why??

Sure you could research classical sorting methods and then implement one in Matlab, but what's the point? Are you expected to dream up a wholly new scheme?

Subject: sorting algorithm

From: Evren

Date: 3 Dec, 2008 16:04:01

Message: 3 of 6

"Steve Amphlett" <Firstname.Lastname@Where-I-Work.com> wrote in message <gh6aa9$g95$1@fred.mathworks.com>...
> "Evren " <evrenbrs@gmail.com> wrote in message <gh69ni$6gv$1@fred.mathworks.com>...
> > Hi I am in a matlab course and the teacher wants us to create a function in MATLAB 7.5.0(R2007b) that does the following
> > A. Using the input command, you enter a set of number to be sorted as a row vector. the length of the vector is arbitrary.
> > B. You program should then sort the numbers from the most negative to the most positive.
> > C. You program should then print out the unsorted list and the sorted list.
> > D. You can't use the built in matlab sort function to complete this assignmen. You must develop your own sorting algorithm.
> >
> > I tried for hours and just can't seem to get it and its do tomorrow. If anyone can help, thank you very much.
> >
> > Here is what I got so far with some help, but it still doesn't work.
> >
> > for i=1:(n-1) % n is the length of the vector V
> > if V(i)>V(i+1)
> > s = V(i); V(i) = V(i+1); V(i+1) = s;
> > end
> > end
>
> "develop your own sorting algorithm". Why??
>
> Sure you could research classical sorting methods and then implement one in Matlab, but what's the point? Are you expected to dream up a wholly new scheme?

thats what i am expected to do.i must develop my sorting code.can you help me with this.what is the wrong with my code?

Subject: sorting algorithm

From: John D'Errico

Date: 3 Dec, 2008 16:10:19

Message: 4 of 6

"Evren " <evrenbrs@gmail.com> wrote in message <gh6alh$lvv$1@fred.mathworks.com>...

> > > I tried for hours and just can't seem to get it and its do tomorrow. If anyone can help, thank you very much.
> > >
> > > Here is what I got so far with some help, but it still doesn't work.
> > >
> > > for i=1:(n-1) % n is the length of the vector V
> > > if V(i)>V(i+1)
> > > s = V(i); V(i) = V(i+1); V(i+1) = s;
> > > end
> > > end
> >
> > "develop your own sorting algorithm". Why??
> >
> > Sure you could research classical sorting methods and then implement one in Matlab, but what's the point? Are you expected to dream up a wholly new scheme?
>
> thats what i am expected to do.i must develop my sorting code.can you help me with this.what is the wrong with my code?

While this will work for two numbers, what happens when
you have 3 numbers? I.e., what if you use your code to
sort the numbers [3 2 1]? Try it. Are they in order? Why
did it fail? Look (carefully) at what it did.

John

Subject: sorting algorithm

From: Evren

Date: 3 Dec, 2008 16:18:02

Message: 5 of 6

"John D'Errico" <woodchips@rochester.rr.com> wrote in message <gh6b1b$s8t$1@fred.mathworks.com>...
> "Evren " <evrenbrs@gmail.com> wrote in message <gh6alh$lvv$1@fred.mathworks.com>...
>
> > > > I tried for hours and just can't seem to get it and its do tomorrow. If anyone can help, thank you very much.
> > > >
> > > > Here is what I got so far with some help, but it still doesn't work.
> > > >
> > > > for i=1:(n-1) % n is the length of the vector V
> > > > if V(i)>V(i+1)
> > > > s = V(i); V(i) = V(i+1); V(i+1) = s;
> > > > end
> > > > end
> > >
> > > "develop your own sorting algorithm". Why??
> > >
> > > Sure you could research classical sorting methods and then implement one in Matlab, but what's the point? Are you expected to dream up a wholly new scheme?
> >
> > thats what i am expected to do.i must develop my sorting code.can you help me with this.what is the wrong with my code?
>
> While this will work for two numbers, what happens when
> you have 3 numbers? I.e., what if you use your code to
> sort the numbers [3 2 1]? Try it. Are they in order? Why
> did it fail? Look (carefully) at what it did.
>
> John

i am trying to understand why it doesnt work but i cant figuger it out.what is the wrong?what can i do to work the code for more than 5 numbers. i cant solve what to do..i even couldnt work the code yet...

Subject: sorting algorithm

From: John D'Errico

Date: 3 Dec, 2008 16:35:04

Message: 6 of 6

"Evren " <evrenbrs@gmail.com> wrote in message <gh6bfq$6b6$1@fred.mathworks.com>...
> "John D'Errico" <woodchips@rochester.rr.com> wrote in message <gh6b1b$s8t$1@fred.mathworks.com>...
> > "Evren " <evrenbrs@gmail.com> wrote in message <gh6alh$lvv$1@fred.mathworks.com>...
> >
> > > > > I tried for hours and just can't seem to get it and its do tomorrow. If anyone can help, thank you very much.
> > > > >
> > > > > Here is what I got so far with some help, but it still doesn't work.
> > > > >
> > > > > for i=1:(n-1) % n is the length of the vector V
> > > > > if V(i)>V(i+1)
> > > > > s = V(i); V(i) = V(i+1); V(i+1) = s;
> > > > > end
> > > > > end
> > > >
> > > > "develop your own sorting algorithm". Why??
> > > >
> > > > Sure you could research classical sorting methods and then implement one in Matlab, but what's the point? Are you expected to dream up a wholly new scheme?
> > >
> > > thats what i am expected to do.i must develop my sorting code.can you help me with this.what is the wrong with my code?
> >
> > While this will work for two numbers, what happens when
> > you have 3 numbers? I.e., what if you use your code to
> > sort the numbers [3 2 1]? Try it. Are they in order? Why
> > did it fail? Look (carefully) at what it did.
> >
> > John
>
> i am trying to understand why it doesnt work but i cant figuger it out.what is the wrong?what can i do to work the code for more than 5 numbers. i cant solve what to do..i even couldnt work the code yet...

Ok. Think about what would happen if you applied your
sorting scheme to the numbers a SECOND time. Would
a second pass through your scheme guarantee that three
numbers will always be sorted properly?

Why does a single loop such as your fails to give a sorted
vector? Apply it to the numbers [3 2 1]. The first test in
your loop will convert this set into [2 3 1]. Then the second
test yields [2 1 3]. These are clearly not sorted, yet you are
closer to that goal.

Now make a second pass through. What happens? Think
about it. Then try again with the numbers [4 3 2 1]. Can
you sort them using your scheme? Will a second pass
succeed?

John

Tags for this Thread

Everyone's Tags:

Add a New Tag:

Separated by commas
Ex.: root locus, bode

What are tags?

A tag is like a keyword or category label associated with each thread. Tags make it easier for you to find threads of interest.

Anyone can tag a thread. Tags are public and visible to everyone.

Tag Activity for This Thread
Tag Applied By Date/Time
sorting algorithm Evren 3 Dec, 2008 10:50:05
rssFeed for this Thread

Contact us at files@mathworks.com