Sort the given Array in Spiral manner starting from the center
Last Updated :
01 Sep, 2021
Given an array arr[] of size N, the task is to sort the array in descending order starting from the mid of the array having the next largest element at the right of the middle element and the next largest at the left of the middle element and so on.
Examples:
Input: arr[] = {4, 9, 3, 5, 7}
Output: 3 5 9 7 4
Explanation:
The largest element in the array is 9 is kept in the middle of the array at index = 2.
The next largest element is 7 is placed to the right of the middle element at index = 3.
The 3rd largest element is 5 is placed to the left of the middle element at index = 1.
The 4th largest element is 4 is placed to the right of the element is 7 at index = 4.
The smallest element is 3 is placed to left of the element is 5 at index = 0.
Input: arr[] = {4, 5, 3, 7, 6, 9, 7}
Output: 3 5 7 9 7 6 4
Explanation:
The largest element in the array is 9 is kept in the middle of the array at index = 3.
The next largest element is 7 is placed to the right of the middle element at index = 3.
The 3rd largest element is 7 is placed to the left of the middle element at index = 2.
The 4th largest element is 6 is placed to the right of the element 7 at index = 5.
The 5th largest element is 5 is placed to the left of the element 7 at index = 1.
The 6th largest element is 4 is placed at the right of the element 6 at index = 6.
The smallest element is 3 is placed to the left of the element 5 at index = 0.
Approach: The idea is to use bubble sort and to start sorting the array from the smallest element. First, place the smallest element in the left-most index and then, place the next smallest element in the rightmost index. Below are the steps:
- Initialize variables, say left = 0, right = N – 1 and i = 1.
- Start at the left position i.e. from left = 0 then perform bubble sort in the remaining part of the array finding the smallest element and placing it at the left-most position until i == right.
- Increase the value of left by 1 since its element is already placed, and doesn’t need to be changed.
- Start at the right position and perform bubble sort traversing in reverse direction again finding the smallest element and placing it in the position right until i == left.
- Decrement the value of right by 1 since its element is already placed, and doesn’t need to be changed.
- If N is even, the smallest element should come at the right extreme of the array but in our approach, the smallest element is placed at the left-most extreme. So, for achieving the required pattern, reverse the first half of the array.
- Print the modified sorted array.
Below is the implementation of the above approach:
C++
#include <bits/stdc++.h>
using namespace std;
void centerSpiralSort( int arr[], int N)
{
int left = 0;
int right = N - 1;
int i = 1;
while (left < right) {
for (i = left + 1; i <= right;
i++) {
if (arr[left] > arr[i]) {
swap(arr[left], arr[i]);
}
}
left++;
for (i = right - 1; i >= left;
i--) {
if (arr[right] > arr[i]) {
swap(arr[right], arr[i]);
}
}
right--;
}
if (N % 2 == 0) {
for ( int i = 0; i < N / 2; i++) {
swap(arr[i], arr[N - 1 - i]);
}
}
for ( int i = 0; i < N; i++) {
cout << arr[i] << " " ;
}
}
int main()
{
int arr[] = { 4, 9, 3, 5, 7 };
int N = sizeof arr / sizeof arr[0];
centerSpiralSort(arr, N);
return 0;
}
|
Java
import java.io.*;
class GFG
{
static void centerSpiralSort( int arr[], int N)
{
int left = 0 ;
int right = N - 1 ;
int i = 1 ;
int temp;
while (left < right) {
for (i = left + 1 ; i <= right; i++) {
if (arr[left] > arr[i]) {
temp = arr[left];
arr[left] = arr[i];
arr[i] = temp;
}
}
left++;
for (i = right - 1 ; i >= left; i--) {
if (arr[right] > arr[i]) {
temp = arr[right];
arr[right] = arr[i];
arr[i] = temp;
}
}
right--;
}
if (N % 2 == 0 ) {
for (i = 0 ; i < N / 2 ; i++) {
temp = arr[i];
arr[i] = arr[N - 1 - i];
arr[N - 1 - i] = temp;
}
}
for (i = 0 ; i < N; i++) {
System.out.print(arr[i] + " " );
}
}
public static void main(String[] args)
{
int arr[] = { 4 , 9 , 3 , 5 , 7 };
int N = arr.length;
centerSpiralSort(arr, N);
}
}
|
Python3
def centerSpiralSort(arr, N):
left = 0
right = N - 1
i = 1
while (left < right):
for i in range (left + 1 , right + 1 , 1 ):
if (arr[left] > arr[i]):
temp = arr[left]
arr[left] = arr[i]
arr[i] = temp
left + = 1
i = right - 1
while (i > = left):
if (arr[right] > arr[i]):
temp = arr[right]
arr[right] = arr[i]
arr[i] = temp
i - = 1
right - = 1
if (N % 2 = = 0 ):
for i in range (N / 2 ):
temp = arr[i]
arr[i] = arr[N - 1 - i]
arr[N - 1 - i] = temp
for i in range (N):
print (arr[i], end = " " )
if __name__ = = '__main__' :
arr = [ 4 , 9 , 3 , 5 , 7 ]
N = len (arr)
centerSpiralSort(arr, N)
|
C#
using System;
class GFG{
static void centerSpiralSort( int []arr, int N)
{
int left = 0;
int right = N - 1;
int i = 1;
int temp;
while (left < right)
{
for (i = left + 1; i <= right; i++)
{
if (arr[left] > arr[i])
{
temp = arr[left];
arr[left] = arr[i];
arr[i] = temp;
}
}
left++;
for (i = right - 1; i >= left; i--)
{
if (arr[right] > arr[i])
{
temp = arr[right];
arr[right] = arr[i];
arr[i] = temp;
}
}
right--;
}
if (N % 2 == 0)
{
for (i = 0; i < N / 2; i++)
{
temp = arr[i];
arr[i] = arr[N - 1 - i];
arr[N - 1 - i] = temp;
}
}
for (i = 0; i < N; i++)
{
Console.Write(arr[i] + " " );
}
}
public static void Main(String[] args)
{
int []arr = { 4, 9, 3, 5, 7 };
int N = arr.Length;
centerSpiralSort(arr, N);
}
}
|
Javascript
<script>
function centerSpiralSort(arr, N) {
let left = 0;
let right = N - 1;
let i = 1;
while (left < right) {
for (i = left + 1; i <= right; i++) {
if (arr[left] > arr[i]) {
let temp = arr[left];
arr[left] = arr[i];
arr[i] = temp;
}
}
left++;
for (i = right - 1; i >= left; i--) {
if (arr[right] > arr[i]) {
let temp = arr[right];
arr[right] = arr[i];
arr[i] = temp;
}
}
right--;
}
if (N % 2 == 0) {
for (let i = 0; i < N / 2; i++) {
let temp = arr[N - 1 - i];
arr[N - 1 - i] = arr[i];
arr[i] = temp;
}
}
for (let i = 0; i < N; i++) {
document.write(arr[i] + " " );
}
}
let arr = [4, 9, 3, 5, 7];
let N = arr.length;
centerSpiralSort(arr, N);
</script>
|
Time Complexity: O(N2)
Auxiliary Space: O(1)
Share your thoughts in the comments
Please Login to comment...