## bubble sort function that sort element of a vector from tha largest to the smallest

### kevin piaget (view profile)

on 9 Dec 2012

defined function that sorts the elements of a vector (of any length) from largest to smallest.

this is my work

```function [ y ] = downsort_function( x )
%UNTITLED Summary of this function goes here
%   Detailed explanation goes here
n=numel(x);
t=1;
a=0;
while t==1;
for i=1:(n-1)
if x(i)>x(i+1)
x(i+1)=x(i);
t=1;
end
```
`   end`
```end
```

the problem is when i try to read with my vector, my machine gets stuck. it not giving me anything or i have to restart the machine.

thanks for yr help

## Products

No products are associated with this question.

### Walter Roberson (view profile)

on 9 Dec 2012

You never set t to 0 so your loop never exits.

Walter Roberson

### Walter Roberson (view profile)

on 9 Dec 2012

Correct, but I was going to let him discover that once the code did not infinite loop.

kevin piaget

### kevin piaget (view profile)

on 9 Dec 2012

```function [ y ] = downsort_function( x )
%UNTITLED Summary of this function goes here
%   Detailed explanation goes here
n=numel(x);
t=1;
a=0;
while t==0;
for i=1:(n-1)
if x(i)>x(i+1)
x(i+1)=x(i);
swap(x(i+1),x(i))
t=1;
end
```
`   end`
```end
```
Walter Roberson

### Walter Roberson (view profile)

on 10 Dec 2012

I cannot seem to locate any Mathworks function named "swap" ?

#### Join the 15-year community celebration.

Play games and win prizes!

MATLAB and Simulink resources for Arduino, LEGO, and Raspberry Pi