Frequently used routines, such as sorting, can be implemented as standalone functions. This example demonstrates how a bubble sort algorithm can be incorporated into an Origin C function and used to sort data in a worksheet column. The effect is heightened by a graphical display of the worksheet data as a Bar graph.
If you have Origin or the Origin Evaluation/Demo, open the sample project called "Bubble Sort.OPJ" in the \Origin\Samples\Programming\Bubble Sort subfolder. Alternatively, download the example in ZIP form, extract its contents, and open Bubble Sort.OPJ. Bubble Sort.OPJ contains instructions on how to run the demonstration.
- Start - Compiles Bubble Sort.C and populates the graph window with a random bar plot containing 50 data points.
- Shuffle - Redistributes the 50 data points in the bar plot in a random fashion.
- Sort All - Makes 50 passes through the sort algorithm, thereby sorting all 50 bars in the bar plot.
- Step Sort - Makes one pass through the same sort algorithm, thereby restricting the sorting to each bar in the bar plot with its nearest neighbor.
When you look at the project and its associated code (PDF), take a look at the comments to learn more about Origin C syntax. Some things to notice are that:
There are only two functions: bsdShuffle and bsdSort in Bubble Sort.C, but 4 buttons on the demonstration graph: Start, Shuffle, Sort All, and Step Sort.
The Buttons
- Start - This button calls the bsdShuffle function after loading and compiling Bubble Sort.C. This initiates the demonstration. The LabTalk object method, run.loadOC, is used to load and compile Bubble Sort.C automatically.
- Shuffle - This button also calls bsdShuffle, but since the C file has already been compiled (by clicking the Start button), the run.loadOC method is not required.
- Sort All - This button calls bsdSort with 50 as the argument. Since the bar graph has 50 data points, the whole plot is sorted.
- Step Sort - This button calls bsdSort with 1 as the argument.
To see how the calls are made, ALT + double-click on the button your are interested in to open its Label Control dialog.
The Functions
