OriginLab Corporation - Data Analysis and Graphing Software - 2D graphs, 3D graphs, Contour Plots, Statistical Charts, Data Exploration, Statistics, Curve Fitting, Signal Processing, and Peak Analysis
  Company    Products    Support    Solutions    Purchase    Downloads   
 


Products :  Origin :  Programming :  Origin C

 

   Image how Origin C can be used to create a Bubble Sort routine


What is in the example?

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.

How to use it

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.
Things to notice

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

  • In bsdShuffle, two Origin columns (one X and one Y) are mapped to two Dataset variables (e.g. Dataset dsCol1("data1",0);Dataset dsCol2("data1",1); ) . The X is populated with row numbers, while the Y is populated with random values using the rand (random) function as seen here:

    for(i=0;i<50;i++)
    {
    dsCol1[i]=i+1;
    dsCol2[i]=rand();
    }

  • In bsdShuffle, a series of LabTalk commands are used to sort the data in random order. To accomplish this, the commands are first placed into a string. The string is then executed by the LT_execute global function as seen below:

    strLTcmd="sort.wksname$=Data1;"
    "sort.c1=0;"
    "sort.r1=1;"
    "sort.r2=50;"
    "sort.cname1$=D: B;"
    "sort.wks( );"
    ;
    LT_execute(strLTcmd,0);


  • The function bsdSort takes one argument (iNumPass), an integer. This integer represents the number of passes made through the sorting routine.
  • In bsdSort, a do-while loop is used to perpetuate the sort as long as two conditions are true: 1) the flag called iAllSorted must return a non-zero value and 2) the number of passes (iNumPass) must be greater than the current pass index (iPass).

    do{
    iAllSorted = 1;
    for( i=0; i<iColLen-iPass; i++ )
    {
    dNum1 = dsCol[i];
    dNum2 = dsCol[i+1];
    if( dNum1>dNum2 )
    {
    dsCol[i] = dNum2;
    dsCol[i+1] = dNum1;
    iAllSorted = 0;
    }
    }
    iPass++;
    }while ( !iAllSorted && ( iPass < iNumPass ) );