04-29-2015 02:05 PM
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
Solved! Go to Solution.
04-29-2015 02:24 PM - edited 04-29-2015 02:25 PM
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. 😉
04-29-2015 02:28 PM
12-29-2016 11:50 PM
Try this code bro.it works perfectly in LabVIEW 2014 and later.Kudos
12-30-2016 01:56 AM
12-30-2016 02:56 AM - edited 12-30-2016 03:07 AM
@Prabsun wrote:
Try this code bro.it works perfectly in LabVIEW 2014 and later.Kudos
Gerd already touched some points, but here are a few:
12-30-2016 03:56 AM
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.
12-30-2016 06:06 AM
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?)
12-30-2016 01:16 PM - edited 12-30-2016 03:52 PM
@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. 🐵
06-17-2017 12:01 PM
you can learn about Bubble sort performance in detail here :