# Exchange Sort Algorithm

The exchange sort is almost similar as the bubble sort. In fact some people refer to the exchange sort as just a different bubble sort. (When they see the source they even call it a bubble sort instead of its real name exchange sort.)

The exchange sort compares each element of an array and swap those elements that are not in their proper position,

just like a bubble sort does. The only difference between the two sorting algorithms is the manner in which they compare the elements.

The exchange sort compares the first element with each element of the array, making a swap where is necessary.

In some situations the exchange sort is slightly more efficient than its counter part the bubble sort. The bubble sort needs a final pass to determine that it is finished, thus is slightly less efficient than the exchange sort, because the exchange sort doesn’t need a final pass.

Take a look at the source code example:

```
#include<iostream>
using namespace std;
```

int main(void)
{
int array[5]; // An array of integers.
int length = 5; // Lenght of the array.
int i, j;
int temp;
//Some input
for (i = 0; i < 5; i++)
{
cout << "Enter a number: ";
cin >> array[i];
}
//Algorithm
for(i = 0; i < (length -1); i++)
{
for (j=(i + 1); j < length; j++)
{
if (array[i] < array[j])
{
temp = array[i];
array[i] = array[j];
array[j] = temp;
}
}
}
//Some output
for (i = 0; i < 5; i++)
{
cout << array[i] << endl;
}
}

That is all for this tutorial.

**or use**

**to share this post with others.**

its great!