Bubblesort
Bubblesort
////////////////////////////////////////////////////////////////////////////////////
// you must include this header file for all Origin built-in functions and classes
#include <origin.h>
//
////////////////////////////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////////////////////////
// start your functions here
//------------------------------------------------------------------------------------
//
// We define two functions in this file:
// 1> bsdShuffle(): This function orders the data in the graph in random order
// 2> bsdSort(n): This function sorts the data using the bubble sort algorithm
// for n passes
//
// The bsdShuffle() function also demonstrates calling LabTalk commands from within
// an Origin C function.
//
//------------------------------------------------------------------------------------
void bsdShuffle()
{
// define two Dataset variable and map them to the 1st and 2nd columns of the worksheet
Dataset dsCol1("data1", 0);
Dataset dsCol2("data1", 1);
dsCol1.SetSize(50);
dsCol2.SetSize(50);
//first fill 1st column with row numbers and 2nd column with random nos.
for(int i=0; i<50; i++)
{
dsCol1[i] = i+1;
dsCol2[i] = rand();
}
// Now sort the 1st column in random order by sorting the worksheet based on the 2nd column
int nSortByCol = 1;
Worksheet wks("data1");
wks.Sort(nSortByCol, SORT_DESCENDING);
double dNum1,dNum2;
int iAllSorted, iPass = 1;
do{
iAllSorted = 1;
for( int 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 ) );
}
Page 1/1