# Sort a Bitonic Array

Given a bitonic array arr[] the task is to sort the given bitonic array.

A Bitonic Sequence is a sequence of numbers that is first strictly increasing then after a point strictly decreasing.

Examples:

Input: arr[] = {5, 10, 15, 25, 20, 3, 2, 1}
Output: 1 2 3 5 10 15 20 25

Input: arr[] = {5, 20, 30, 40, 36, 33, 25, 15, 10}
Output: 5 10 15 20 25 30 33 36 40

Approach:

• The idea is to initialize a variable K to the highest power of 2 in size of the array such as to compare elements that are K distant apart.
• Swap the elements if they are not in increasing order.
• Reduce K by half and repeat the process until K becomes zero.

Below is the implementation of the above approach:

## C++

 `// C++ program for the above approach ` `#include ` `using` `namespace` `std; ` ` `  `// Function to Sort a Bitonic array ` `// in constant space ` `void` `sortArr(``int` `a[], ``int` `n) ` `{ ` `    ``int` `i, k; ` ` `  `    ``// Initialse the value of k ` `    ``k = (``int``)log2(n); ` `    ``k = ``pow``(2, k); ` ` `  `    ``// In each iteration compare elements ` `    ``// k distance apart and swap if ` `    ``// they are not in order ` `    ``while` `(k > 0) { ` `        ``for` `(i = 0; i + k < n; i++) ` `            ``if` `(a[i] > a[i + k]) ` `                ``swap(a[i], a[i + k]); ` ` `  `        ``// k is reduced to half ` `        ``// after every iteration ` `        ``k = k / 2; ` `    ``} ` ` `  `    ``// Print the array elements ` `    ``for` `(i = 0; i < n; i++) { ` `        ``cout << a[i] << ``" "``; ` `    ``} ` `} ` ` `  `// Driver Code ` `int` `main() ` `{ ` `    ``// Given array arr[] ` `    ``int` `arr[] = { 5, 20, 30, 40, 36, 33, 25, 15, 10 }; ` `    ``int` `n = ``sizeof``(arr) / ``sizeof``(arr[0]); ` ` `  `    ``// Function Call ` `    ``sortArr(arr, n); ` ` `  `    ``return` `0; ` `} `

## Java

 `// Java program for the above approach  ` `import` `java.io.*; ` ` `  `class` `GFG{ ` `     `  `// Function to Sort a Bitonic array  ` `// in constant space  ` `static` `void` `sortArr(``int` `a[], ``int` `n)  ` `{  ` `    ``int` `i, k;  ` ` `  `    ``// Initialse the value of k  ` `    ``k = (``int``)(Math.log(n) / Math.log(``2``));  ` `    ``k = (``int``) Math.pow(``2``, k);  ` ` `  `    ``// In each iteration compare elements  ` `    ``// k distance apart and swap if  ` `    ``// they are not in order  ` `    ``while` `(k > ``0``)  ` `    ``{  ` `        ``for``(i = ``0``; i + k < n; i++)  ` `            ``if` `(a[i] > a[i + k]) ` `            ``{ ` `                ``int` `tmp = a[i]; ` `                ``a[i] = a[i + k]; ` `                ``a[i + k] = tmp; ` `            ``} ` ` `  `        ``// k is reduced to half  ` `        ``// after every iteration  ` `        ``k = k / ``2``;  ` `    ``}  ` ` `  `    ``// Print the array elements  ` `    ``for``(i = ``0``; i < n; i++) ` `    ``{  ` `        ``System.out.print(a[i] + ``" "``);  ` `    ``}  ` `} ` `     `  `// Driver code ` `public` `static` `void` `main (String[] args)  ` `{ ` `     `  `    ``// Given array arr[]  ` `    ``int` `arr[] = { ``5``, ``20``, ``30``, ``40``, ``36``,  ` `                  ``33``, ``25``, ``15``, ``10` `};  ` `    ``int` `n = arr.length;  ` `     `  `    ``// Function call  ` `    ``sortArr(arr, n);  ` `} ` `} ` ` `  `// This code is contributed by offbeat`

## Python3

 `# Python3 program for the ` `# above approach ` `import` `math ` ` `  `# Function to sort bitonic ` `# array in constant space ` `def` `sortArr(a, n): ` ` `  `    ``# Initialize thevalue of k ` `    ``k ``=` `int``(math.log(n, ``2``)) ` `    ``k ``=` `int``(``pow``(``2``, k)) ` ` `  `    ``# In each iteration compare elements ` `    ``# k distance apart and swap it ` `    ``# they are not in order ` `    ``while``(k > ``0``): ` `        ``i ``=` `0` `        ``while` `i ``+` `k < n: ` `            ``if` `a[i] > a[i ``+` `k]: ` `                ``a[i], a[i ``+` `k] ``=` `a[i ``+` `k], a[i] ` `            ``i ``=` `i ``+` `1` `             `  `        ``# k is reduced to half after ` `        ``# every iteration ` `        ``k ``=` `k ``/``/` `2` `     `  `    ``# Print the array elements      ` `    ``for` `i ``in` `range``(n): ` `        ``print``(a[i], end ``=` `" "``) ` ` `  `# Driver code ` ` `  `# Given array ` `a ``=` `[ ``5``, ``20``, ``30``, ``40``, ``36``, ``33``, ``25``, ``15``, ``10` `] ` `n ``=` `len``(a) ` ` `  `# Function call ` `sortArr(a, n) ` ` `  `# This code is contributed by virusbuddah_ `

## C#

 `// C# program for the above approach  ` `using` `System; ` ` `  `class` `GFG{ ` `     `  `// Function to Sort a Bitonic array  ` `// in constant space  ` `static` `void` `sortArr(``int` `[]a, ``int` `n)  ` `{  ` `    ``int` `i, k;  ` ` `  `    ``// Initialse the value of k  ` `    ``k = (``int``)(Math.Log(n) / Math.Log(2));  ` `    ``k = (``int``) Math.Pow(2, k);  ` ` `  `    ``// In each iteration compare elements  ` `    ``// k distance apart and swap if  ` `    ``// they are not in order  ` `    ``while` `(k > 0)  ` `    ``{  ` `        ``for``(i = 0; i + k < n; i++)  ` `            ``if` `(a[i] > a[i + k]) ` `            ``{ ` `                ``int` `tmp = a[i]; ` `                ``a[i] = a[i + k]; ` `                ``a[i + k] = tmp; ` `            ``} ` ` `  `        ``// k is reduced to half  ` `        ``// after every iteration  ` `        ``k = k / 2;  ` `    ``}  ` ` `  `    ``// Print the array elements  ` `    ``for``(i = 0; i < n; i++) ` `    ``{  ` `        ``Console.Write(a[i] + ``" "``);  ` `    ``}  ` `} ` `     `  `// Driver code ` `public` `static` `void` `Main(String[] args)  ` `{ ` `     `  `    ``// Given array []arr  ` `    ``int` `[]arr = { 5, 20, 30, 40, 36,  ` `                  ``33, 25, 15, 10 };  ` `    ``int` `n = arr.Length;  ` `     `  `    ``// Function call  ` `    ``sortArr(arr, n);  ` `} ` `} ` ` `  `// This code is contributed by amal kumar choubey  `

Output:

```5 10 15 20 25 30 33 36 40
```

Time Complexity: O(N*log N)
Auxiliary Space: O(1)

Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the DSA Self Paced Course at a student-friendly price and become industry ready.

My Personal Notes arrow_drop_up

Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.

Article Tags :
Practice Tags :

2

Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.