Related Articles
C/C++ Program for Odd-Even Sort / Brick Sort
• Last Updated : 04 Dec, 2018

This is basically a variation of bubble-sort. This algorithm is divided into two phases- Odd and Even Phase. The algorithm runs until the array elements are sorted and in each iteration two phases occurs- Odd and Even Phases.

In the odd phase, we perform a bubble sort on odd indexed elements and in the even phase, we perform a bubble sort on even indexed elements.

 `// A C++ Program to implement Odd-Even / Brick Sort``#include ``using` `namespace` `std;`` ` `// A function to sort the algorithm using Odd Even sort``void` `oddEvenSort(``int` `arr[], ``int` `n)``{``    ``bool` `isSorted = ``false``; ``// Initially array is unsorted`` ` `    ``while` `(!isSorted) {``        ``isSorted = ``true``;`` ` `        ``// Perform Bubble sort on odd indexed element``        ``for` `(``int` `i = 1; i <= n - 2; i = i + 2) {``            ``if` `(arr[i] > arr[i + 1]) {``                ``swap(arr[i], arr[i + 1]);``                ``isSorted = ``false``;``            ``}``        ``}`` ` `        ``// Perform Bubble sort on even indexed element``        ``for` `(``int` `i = 0; i <= n - 2; i = i + 2) {``            ``if` `(arr[i] > arr[i + 1]) {``                ``swap(arr[i], arr[i + 1]);``                ``isSorted = ``false``;``            ``}``        ``}``    ``}`` ` `    ``return``;``}`` ` `// A utility function ot print an array of size n``void` `printArray(``int` `arr[], ``int` `n)``{``    ``for` `(``int` `i = 0; i < n; i++)``        ``cout << arr[i] << ``" "``;``    ``cout << ``"\n"``;``}`` ` `// Driver program to test above functions.``int` `main()``{``    ``int` `arr[] = { 34, 2, 10, -9 };``    ``int` `n = ``sizeof``(arr) / ``sizeof``(arr);`` ` `    ``oddEvenSort(arr, n);``    ``printArray(arr, n);`` ` `    ``return` `(0);``}`
Output:
```-9 2 10 34
```

Please refer complete article on Odd-Even Sort / Brick Sort for more details!

Want to learn from the best curated videos and practice problems, check out the C Foundation Course for Basic to Advanced C.

My Personal Notes arrow_drop_up