0% found this document useful (0 votes)
1K views

Exchange Sort

The exchange sort algorithm compares each element of an array to all other elements and swaps them if out of order. It does this by comparing the first element to all others, swapping if needed, then moving to the next element. This process repeats until the array is fully sorted. The time complexity of exchange sort is O(n(n-1)/2) in all cases (best, average, worst) as the number of comparisons does not depend on the input ordering.

Uploaded by

Achyut Kayastha
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
1K views

Exchange Sort

The exchange sort algorithm compares each element of an array to all other elements and swaps them if out of order. It does this by comparing the first element to all others, swapping if needed, then moving to the next element. This process repeats until the array is fully sorted. The time complexity of exchange sort is O(n(n-1)/2) in all cases (best, average, worst) as the number of comparisons does not depend on the input ordering.

Uploaded by

Achyut Kayastha
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 3

Exchange sort: 

 
● The exchange sort compares each element of an array and 
swaps those elements that are not in their position. 
● The exchange sort compares the first element with each 
element of the array, and makes swap whenever it is necessary. 
● This technique is similar to bubble sort but there is slightly 
difference in the matter they compare the element. 
 
Example: 
Step 1: Select index 0 element, and compare it with all other 
elements in the array with higher index, and swap when it is not in 
order. 
 
Let us consider the following array: 
4  8  9  6  2  5 
 
Iteration 1:Compare 4 with 8, no exchange. 
4  8  9  6  2  5 
 
Iteration 2:Compare 4 with 9, no exchange. 
4  8  9  6  2  5 
 
Iteration 3:Compare 4 with 6,no exchange. 
4  8  9  6  2  5 
 
Iteration 4:Compare 4 with 2, exchange. 
4  8  9  6  2  5 
 
After exchange: 
2  8  9  6  4  5 
 
Iteration 5:Compare 2 with 5, no exchange. 
 
2  8  9  6  4  5 
 
In the first round 2 is sorted. 
2  8  9  6  4  5 
 
Similarly rest other elements are also sorted. 
 
 
 
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; 
Endif 
End for 
End for 
 
 
 
Best, Worst and Average case analysis: 
 
Worst case: 
A determination of the maximum amount of time that an algorithm 
requires to solve problems of size n. 
 
 
Best case: 
A determination of the minimum amount of time that an algorithm 
requires to solve problems of size n. 
 
Average case: 
A determination of the average amount of time that an algorithm 
requires to solve problems of size n. 
 
Counting an algorithm's operations is a way to access its efficiency 
An algorithm’s execution time is related to the number of operations 
it requires 
Examples  
• Traversal of a linked list  
• Nested Loops 
 
Number of operation(nested loop) for exchange sort is given by: 
f(n) = 1+2+3+.........+(n-1) 
= n(n-1)/2 
 
It can be specified using big O notation as: O(f(n)) = O(n(n-1)/2). 
 
The time required for execution remains the same without regard to 
the input given. So efficiency remains the same. 
★ Efficiency: 
➔ Worst case: O(n(n-1)/2) 
➔ Average case: O(n(n-1)/2) 
➔ Best case: O(n(n-1)/2) 
 

You might also like