عزیزان این هم سورس مرتب سازی حبابی
#include <iostream.h>
#include <string.h>
#include <stdlib.h>
// swap two strings
void sswap( char **str1, char **str2 )
{
char *tmp = *str1;
*str1 = *str2;
*str2 = tmp;
tmp = NULL;
}
// ordering strings alphabetically in ascendant or descendant order
void alphasort( char **str, const int &strnum, bool ascendant )
{
int result = 0;
for( int i = strnum - 1; i > 0; i-- )
{
for( int j = 0; j < i; j++ )
{
result = strcmp( str[j], str[j+1] );
if(( ascendant && result > 0 ) ||
( !ascendant && result < 0 )) {
sswap( &str[j], &str[j+1] );
}
}
}
}
// displays a list countries on the screen
void displayCountries( char **Country, int country_number )
{
for( int k = 0; k < country_number; k++ )
{
cout << "(" << k+1 << ")" << Country[k];
if( k != country_number - 1 ) {
cout << endl;
}
}
}
void main()
{
const int number = 12; // the number of countries
char *Country[number] = {
"France", "Russia", "Ghana", "Canada",
"Zimbabwe", "Japan", "Tahiti", "Cuba",
"China", "Australia", "USA", "Greece"
};
cout << "\nList of countries before alphabetical ordering:" << endl;
displayCountries( Country, number );
// perform ascendant alphabetical ordering
alphasort( Country, number, 1 );
cout << "\n\n\n" << "after ascendant alphabetical ordering:" << endl;
displayCountries( Country, number );
// perform descendant alphabetical ordering
alphasort( Country, number, 0 );
cout << "\n\n\n" << "after descendant alphabetical ordering:" << endl;
displayCountries( Country, number );
cout << endl;
}