Balloon Sorting in C++
Tuesday, October 26, 2010
Question: What is a Balloon Sort?
Answer: BALLOON SORT is actually an another term for EXCHANGE SORT. So here it is:
The balloon sort is similar to its cousin, the bubble sort, in that it compares elements of the array and swaps those that are not in their proper positions. (Some people refer to the "balloon sort" as a "bubble sort".)
The difference between these two sorts is the manner in which they compare the elements. The balloon sort compares the first element with each following element of the array, making any necessary swaps
.
When the first pass through the array is complete, the balloon sort then takes the second element and compares it with each following element of the array swapping elements that are out of order. This sorting process continues until the entire array is ordered.
The balloon sort, in some situations, is slightly more efficient than the bubble sort. It is not necessary for the balloon sort to make that final complete pass needed by the bubble sort to determine that it is finished.
//Balloon Sort Function for Descending Order
void ExchangeSort(apvector &num)
{
int i, j;
int temp; // holding variable
int numLength = num.length( );
for (i=0; i< (numLength -1); i++) // element to be compared
{
for(j = (i+1); j < numLength; j++) // rest of the elements
{
if (num[i] < num[j]) // descending order
{
temp= num[i]; // swap
num[i] = num[j];
num[j] = temp;
}
}
}
return;
}
void ExchangeSort(apvector
{
int i, j;
int temp; // holding variable
int numLength = num.length( );
for (i=0; i< (numLength -1); i++) // element to be compared
{
for(j = (i+1); j < numLength; j++) // rest of the elements
{
if (num[i] < num[j]) // descending order
{
temp= num[i]; // swap
num[i] = num[j];
num[j] = temp;
}
}
}
return;
}
Sharing is so Easy: |
Labels:
C++,
Programming