0% found this document useful (0 votes)
79 views

Bubblesort

Uploaded by

Subhankar Bose
Copyright
© Attribution Non-Commercial (BY-NC)
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
79 views

Bubblesort

Uploaded by

Subhankar Bose
Copyright
© Attribution Non-Commercial (BY-NC)
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 1

/*------------------------------------------------------------------------------*

* File Name: BubbleSortDemo.c *


* Creation: *
* Purpose: Demonstration of Bubble Sort *
* Copyright (c) ABCD Corp. 2003, 2004, 2005, 2006, 2007, 2008, 2009, 2010 *
* All Rights Reserved *
* *
* Modification Log: *
*------------------------------------------------------------------------------*/

////////////////////////////////////////////////////////////////////////////////////
// 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);

void bsdSort(int iNumPass)


{
Dataset dsCol("data1",0);
int iColLen = dsCol.GetSize();

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

You might also like