The Two Directional Selection Sort Algorithm - C++ code
By Sergey
![]()
![]()
![]()
![]()
Precondition: The TwoDirectionSelSort accepts two parameters: unsorted array [] and integer size that is the size of the array.
Post condition: Sorted array.
The function sorts the array in two directions. It checks if the current number is the smallest or the largest one. If it is the smallest, the number is moved to the front of the array.
If it is the largest, the number is moved to the end of the array. After each pass, the array becomes shorted by one number from the front and by one number from the end of the array
void TwoDirectionSelSort (
int size, int
array [] )
{
int current = 0;
bool sorted = false;
while ((current <
size-1) && (sorted = = false))
{
int
smallest = current; // the
first element of the array
int largest =
size - 1;
// the last element of the array
int walker = current
+1;
sorted = true;
if ( array [smallest] > array [largest] )
{
exchange ( array, smallest, largest );
sorted = false;
}
while( walker < size )
{
if ( array [walker] < array [smallest] )
{
exchange ( array, walker, smallest );
sorted = false;
}
if ( array [walker] > array [largest] )
{
exchange( array, walker, largest);
sorted = false;
}
walker = walker + 1;
}
current = current + 1;
size = size -1;
}
}
void exchange (int array[], int a, int b)
{
int temp=array[a];
array[a]=array[b];
array[b]=temp;
}
Did you find information useful?
Send to your friend a link to this page
Please rate the tutorial
Learn Visual Basic 6.0 [Kindle Edition] $1.99
How to Build Your Own Web Site from Scratch [Kindle Edition] $1.99
| Comments | |
|---|---|
- Home
- twoselcpp
- 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