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:

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];

	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.

This entry was posted in Programming Algorithms. You can follow any responses to this entry through the RSS 2.0 feed. You can leave a response, or trackback from your own site. Tweet This! Tweet This! or use to share this post with others.

There is currently one response to “Exchange Sort Algorithm”

Why not let us know what you think by adding your own comment!

  1. pranav asthana on November 8th, 2009:

    its great!

Leave a Reply: