从中心开始以螺旋方式对给定的数组进行排序
给定一个大小为N的数组arr[] ,任务是从数组的中间开始按降序对数组进行排序,第二大元素在中间元素的右侧,第二大元素在中间元素的左侧等等。
例子:
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.
方法:这个想法是使用冒泡排序并从最小元素开始对数组进行排序。首先,将最小的元素放在最左边的索引中,然后将下一个最小的元素放在最右边的索引中。以下是步骤:
- 初始化变量,比如left = 0 、 right = N – 1和i = 1 。
- 从左边的位置开始,即从left = 0开始,然后在数组的剩余部分执行冒泡排序,找到最小的元素并将其放在最左边的位置,直到i == right 。
- 将 left 的值增加1 ,因为它的元素已经放置,不需要更改。
- 从正确的位置开始,再次反向执行冒泡排序,找到最小的元素并将其放置在正确的位置,直到i == left 。
- 将 right 的值减1 ,因为它的元素已经放置,不需要更改。
- 如果N是偶数,则最小元素应位于数组的最右端,但在我们的方法中,最小元素位于最左端。因此,为了实现所需的模式,请反转数组的前半部分。
- 打印修改后的排序数组。
下面是上述方法的实现:
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
3 5 9 7 4
时间复杂度: O(N 2 )
辅助空间: O(1)