
- C Programming Tutorial
- C - Home
- C - Overview
- C - Environment Setup
- C - Program Structure
- C - Basic Syntax
- C - Data Types
- C - Variables
- C - Constants
- C - Storage Classes
- C - Operators
- C - Decision Making
- C - Loops
- C - Functions
- C - Scope Rules
- C - Arrays
- C - Pointers
- C - Strings
- C - Structures
- C - Unions
- C - Bit Fields
- C - Typedef
- C - Input & Output
- C - File I/O
- C - Preprocessors
- C - Header Files
- C - Type Casting
- C - Error Handling
- C - Recursion
- C - Variable Arguments
- C - Memory Management
- C - Command Line Arguments
- C Programming useful Resources
- C - Questions & Answers
- C - Quick Guide
- C - Useful Resources
- C - Discussion
C/C++ Program for Odd-Even Sort (Brick Sort)?
Here we will see how the brick sort works. The Brick sort is one modification of bubble sort. This algorithm is divided into two parts. These parts are odd part and even parts. In the odd part we will use the bubble sort on odd indexed items, and in the even part we will use the bubble sort on even indexed elements. Let us see the algorithm to get the idea.
Algorithm
brickSort(arr, n)
begin flag := false while the flag is not true, do flag := true for i := 1 to n-2, increase i by 2, do if arr[i] > arr[i+1], then exchange arr[i] and arr[i+1] flag := false end if done for i := 0 to n-2, increase i by 2, do if arr[i] > arr[i+1], then exchange arr[i] and arr[i+1] flag := false end if done done end
Example
#include<iostream> using namespace std; void brickSort(int arr[], int n){ bool flag = false; while(!flag){ flag = true; for(int i = 1; i<n-1; i= i+2){ if(arr[i] > arr[i+1]){ swap(arr[i], arr[i+1]); flag = false; } } for(int i = 0; i<n-1; i= i+2){ if(arr[i] > arr[i+1]){ swap(arr[i], arr[i+1]); flag = false; } } } } main() { int data[] = {54, 74, 98, 154, 98, 32, 20, 13, 35, 40}; int n = sizeof(data)/sizeof(data[0]); cout << "Sorted Sequence "; brickSort(data, n); for(int i = 0; i <n;i++){ cout << data[i] << " "; } }
Output
Sorted Sequence 13 20 32 35 40 54 74 98 98 154
- Related Articles
- C/C++ Program for the Odd-Even Sort (Brick Sort)?
- Python Program for Odd-Even Sort / Brick Sort
- Odd even sort in an array - JavaScript
- C++ Program for Cycle Sort?
- C++ Program for Cocktail Sort?
- C++ Program for Comb Sort?
- C++ Program for Gnome Sort?
- C++ Program for Pigeonhole Sort?
- C Program for Selection Sort?
- C Program for Radix Sort
- C++ Program for Recursive Bubble Sort?
- C++ Program for the Cocktail Sort?
- C++ Program for the Comb Sort?
- C++ Program for the Gnome Sort?
- C Program for Iterative Merge Sort

Advertisements