The two directions bubble sort algorithm - C++ source code - Learning C++
By Sergey Skudaev
![]()
![]()
![]()
![]()
![]()
Precondition: The function accepts an unsorted array [] and integer size that is the size of the array.
Post condition: Sorted array
The function performs the two direction bubble sort. It starts from the head and from the tail of the array and compares two adjusted cells. If the numbers are not in order, they are exchanged.
After each pass, the array becomes shorter by one cell from the head and by one cell from the tail.
void TwoBubbleSort ( int
size, int array [] )
{
int current =
0;
bool sorted =
false;
while
(( current < size )&&( sorted = = false
))
{
int walker1 = size -1;
// walks from the tail to the head
int walker2 = current;
// walks from the head to the tail
sorted = true;
while ( walker1 > current )
{
if ( array [walker1] < array [walker1-1]
)
{
sorted = false;
exchange ( array, walker1, walker1-1 );
}
walker1 = walker1 - 1;
if ( array [walker2] > array [walker2 + 1]
)
{
sorted = false;
exchange ( array, walker2, walker2 + 1 );
}
walker2 = walker2 + 1;
}
current = current + 1 ;
size = size -1;
}
}
{
int temp=array[a];
array[a]=array[b];
array[b]=temp;
}
See my DEMO in Java that shows how sorting is performed.
Did you find information useful?
Send to your friend a link to this page
If you like this page click +1 button.
Please rate the tutorial
How to Build Your Own Web Site from Scratch [Kindle Edition] $2.99
Earn Money on Internet as an Affiliate [Kindle Edition] $0.99
| Comments | |
|---|---|
- Home
- twobubcpp
- Batch Files
- Java Properties
- Form Validation
- Display Image PHP
- Upload File PHP
- phpMyAdmin
- Environment Variables
- Delete Trojan horse
- C ++ Code Examples
- Start C++
- C ++ Continue
- C ++ Pointer
- Variable Scope
- Linked List
- Bubble Sort
- Two Bubble Sort
- Insertion Sort
- Selection Sort
- Two Selection Sort
- Database Design
- Password Keeper
- MySQL and Excel
- Learn MySQL
- Learn PHP
- Learn Visual Basic
- Learn Java
- Web Site Tips
- User_Auth. Demo
- Merge Bin Sort
- Advertise here
- Using Twitter
- Site map
- Registration
Web programming Tips