View Full Version : Bubble Sort
Anyone familiar with a bubble sort? and if so how to implement it?
Anyone familiar with a bubble sort?
Lots of people are.
http://www.google.com/search?q=bubble+sort
and if so how to implement it?
using what?
http://www.google.com/search?hl=en&q=bubble+sort+matlab
http://www.google.com/search?hl=en&q=bubble+sort+c%2B%2B
http://www.google.com/search?hl=en&q=bubble+sort+python
http://www.google.com/search?hl=en&q=bubble+sort+basic
http://www.google.com/search?hl=en&q=bubble+sort+fortran
I have two array of numbers an x array and a y array. I want to sort the y array to find the minimum y value, but the number I am looking for is the corresponding x value. I do not know how to sort just the y array allowing each respective x value to remain with each y value. I tried building a matrix [x y] and sorting it but obviously it sorted both arrays. Comments Please.
Start with the procedure for sorting the y array and make sure that it works properly.
Then, wherever you swap two y values, add code that also swaps the corresponding x values. For example, if you are swapping the values in positions i and j:
temp = y[i];
y[i] = y[j];
y[j] = temp;
temp = x[i];
x[i] = x[j];
x[j] = temp;
If you sort the y values in ascending order, the first item in the x array after the sort has finished, is the one you want.
I tried adding the code you gave me into the bubble sort i have, however I am having a problem. Why does this not work?
function y= bubble(x,y)
n = length(x);
for k = 1:n-1
for j = 1:n-k
if(x(j)> x(j+1))
temp = x(j);
x(j) = x(j+1);
x(j+1) = temp;
temp = y(j);
y(j) = y(j+1);
y(j+1) = temp;
end % if
end % for
end % for
y = x;
I am having a problem
According to the description in post #3, you want to sort the y array. Therefore in your "if" statement you should be comparing two y values, not two x values. Could that be your problem?
vBulletin® v3.8.7, Copyright ©2000-2012, vBulletin Solutions, Inc.