LabVIEW

cancel
Showing results for 
Search instead for 
Did you mean: 

How to perform Bubble Sort

Solved!
Go to solution

I am looking for a way to create a VI that works its way through the elements of an array, compares adjacent element and if the first element is smaller than the second, swapping them over. When the swap reaches the end of the array it goes back to the beginning and repeats until everything is in order a maximum of N-1 times. It would also be great if you could advise me on how to combine this with a VI that generates random numbers. Many thanks

0 Kudos
Message 1 of 10
(5,358 Views)
Solution
Accepted by topic author adam_woolsey

To generate an array of random numbers use one of the signal generation tools or simply place a set of dice inside a FOR loop, auotindexing at the loop boundary. Or create an integer ramp, followed by a riffle.

 

To implement your sort, use a while loop with a shift register initialized with your array to be sorted. Swap elements with each iteration until the array is sorted. Stop the loop and display the sorted array coming out of the shift register output.

 

Your questions are quite trivial. You probably should start with some tutorials first. 😉

Message 2 of 10
(5,346 Views)
Solution
Accepted by topic author adam_woolsey
Message 3 of 10
(5,339 Views)

Try this code bro.it works perfectly in LabVIEW 2014 and later.KudosSmiley Very Happy

0 Kudos
Message 4 of 10
(5,010 Views)

Hi Prabsun,

 

apart from posting in an old thread: why do you post such inefficient code?

 

After some cleanup work:

check.png

 

You might even use the Inplace structure:

check.png

 

Best regards,
GerdW


using LV2016/2019/2021 on Win10/11+cRIO, TestStand2016/2019
Message 5 of 10
(5,000 Views)

@Prabsun wrote:

Try this code bro.it works perfectly in LabVIEW 2014 and later.KudosSmiley Very Happy


Gerd already touched some points, but here are a few:

  • Why is your diagram and front panel maximised to the screen? Very annoying!
  • Obviously, taking the size of the array twice (once inside the loop) is redundant. The result never changes. Once before the loop is enough!
  • Before seeing Gerd's code, you haven't noticed yet that index array and replace array subset is resizeable.
  • Why do you do a "+1" of the same wire segment twice? Again, once is enough. (with resized nodes, it is not even needed, see Gerd's solution).
  • Your outer loop runs once too many.
  • Your inner loop runs way too many times. You can reduce the number of iterations by one for ever iteration of the outer loop. (further improvements are possible, see here)
  • If this is supposed to be a subVI, assign connectors and an icon.
  • If it is just experimental code, compare the result with he output of "sort array" and include verification code.
  • Your code does not work correctly if some of the input elements are NaN. It shoud create the same output as "sort array" in all cases.
  • ...
Message 6 of 10
(4,994 Views)

I'm just trying to help him.I'm not doing it for money or there is no requirements.If you want any changes,do it by yourself.

0 Kudos
Message 7 of 10
(4,984 Views)

Hi Prabsun,

 

and we just try to help you by suggesting improvements to your code to make it more "LabVIEW-like"!

 

(We also don't get money for our contributions. And we already presented our changes to your VI. Are you complaining about our comments?)

Best regards,
GerdW


using LV2016/2019/2021 on Win10/11+cRIO, TestStand2016/2019
0 Kudos
Message 8 of 10
(4,973 Views)

@Prabsun wrote:

I'm just trying to help him.I'm not doing it for money or there is no requirements.If you want any changes,do it by yourself.


Writing code for a well delineated problem is a great exercise to learn programming and your contributions are highly welcome. This is a place to learn, so we sometimes take the opportunity to suggest improvements and this should be seen in a positive way. We all started learning once.

 

One of the best ways to become a better programmer is to try to solve a programming problem, and once it seems to work correctly (congratulations!) we can start focusing on little (or big!) improvements. We also need to make sure it works for all unusual inputs (e.g. empty array, some or all elements NaN, duplicate elements, Inf, -Inf, etc.). We should also explore how it behaves as the input array gets large (See also my NI-Week 2016 talk). Bubble sort is O(N²), so it scales poorly once the arrays get very large and other sorting algorithms are preferable. It would be an interesting exercise to do a few other algorithms and compare them. 

 

Some of the veterans here have been programming in LabVIEW for well over 20 years, and this collective expertise and experience is a fantastic source to speed up the learning curve. Somtimes even we learn something new here.

 

Nobody gets paid here. We are all volunteers with an interest in spreading the word why LabVIEW is such a great programming language.

 

Your code was not bad at all! We've seen much worse. 😄 (there is a thread with almost 2000 messages discusing some really cringeworthy examples. 🐵

 

 

 

0 Kudos
Message 9 of 10
(4,955 Views)

you can learn about Bubble sort performance in detail here : 

https://youtu.be/dzQwlsg-rro

0 Kudos
Message 10 of 10
(4,598 Views)