Home Forums C Programming please help me Re: Re: please help me

#3590
GWILouisaxwzkla
Participant

Heres a bubble sort I wrote awhile ago ( hope this helps ):



/****************************************************************
* File Name : c:programstempCG.cpp
* Date : March,28,2009
* Comments : new project
* Compiler/Assembler : Visual C++ 6.0
* Modifications :
*
* Notes: A good sort for a small number of items. Total run time
* is proportional to the number of items squared ( o ( N^2 ) ).
*
*
* Program Shell Generated At: 11:25:48 a.m.
=-=-=-=-=-=-=-=--=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=*/


#include < iostream >
//#include < string.h >
//#include < conio.h >
//#include < math.h >
//#include < iomanip >
//#include < ctype.h >
#include
#include

using namespace std;

//>>>>>>>>>>>>>>>>>>>>>>>> GLOBAL DATA <<<<<<<<<<<<<<<<<<<<<<<<<
const int MAX_NUMBERS = 50;
int NUMBERS_PER_LINE = 15;
//<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<




//@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@ FUNCTION PROTOTYPES @@@@@@@@@@@@@@@@@@@@@@@@@@

void generateNumbers ( int * numbers , int numberItems );
void bubbleSort ( int * array , int numberItems );
void printArray ( int * array , int numberItems );

//##################################################################################


//main function ******************************

int main ( )
{

int array [ MAX_NUMBERS ];
generateNumbers ( array , MAX_NUMBERS );
cout << endl << endl;
cout << "unsorted array " << endl;
printArray ( array , MAX_NUMBERS );
bubbleSort ( array , MAX_NUMBERS );
cout << endl << endl << "sorted array: " << endl;
printArray ( array , MAX_NUMBERS );
cout << endl;


return 0 ;
}


/******************************* FUNCTION DEFINITION ******************************

Name : bubbleSort
Parameters :

array a(n) int * ,
numberItems a(n) int


Returns: Void type
Comments:



++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++*/
void bubbleSort ( int * array , int numberItems )
{

int endSortedArray = numberItems - 1;
int lastSwapIndex;
int temp;
while ( endSortedArray > 0 ) //while not at the end of the unsorted array
{
lastSwapIndex = 0; //save index of the last item swapped
int i = 0; //start at the beggining of the unsorted array
while ( i < endSortedArray ) //while not in the sorted items
{
if ( array [ i ] > array [ i + 1 ] ) //if current item is smaller than next , bubble up
{
//swap array [ i ] and array [ i + 1 ]
temp = array [ i ];
array [ i ] = array [ i + 1 ];
array [ i + 1 ] = temp;
lastSwapIndex = i;
}
i ++;
}
endSortedArray = lastSwapIndex; //reset swap boundry
}



return;
}
/******************************* FUNCTION DEFINITION ******************************

Name : printArray
Parameters :

array a(n) int * ,
numberItems a(n) int


Returns: Void type
Comments:



++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++*/
void printArray ( int * array , int numberItems )
{


int i = 0;
cout << endl << endl;
cout << "array : " << endl;
while ( i < numberItems )
{
cout << array [ i ] << " " ;
if ( i != 0 )
if ( i % NUMBERS_PER_LINE == 0 )
cout << endl;
i ++;
}

return;
}
/******************************* FUNCTION DEFINITION ******************************

Name : generateNumbers
Parameters :

numbers a(n) int ,
numberItems a(n) int


Returns: Void type
Comments:



++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++*/
void generateNumbers ( int * numbers , int numberItems )
{

int i = 0;

srand ( time ( NULL ) );
while ( i < numberItems )
{
numbers [ i ] = rand () % MAX_NUMBERS;
i ++;
}


return;
}