Important alert: (current site time 7/15/2013 5:52:47 PM EDT)
 

VB icon

A Simple Bubble Sort

Email
Submitted on: 12/10/2001 12:25:34 PM
By: Brutus 
Level: Beginner
User Rating: By 7 Users
Compatibility: Microsoft Visual C++
Views: 66555
 
     Sorts a pre-definied array into order. My second submission under the C++ section, and my second day of learning this language..so go easy :)
 
code:
Can't Copy and Paste this?
Click here for a copy-and-paste friendly version of this code!
 
Terms of Agreement:   
By using this code, you agree to the following terms...   
  1. You may use this code in your own programs (and may compile it into a program and distribute it in compiled format for languages that allow it) freely and with no charge.
  2. You MAY NOT redistribute this code (for example to a web site) without written permission from the original author. Failure to do so is a violation of copyright laws.   
  3. You may link to this code from another website, but ONLY if it is not wrapped in a frame. 
  4. You will abide by any additional copyright restrictions which the author may have placed in the code or code's description.
				
//**************************************
// Name: A Simple Bubble Sort
// Description:Sorts a pre-definied array into order. My second submission under the C++ section, and my second day of learning this language..so go easy :)
// By: Brutus
//
//This code is copyrighted and has// limited warranties.Please see http://www.Planet-Source-Code.com/vb/scripts/ShowCode.asp?txtCodeId=2966&lngWId=3//for details.//**************************************

#include <iostream.h>
#include <string.h>
#include <conio.h>
	//jumbled up text in array
	char strarray[] = "fgjhsflsdlkfghdksdkjdgskakdkfkjggkdkgjg";
	int i = 0;
	int j = 0;
	char temp;
	void Bubble(char* strarray, int arrsize);
	
int main()
{
cout << strarray << endl;
Bubble(strarray, strlen(strarray));
cout << strarray << endl;
	
return 0;
}
	
void Bubble(char* strarray, int arrsize) //Bubble Sort code
{
	for(i=0; i< (arrsize - 1); ++i)
	{
		for(j = i + 1; j > 0; --j)
		{
			if(strarray[j] < strarray[j-1])
			{
				//Swaps the values
				temp = strarray[j];
				strarray[j] = strarray[j - 1];
				strarray[j - 1] = temp;
			}
			
		}
	}
}


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

4/26/2002 11:54:59 AMPaul Spiteri

nice one sir
smaller than the one i'd seen before, and logical. good
(If this comment was disrespectful, please report it.)

 
8/8/2002 12:51:29 PMBob

The Bubble Sort code is poor and demonstrates bad habits. It uses global variables where none are needed, it should use the C++ swap() function instead of handcoding, and the array length could be calculated inside the bubble sort routine instead of passed as a parameter. With just a few small changes it could be a much better piece of code.
(If this comment was disrespectful, please report it.)

 
2/23/2003 10:53:40 AMRyanD

note that the array length of an c-style array passed to a function cannot be calculated within that function -- i don't know where you got that from... and who cares about using the std swap() function, i mean, i'm sure you don't know all of the functions in the stl... it's not like anything is 'hardcoded'... the purpose of this is to show a bubble sort, and it was shown. the global variables aren't good for style, but look at the size of the code, it's like 30 lines or less -- who cares.

...i'm kind of getting sick of all of the commenters at psc who complain about things that they know nothing about
(If this comment was disrespectful, please report it.)

 
3/10/2006 1:12:29 PMraju

its good and very simple programe......
(If this comment was disrespectful, please report it.)

 
6/1/2007 6:50:46 PM

thanks a lot for sharing this
(If this comment was disrespectful, please report it.)

 
10/21/2007 11:04:26 AMRenjini

for(int x=0; x
{

for(int y=x+1; y
{

if(array[x]>array[y])

{

int temp = array[x];

array[x] = array[y];

array[y] = temp;

}

}

}

can we use this logic? is it correct?
(If this comment was disrespectful, please report it.)

 
12/13/2007 6:14:23 AMkenneth

can u tell me a simple method for the bubble sort...not using return or functions
(If this comment was disrespectful, please report it.)

 
9/20/2008 2:54:36 AMryan

good

(If this comment was disrespectful, please report it.)

 

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.