#include
#include
using namespace std;
void bubbleSort1(int* pa, int size)
{
int* pi = pa;
// 2 nested for-loops
for (int i = 0; i < size; i++)
{
int* pj = pi+1;
for (int j = i + 1; j < size; j++)
{
if ( *pi > *pj)
{
// swap code
int temp = *pi;
*pi = *pj;
*pj = temp;
} // if
pj++;
} // for
pi++;
} // for
} // bubbleSort1
void bubbleSort2(int* pa, int size)
{
for (int i = 0; i < size; i++)
{
for (int j = i + 1; j < size; j++)
{
if (pa[i] > pa[j])
{
// swap code
int temp = pa[i];
pa[i] = pa[j];
pa[j] = temp;
} // if
} // for
} // for
} // bubbleSort2
int main()
{
const int SIZE = 7;
int day[] = {8, 44, 33, 55, 908, 39, 88};
cout << "Unsorted: ";
int i;
for (i = 0; i < SIZE; i++)
cout << day[i] << ' ';
cout << endl;
bubbleSort2(day, SIZE);
cout << "Sorted: ";
for (i = 0; i < SIZE; i++)
cout << day[i] << ' ';
cout << endl;
return 0;
} // main
Thursday, April 14, 2011
C++ bubbleSort
Subscribe to:
Post Comments (Atom)
Sponsored By
Edu-Tester is Powered by Blogger and Wearing Capung Template ; an XML Blogger Template Designed By Gre | Template-Godown
0 comments:
Post a Comment