Sort a Bitonic Array
Last Updated :
21 Nov, 2022
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++
#include <bits/stdc++.h>
using namespace std;
void sortArr( int a[], int n)
{
int i, k;
k = ( int )log2(n);
k = pow (2, k);
while (k > 0) {
for (i = 0; i + k < n; i++)
if (a[i] > a[i + k])
swap(a[i], a[i + k]);
k = k / 2;
}
for (i = 0; i < n; i++) {
cout << a[i] << " " ;
}
}
int main()
{
int arr[] = { 5, 20, 30, 40, 36, 33, 25, 15, 10 };
int n = sizeof (arr) / sizeof (arr[0]);
sortArr(arr, n);
return 0;
}
|
Java
import java.io.*;
class GFG{
static void sortArr( int a[], int n)
{
int i, k;
k = ( int )(Math.log(n) / Math.log( 2 ));
k = ( int ) Math.pow( 2 , k);
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 = k / 2 ;
}
for (i = 0 ; i < n; i++)
{
System.out.print(a[i] + " " );
}
}
public static void main (String[] args)
{
int arr[] = { 5 , 20 , 30 , 40 , 36 ,
33 , 25 , 15 , 10 };
int n = arr.length;
sortArr(arr, n);
}
}
|
Python3
import math
def sortArr(a, n):
k = int (math.log(n, 2 ))
k = int ( pow ( 2 , k))
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 = k / / 2
for i in range (n):
print (a[i], end = " " )
a = [ 5 , 20 , 30 , 40 , 36 , 33 , 25 , 15 , 10 ]
n = len (a)
sortArr(a, n)
|
C#
using System;
class GFG{
static void sortArr( int []a, int n)
{
int i, k;
k = ( int )(Math.Log(n) / Math.Log(2));
k = ( int ) Math.Pow(2, k);
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 = k / 2;
}
for (i = 0; i < n; i++)
{
Console.Write(a[i] + " " );
}
}
public static void Main(String[] args)
{
int []arr = { 5, 20, 30, 40, 36,
33, 25, 15, 10 };
int n = arr.Length;
sortArr(arr, n);
}
}
|
Javascript
<script>
function sortArr(a,n)
{
let i, k;
k = parseInt(Math.log(n) / Math.log(2));
k = parseInt( Math.pow(2, k));
while (k > 0)
{
for (i = 0; i + k < n; i++)
if (a[i] > a[i + k])
{
let tmp = a[i];
a[i] = a[i + k];
a[i + k] = tmp;
}
k = k / 2;
}
for (i = 0; i < n; i++)
{
document.write(a[i] + " " );
}
}
let arr = [ 5, 20, 30, 40, 36,
33, 25, 15, 10 ];
let n = arr.length;
sortArr(arr, n);
</script>
|
Output
5 10 15 20 25 30 33 36 40
Time Complexity: O(N*log N)
Auxiliary Space: O(1)
Efficient Approach : Using Binary search and Merge function of Merge sort.
- Find peak element index using binary search in given array.
- Divide the array into two parts first from index 0 to peak and second from index peak+1 to N-1
and traverse both the arrays in ascending order and do the following operations until any of the
arrays is exhausted :
(i) If element of first array is less than element of second array then store it into a tmp array
and increase the indexes of both the arrays (tmp and first).
(ii) Else store the element of second array into tmp array and increase the indexes of both
the arrays (tmp and second).
- Check both the arrays and the array which is not fully traversed, store the remaining elements into
tmp array.
- Copy all the elements of tmp array back in original array.
Below is the implementation of the above approach :
C++
#include<bits/stdc++.h>
using namespace std;
void sortArr(vector< int >&arr, int n){
vector< int >tmp(n, 0);
int peak = -1;
int low = 0;
int high = n - 1;
int k = 0;
while (low <= high){
int mid = (low + high) / 2;
if (( mid == 0 || arr[mid - 1] < arr[mid] ) &&
( mid == n - 1 || arr[mid + 1] < arr[mid] )){
peak = mid;
break ;
}
if (arr[mid] < arr[mid + 1])
low = mid + 1;
else
high = mid - 1;
}
low = 0;
high = n - 1;
while (low <= peak && high > peak){
if (arr[low] < arr[high]){
tmp[k++] = arr[low++];
}
else {
tmp[k++] = arr[high--];
}
}
while (low <= peak){
tmp[k++] = arr[low++];
}
while (high > peak){
tmp[k++] = arr[high++];
}
for ( int i = 0; i < n; i++)
arr[i] = tmp[i];
}
int main(){
vector< int >arr = { 5, 20, 30, 40, 36, 33, 25, 15, 10 };
int n = arr.size();
sortArr(arr, n);
for ( auto x : arr)
cout<<x<< " " ;
}
|
Java
import java.util.*;
public class GFG {
static void sortArr( int arr[], int n)
{
int tmp[] = new int [n];
int peak = - 1 , low = 0 , high = n- 1 , k= 0 ;
while (low <= high){
int mid = (low + high) / 2 ;
if (( mid == 0 || arr[mid - 1 ] < arr[mid] )
&& ( mid == n - 1 || arr[mid + 1 ] < arr[mid]
))
{
peak = mid;
break ;
}
if (arr[mid] < arr[mid + 1 ]) low = mid + 1 ;
else high = mid - 1 ;
}
low = 0 ;
high = n - 1 ;
while (low <= peak && high > peak){
if (arr[low] < arr[high]) tmp[k++] = arr[low++];
else tmp[k++] = arr[high--];
}
while (low <= peak) tmp[k++] = arr[low++];
while (high > peak) tmp[k++] = arr[high--];
for ( int i = 0 ; i < n; i++) arr[i] = tmp[i];
}
public static void main(String[] args)
{
int arr[] = { 5 , 20 , 30 , 40 , 36 , 33 , 25 , 15 , 10 };
int n = arr.length;
sortArr(arr, n);
for ( int x : arr) System.out.print(x + " " );
}
}
|
Python3
def sortArr(arr, n):
tmp = [ 0 for i in range (n)]
peak, low, high, k = - 1 , 0 , n - 1 , 0
while (low < = high):
mid = (low + high) / / 2
if (( mid = = 0 or arr[mid - 1 ] < arr[mid] ) and ( mid = = n - 1 or arr[mid + 1 ] < arr[mid] )):
peak = mid
break
if (arr[mid] < arr[mid + 1 ]):
low = mid + 1
else :
high = mid - 1
low,high = 0 ,n - 1
while (low < = peak and high > peak):
if (arr[low] < arr[high]):
tmp[k] = arr[low]
k + = 1
low + = 1
else :
tmp[k] = arr[high]
k + = 1
high - = 1
while (low < = peak):
tmp[k] = arr[low]
k + = 1
low + = 1
while (high > peak):
tmp[k] = arr[high]
k + = 1
high - = 1
for i in range (n):
arr[i] = tmp[i]
arr = [ 5 , 20 , 30 , 40 , 36 , 33 , 25 , 15 , 10 ]
n = len (arr)
sortArr(arr, n)
for x in arr:
print (x ,end = " " )
|
Javascript
<script>
function sortArr(arr, n){
let tmp = new Array(n).fill(0)
let peak = -1
let low = 0
let high = n - 1
let k = 0
while (low <= high){
let mid = Math.floor((low + high) / 2)
if (( mid == 0 || arr[mid - 1] < arr[mid] ) && ( mid == n - 1 || arr[mid + 1] < arr[mid] )){
peak = mid
break
}
if (arr[mid] < arr[mid + 1])
low = mid + 1
else
high = mid - 1
}
low = 0
high = n - 1
while (low <= peak && high > peak){
if (arr[low] < arr[high]){
tmp[k++] = arr[low++]
}
else {
tmp[k++] = arr[high--]
}
}
while (low <= peak){
tmp[k++] = arr[low++]
}
while (high > peak){
tmp[k++] = arr[high++]
}
for (let i = 0; i < n; i++)
arr[i] = tmp[i]
}
let arr = [ 5, 20, 30, 40, 36, 33, 25, 15, 10 ]
let n = arr.length
sortArr(arr, n)
for (let x of arr)
document.write(x , " " )
</script>
|
C#
using System;
public class GFG
{
public static void sortArr( int [] arr, int n)
{
int [] tmp = new int [n];
var peak = -1;
var low = 0;
var high = n - 1;
var k = 0;
while (low <= high)
{
var mid = ( int )((low + high) / 2);
if ((mid == 0 || arr[mid - 1] < arr[mid]) && (mid == n - 1 || arr[mid + 1] < arr[mid]))
{
peak = mid;
break ;
}
if (arr[mid] < arr[mid + 1])
{
low = mid + 1;
}
else
{
high = mid - 1;
}
}
low = 0;
high = n - 1;
while (low <= peak && high > peak)
{
if (arr[low] < arr[high])
{
tmp[k++] = arr[low++];
}
else
{
tmp[k++] = arr[high--];
}
}
while (low <= peak)
{tmp[k++] = arr[low++];
}
while (high > peak)
{tmp[k++] = arr[high--];
}
for ( int i = 0; i < n; i++)
{
arr[i] = tmp[i];
}
}
public static void Main(String[] args)
{
int [] arr = {5, 20, 30, 40, 36, 33, 25, 15, 10};
var n = arr.Length;
sortArr(arr, n);
foreach ( int x in arr)
{ Console.Write(x.ToString() + " " );
}
}
}
|
Output
5 10 15 20 25 30 33 36 40
Time Complexity : O(N)
Auxiliary Space : O(N) (can be done in O(1) also)
Share your thoughts in the comments
Please Login to comment...