VB icon

Bubble Sort

Email
Submitted on: 1/1/2015 9:10:00 AM
By: Joshua Thompson (from psc cd)  
Level: Beginner
User Rating: By 5 Users
Compatibility: C, C++ (general)
Views: 1250
 
     The purpose of this code is to show novice programmers the simple and classic sorting method of Bubble Sort. The code is in C/C++ and should be compilied in DJGPP. The code includes the Bubble Sort Algorithm in action.
 

INCLUDE files:

Can't Copy and Paste this?
Click here for a copy-and-paste friendly version of this code!
//**************************************
//INCLUDE files for :Bubble Sort
//**************************************
#include <stdio.h>
#include <stdlib.h>
#include <conio.h>
code:
Can't Copy and Paste this?
Click here for a copy-and-paste friendly version of this code!
				
//**************************************
// Name: Bubble Sort
// Description:The purpose of this code is to show novice programmers the simple and classic sorting method of Bubble Sort. The code is in C/C++ and should be compilied in DJGPP. The code includes the Bubble Sort Algorithm in action.
// By: Joshua Thompson (from psc cd)
//
// Inputs:No Inputs
//
// Returns:No Returns
//
// Assumes:The code is complete. The Bubble Sort Algorithm is modulizied inside its own function. It is called from main, the current implementation sorts an array of ints, but can be modified to sort varying data types.
//
// Side Effects:No Side Effects
//**************************************

/* Function Prototypes */
void BubbleSort( int Array[], const int Size );
void PrintArray( int Array[], const int Size );
int main( void )
{
 int i;
 const int Size = 20;
 int Array[ Size ];
 /* Fill the Array with random values 
 between 0 and 99 */
 for( i = 0; i < Size; i++ )
Array[i] = random() % 100;
 /* Print the Random Array to Screen */
 clrscr();
 printf( "The Array with random order:\n\n");
 PrintArray( Array, Size );
 /* Wait for key Press... */
 printf( "\nPress any key..." );
 getch();
 /* Sort the Array using Bubble Sort */
 BubbleSort( Array, Size );
 /* Print the Smallest-to-Largest 
 Order Array */
 clrscr();
 printf( "The Array after Bubble Sort:\n\n");
 PrintArray( Array, Size );
 /* End the Program */
 printf( "\nPress any key to quit..." );
 getch();
 return 0;
}
/* Uses the classic bubble sort algorithm */
void BubbleSort( int Array[], const int Size )
{
 int i, j, temp;
 for( i = 0; i < Size - 1; i++ )
for( j = 0; j < Size - i + 1; j++ )
 if( Array[j] > Array[j + 1] )
 {
temp = Array[j];
	Array[j] = Array[j + 1];
	Array[j + 1] = temp;
 }
}
/* Prints an integer Array line by line */
void PrintArray( int Array[], const int Size )
{
 int i;
 
 for( i = 0; i < Size; i++ )
printf("Array[%i] = %i\n", i, Array[i] );
}


Other 1 submission(s) by this author

 


Report Bad Submission
Use this form to tell us if this entry should be deleted (i.e contains no code, is a virus, etc.).
This submission should be removed because:

Your Vote

What do you think of this code (in the Beginner category)?
(The code with your highest vote will win this month's coding contest!)
Excellent  Good  Average  Below Average  Poor (See voting log ...)
 

Other User Comments


 There are no comments on this submission.
 

Add Your Feedback
Your feedback will be posted below and an email sent to the author. Please remember that the author was kind enough to share this with you, so any criticisms must be stated politely, or they will be deleted. (For feedback not related to this particular code, please click here instead.)
 

To post feedback, first please login.