Understanding Bubble Sort Algorithms

Click For Summary

Discussion Overview

The discussion revolves around the implementation of bubble sort algorithms, particularly in the context of sorting one array while maintaining the association with another array. Participants explore the mechanics of bubble sort, share implementation details, and address specific coding challenges related to sorting paired data.

Discussion Character

  • Technical explanation
  • Homework-related
  • Debate/contested

Main Points Raised

  • Some participants inquire about the basic implementation of bubble sort and its applications in different programming languages.
  • One participant describes a scenario where they need to sort a y array while keeping the corresponding x values intact, indicating a challenge with sorting paired data.
  • A suggestion is made to modify the bubble sort algorithm to include swapping corresponding x values whenever y values are swapped.
  • A participant expresses difficulty with their implementation of bubble sort, questioning why their code does not work as intended.
  • Another participant points out that the comparison in the sorting logic should be between y values instead of x values, suggesting this may be the source of the problem.

Areas of Agreement / Disagreement

Participants generally agree on the need to sort the y array while maintaining the relationship with the x array. However, there is disagreement regarding the specific implementation details and the source of errors in the provided code.

Contextual Notes

There are unresolved issues regarding the specific implementation of the bubble sort algorithm, particularly in the context of maintaining paired data. The discussion does not clarify all assumptions or dependencies related to the sorting process.

Juanka
Messages
37
Reaction score
0
Anyone familiar with a bubble sort? and if so how to implement it?
 
Physics news on Phys.org
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;
y = y[j];
y[j] = temp;

temp = x;
x = 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?

Code:
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;
 
Juanka said:
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?
 
Last edited:

Similar threads

  • · Replies 9 ·
Replies
9
Views
2K
  • · Replies 6 ·
Replies
6
Views
1K
  • · Replies 8 ·
Replies
8
Views
2K
  • · Replies 22 ·
Replies
22
Views
2K
  • · Replies 13 ·
Replies
13
Views
3K
  • · Replies 8 ·
Replies
8
Views
3K
  • · Replies 11 ·
Replies
11
Views
2K
Replies
3
Views
1K
  • · Replies 6 ·
Replies
6
Views
1K
  • · Replies 3 ·
Replies
3
Views
2K