Understanding Bubble Sort Algorithms

In summary, the conversation is about implementing a bubble sort, specifically for sorting the y array while keeping the corresponding x values. The suggested solution is to first sort the y array and then add code to swap the corresponding x values during the sorting process. A potential issue with the code provided is that it is comparing x values instead of y values.
  • #1
Juanka
40
0
Anyone familiar with a bubble sort? and if so how to implement it?
 
Physics news on Phys.org
  • #2
  • #3
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.
 
  • #4
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.
 
  • #5
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;
 
  • #6
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:

What is a bubble sort algorithm?

A bubble sort algorithm is a simple sorting algorithm that repeatedly steps through a list, compares adjacent elements and swaps them if they are in the wrong order. This process is repeated until the entire list is sorted.

How does a bubble sort algorithm work?

A bubble sort algorithm works by comparing adjacent elements in a list and swapping them if they are in the wrong order. This process is repeated until the entire list is sorted. Each pass through the list will result in the largest number being moved to the end of the list.

What are the advantages of using a bubble sort algorithm?

The main advantage of using a bubble sort algorithm is that it is simple to implement and requires minimal code. It also has a space complexity of O(1), meaning it requires very little additional memory to sort a list. Additionally, it is efficient for sorting small lists or lists that are almost sorted.

What are the limitations of a bubble sort algorithm?

The main limitation of a bubble sort algorithm is that it has a time complexity of O(n^2), meaning it is not efficient for sorting large lists. It also does not take into account the initial ordering of the list, meaning it will still go through all the iterations even if the list is already sorted.

How can the efficiency of a bubble sort algorithm be improved?

The efficiency of a bubble sort algorithm can be improved by adding a flag to check if any swaps were made during a pass through the list. If no swaps were made, it means the list is already sorted and the algorithm can stop. This reduces the number of iterations needed to sort the list and improves the algorithm's time complexity.

Similar threads

  • Cosmology
Replies
8
Views
1K
  • Introductory Physics Homework Help
Replies
3
Views
240
  • High Energy, Nuclear, Particle Physics
Replies
2
Views
1K
  • Programming and Computer Science
Replies
6
Views
810
  • General Discussion
Replies
6
Views
933
  • Special and General Relativity
Replies
32
Views
795
Replies
9
Views
1K
Replies
20
Views
3K
  • Programming and Computer Science
Replies
7
Views
1K
  • Classical Physics
Replies
9
Views
1K
Back
Top