Sort the given Array in Spiral manner starting from the center
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 <bits/stdc++.h> 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
<script> // JavaScript program for the above approach // Function to sort the array // according to the given pattern function centerSpiralSort(arr, N) { // Initializing the variables let left = 0; let right = N - 1; let 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] let 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] let 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 (let i = 0; i < N / 2; i++) { let temp = arr[N - 1 - i]; arr[N - 1 - i] = arr[i]; arr[i] = temp; } } // Print the elements of the array for (let i = 0; i < N; i++) { document.write(arr[i] + " " ); } } // Driver Code // Given array let arr = [4, 9, 3, 5, 7]; // Size of the array let N = arr.length; // Function Call centerSpiralSort(arr, N); </script> |
3 5 9 7 4
Time Complexity: O(N2)
Auxiliary Space: O(1)
Please Login to comment...