# 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++

 `// C++ program for the above approach` `#include ``using` `namespace` `std;` `// Function to sort the array``// according to the given pattern``void` `centerSpiralSort(``int` `arr[], ``int` `N)``{``    ``// Initializing the variables``    ``int` `left = 0;``    ``int` `right = N - 1;``    ``int` `i = 1;` `    ``while` `(left < right) {` `        ``// Performing bubble sort``        ``for` `(i = left + 1; i <= right;``             ``i++) {``            ``if` `(arr[left] > arr[i]) {` `                ``// Swapping if``                ``// arr[left] > arr[i]``                ``swap(arr[left], arr[i]);``            ``}``        ``}` `        ``// Increment left by 1``        ``left++;` `        ``// Performing bubble sort``        ``for` `(i = right - 1; i >= left;``             ``i--) {``            ``if` `(arr[right] > arr[i]) {` `                ``// Swapping if``                ``// arr[right] > arr[i]``                ``swap(arr[right], arr[i]);``            ``}``        ``}` `        ``// Decrement right by 1``        ``right--;``    ``}` `    ``// If N is an even number``    ``// Reversing the array from 0 to N/2-1``    ``if` `(N % 2 == 0) {` `        ``// Reversing the half array``        ``for` `(``int` `i = 0; i < N / 2; i++) {``            ``swap(arr[i], arr[N - 1 - i]);``        ``}``    ``}` `    ``// Print the elements of the array``    ``for` `(``int` `i = 0; i < N; i++) {``        ``cout << arr[i] << ``" "``;``    ``}``}` `// Driver Code``int` `main()``{``    ``// Given array``    ``int` `arr[] = { 4, 9, 3, 5, 7 };` `    ``// Size of the array``    ``int` `N = ``sizeof` `arr / ``sizeof` `arr[0];` `    ``// Function Call``    ``centerSpiralSort(arr, N);` `    ``return` `0;``}`

## Java

 `// Java program for the above approach``import` `java.io.*;``class` `GFG``{``  ` `    ``// Function to sort the array``    ``// according to the given pattern``    ``static` `void` `centerSpiralSort(``int` `arr[], ``int` `N)``    ``{``      ` `        ``// Initializing the variables``        ``int` `left = ``0``;``        ``int` `right = N - ``1``;``        ``int` `i = ``1``;``        ``int` `temp;` `        ``while` `(left < right) {` `            ``// Performing bubble sort``            ``for` `(i = left + ``1``; i <= right; i++) {``                ``if` `(arr[left] > arr[i]) {` `                    ``// Swapping if``                    ``// arr[left] > arr[i]``                    ``temp = arr[left];``                    ``arr[left] = arr[i];``                    ``arr[i] = temp;``                ``}``            ``}` `            ``// Increment left by 1``            ``left++;` `            ``// Performing bubble sort``            ``for` `(i = right - ``1``; i >= left; i--) {``                ``if` `(arr[right] > arr[i]) {` `                    ``// Swapping if``                    ``// arr[right] > arr[i]``                    ``temp = arr[right];``                    ``arr[right] = arr[i];``                    ``arr[i] = temp;``                ``}``            ``}` `            ``// Decrement right by 1``            ``right--;``        ``}` `        ``// If N is an even number``        ``// Reversing the array from 0 to N/2-1``        ``if` `(N % ``2` `== ``0``) {` `            ``// Reversing the half array``            ``for` `(i = ``0``; i < N / ``2``; i++) {``                ``temp = arr[i];``                ``arr[i] = arr[N - ``1` `- i];``                ``arr[N - ``1` `- i] = temp;``            ``}``        ``}` `        ``// Print the elements of the array``        ``for` `(i = ``0``; i < N; i++) {``            ``System.out.print(arr[i] + ``" "``);``        ``}``    ``}` `    ``// Driver Code``    ``public` `static` `void` `main(String[] args)``    ``{``      ` `        ``// Given array``        ``int` `arr[] = { ``4``, ``9``, ``3``, ``5``, ``7` `};` `        ``// Size of the array``        ``int` `N = arr.length;` `        ``// Function Call``        ``centerSpiralSort(arr, N);``    ``}``}` `// This code is contributed by Potta Lokesh`

## Python3

 `# Python3 program for the above approach` `# Function to sort the array``# according to the given pattern``def` `centerSpiralSort(arr, N):``    ` `    ``# Initializing the variables``    ``left ``=` `0``    ``right ``=` `N ``-` `1``    ``i ``=` `1` `    ``while` `(left < right):``        ` `        ``# Performing bubble sort``        ``for` `i ``in` `range``(left ``+` `1``, right ``+` `1``, ``1``):``            ``if` `(arr[left] > arr[i]):``                ` `                ``# Swapping if``                ``# arr[left] > arr[i]``                ``temp ``=` `arr[left]``                ``arr[left] ``=` `arr[i]``                ``arr[i] ``=` `temp` `        ``# Increment left by 1``        ``left ``+``=` `1` `        ``# Performing bubble sort``        ``i ``=` `right ``-` `1``        ` `        ``while``(i >``=` `left):``            ``if` `(arr[right] > arr[i]):``                ` `                ``# Swapping if``                ``# arr[right] > arr[i]``                ``temp ``=` `arr[right]``                ``arr[right] ``=` `arr[i]``                ``arr[i] ``=` `temp``                ` `            ``i ``-``=` `1` `        ``# Decrement right by 1``        ``right ``-``=` `1` `    ``# If N is an even number``    ``# Reversing the array from 0 to N/2-1``    ``if` `(N ``%` `2` `=``=` `0``):``        ` `        ``# Reversing the half array``        ``for` `i ``in` `range``(N ``/` `2``):``            ``temp ``=` `arr[i]``            ``arr[i] ``=` `arr[N ``-` `1` `-` `i]``            ``arr[N ``-` `1` `-` `i] ``=` `temp` `    ``# Print the elements of the array``    ``for` `i ``in` `range``(N):``        ``print``(arr[i], end ``=` `" "``)` `# Driver Code``if` `__name__ ``=``=` `'__main__'``:``    ` `    ``# Given array``    ``arr ``=` `[ ``4``, ``9``, ``3``, ``5``, ``7` `]` `    ``# Size of the array``    ``N ``=` `len``(arr)` `    ``# Function Call``    ``centerSpiralSort(arr, N)``    ` `# This code is contributed by ipg2016107`

## C#

 `// C# program for the above approach``using` `System;` `class` `GFG{` `// Function to sort the array``// according to the given pattern``static` `void` `centerSpiralSort(``int` `[]arr, ``int` `N)``{``    ` `    ``// Initializing the variables``    ``int` `left = 0;``    ``int` `right = N - 1;``    ``int` `i = 1;``    ``int` `temp;` `    ``while` `(left < right)``    ``{``        ` `        ``// Performing bubble sort``        ``for``(i = left + 1; i <= right; i++)``        ``{``            ``if` `(arr[left] > arr[i])``            ``{``                ` `                ``// Swapping if``                ``// arr[left] > arr[i]``                ``temp = arr[left];``                ``arr[left] = arr[i];``                ``arr[i] = temp;``            ``}``        ``}` `        ``// Increment left by 1``        ``left++;` `        ``// Performing bubble sort``        ``for``(i = right - 1; i >= left; i--)``        ``{``            ``if` `(arr[right] > arr[i])``            ``{``                ` `                ``// Swapping if``                ``// arr[right] > arr[i]``                ``temp = arr[right];``                ``arr[right] = arr[i];``                ``arr[i] = temp;``            ``}``        ``}` `        ``// Decrement right by 1``        ``right--;``    ``}` `    ``// If N is an even number``    ``// Reversing the array from 0 to N/2-1``    ``if` `(N % 2 == 0)``    ``{``        ` `        ``// Reversing the half array``        ``for``(i = 0; i < N / 2; i++)``        ``{``            ``temp = arr[i];``            ``arr[i] = arr[N - 1 - i];``            ``arr[N - 1 - i] = temp;``        ``}``    ``}` `    ``// Print the elements of the array``    ``for``(i = 0; i < N; i++)``    ``{``        ``Console.Write(arr[i] + ``" "``);``    ``}``}` `// Driver Code``public` `static` `void` `Main(String[] args)``{``    ` `    ``// Given array``    ``int` `[]arr = { 4, 9, 3, 5, 7 };` `    ``// Size of the array``    ``int` `N = arr.Length;` `    ``// Function Call``    ``centerSpiralSort(arr, N);``}``}` `// This code is contributed by shivanisinghss2110`

## Javascript

 ``

Output

`3 5 9 7 4 `

Time Complexity: O(N2)
Auxiliary Space: O(1)

My Personal Notes arrow_drop_up
{"found_math": false, "script_math_tex": 0, "script_math_asciimath": 0, "math_annotations": 0, "math_alttext": 0, "mathml": 0, "mathjax_tag": 0, "mathjax_inline_tex": 0, "mathjax_display_tex": 0, "mathjax_asciimath": 0, "img_math": 0, "codecogs_latex": 0, "wp_latex": 0, "mimetex.cgi": 0, "/images/math/codecogs": 0, "mathtex.cgi": 0, "katex": 0, "math-container": 0, "wp-katex-eq": 0, "align": 0, "equation": 0, "x-ck12": 0, "texerror": 0, "url": "https://www.geeksforgeeks.org/sort-the-given-array-in-spiral-manner-starting-from-the-center/?ref=rp", "fetch_time": 1679926817000000000, "content_mime_type": "text/html", "warc_filename": "crawl-data/CC-MAIN-2023-14/segments/1679296948632.20/warc/CC-MAIN-20230327123514-20230327153514-00403.warc.gz", "warc_record_offset": 881315926, "warc_record_length": 32373, "token_count": 3661, "char_count": 10942, "score": 3.78125, "int_score": 4, "crawl": "CC-MAIN-2023-14", "snapshot_type": "latest", "language": "en", "language_score": 0.9072138667106628, "file_path": "/sailhome/yjruan/project/latent-lm/data/processed/finemath-4plus/finemath-4plus/train-00002-of-00064.parquet"}