Skip to content
Permalink
master
Switch branches/tags

Name already in use

A tag already exists with the provided branch name. Many Git commands accept both tag and branch names, so creating this branch may cause unexpected behavior. Are you sure you want to create this branch?
Go to file
 
 
Cannot retrieve contributors at this time
#include <iostream>
using namespace std;
void doubEndSelSort (int *sequence, int x) //double ended selection sort function
{
for (int i = 0, j = x - 1; i < j; i++, j--) //nested for loop to check the minimum and maximum in the sequence
{
int mini = sequence[i], maxi = sequence[i]; //initating values
int mini_i = i, maxi_i = i; //initating values as well
for (int k = i; k <= j; k++)
{
if (sequence[k] > maxi) //if the sequence "k" bigger than maximum
{
maxi = sequence[k]; //maximum then equals sequence "k" value
maxi_i = k; //maximum index equals "k" value
}
else if (sequence[k] < mini)
{ //else if sequence "k" smaller than minimum
mini = sequence[k]; //minimum then equals sequence "k" value
mini_i = k; //minimum index equals "k" value
}
}
//use of the swap function
swap (sequence[i], sequence[mini_i]); //swaps the sequence [i] value for the sequence [mini_i] value
if (sequence[mini_i] == maxi) //if the sequence [mini_i] equals maxi value then
swap (sequence[j], sequence[mini_i]); //swaps the sequence [j] value for the sequence [mini_i] value
else //else if it does not equal maxi value then
swap (sequence[j], sequence[maxi_i]); //swaps the sequence [j] value for the sequence [maxi_i] value
}
}
// main function
int main ()
{
int sequence[] = { 13, 82, 54, 5, 32, 69, 2 }; //create the sequence and give the index values
int x = sizeof (sequence) / sizeof (sequence[0]); //find the lenght of the sequence
doubEndSelSort (sequence, x); //call the function and pass values
std::cout << ("Sorted array:\n") << std::endl; //simply outputting a message
for (int i = 0; i < x; i++) //a for loop to print the sequence
cout << sequence[i] << " ";
cout << endl;
return 0;
}