## This problem is based on pancake sorting.

## Given an unsorted array, we need to write a function which uses only flip operation to sort the array. flip is the operation which reverses the array.

We need to write a algorithm which as time complexity of O(nlogn), if we use pancake sorting algorithm the time taken will will be O() beacause in a loop we will find maximum element which takes O(n) time.

To reduce the time complexity we will use insertion sort, binary search and flip operation.

The main idea is to find the smallest element ie, arr[j] which is greater than arr[i] in arr[0,..i-1] using binary search. Now, we need to insert arr[i] just before arr[j] by using flip opoerations.

**Time Complexity: O(nlogn)**

## Algorithm

### Till the end of the array

**1.** Find the index of smallest element which is greater than arr[i] in arr[0..i-1] ie, j = ceilSearch(arr, 0, i-1, arr[i]), Here 0 is the starting index, (i-1) is the ending index

**Â Â Â Â Â Â Â ceilSearch Algorithm:**

The smallest number which is greater than the given number is known as the ceiling of that number

**Â Â Â Â Â Â Â a. **If the given element(arr[i]) is less than or equal to the first element in the array of given size, then return the index of the first element

**Â Â Â Â Â Â Â b.** If the element is greater than last element, then return -1 ie, there is no greater element

**Â Â Â Â Â Â Â c.** If the element is same as the mid element, then return the mid index

**Â Â Â Â Â Â Â d.** If the element is greater than mid element, then either the next element to the mid element is the ceiling of the arr[i], orÂ ceiling lies inÂ arr[mid+1….high]

**Â Â Â Â Â Â Â e.** If the element is less than mid element, then either the previous element to the mid Â Â Â element is the ceiling of the arr[i], or ceiling lies in arr[low….mid-1]

**2.** Now, we need to insert the element arr[i] before arr[j] using flip operations

**Â Â Â Â a.** flip the elements from index 0 to index j-1

**Â Â Â Â b.** flip the elements from index 0 to index i-1

**Â Â Â Â c.** flip the elements from index 0 to index i

**Â Â Â Â d.** flip the elements from index 0 to index j

**3.** Print the sorted array.

The above algorithm can be clearly explained in the below diagram

## C++ Program

```
#include <bits/stdc++.h>
using namespace std;
int flip(int arr[], int m) // Flipping the array till the index m
{
int t;
for(int i = 0; i< m; ++i)
{
t = arr[i];
arr[i] = arr[m];
arr[m] = t;
m--;
}
}
//It is a binary search based function
//The array passed to ceilSearch will be sorted
//This function will return the smallest element index
//which is greater than x
int ceilSearch(int arr[], int low, int high, int x)
{
int mid = (low+high)/2;
if (x<=arr[low])//if x is less than the first element
{
return low;
}
if (x>arr[high])//if there is no element higher than x
{
return -1;
}
if (arr[mid]==x)//if x is the mid element
{
return mid;
}
if (arr[mid]<x) //if x is greater than mid element
{
if (mid +1 <= high && x <= arr[mid +1])
{
return mid +1;
}
else
{
return ceilSearch(arr, mid+1, high, x);
}
}
if (mid-1 >= low && x > arr[mid-1]) //if x is less than mid element
{
return mid;
}
else
{
return ceilSearch(arr, low, mid -1, x);
}
}
void pancakeSort(int arr[], int n)
{
//start from the second element and insert arr[i]
//in already sorted array arr[i-1] ie, similar to insertion sort
for (int i = 1; i < n; ++i)
{
int j = ceilSearch(arr, 0, i-1, arr[i]);
//If there is a greater element
if (j != -1)
{
//Using flip operations insert arr[i] before arr[j]
flip(arr, j-1);
flip(arr, i-1);
flip(arr, i);
flip(arr, j);
}
}
//Printing the sorted array
cout<<"The sorted array is [ ";
for (int i = 0; i < n; ++i)
{
cout<<arr[i]<<" ";
}
cout<<"]"<<endl;
}
int main()
{
int arr[]= {4, 1, 10, 23, 5}; //creating an array
int n = sizeof(arr)/sizeof(arr[0]); //size of the array
pancakeSort(arr,n);
return 0;
}
```