给定N个整数的数组arr [] ,任务是重新排列数组元素,以使最小元素位于第0个位置,第二个最小元素位于第(N-1)个位置,第三个最小元素位于对于arr []中的所有整数,第1个位置,第4个最小元素在第(N-2)个位置,依此类推。
例子:
Input: arr[] = {10, 23, 12, 17, 9}
Output: 9 12 23 17 10
Explanation:
The smallest element is 9 which is put in the index 0.
Then the second smallest element is 10 which is put in the last position.
The third smallest element is put in the second position from the start.
The fourth smallest in the second position from the last and so on.
Input: arr[] = {1, 3, 3, 4, 5}
Output: 1 3 5 4 3
方法:我们将使用两指针技术。这个想法是从数组的变量i标记的开始迭代到变量j标记的结束,直到它们在中间相遇,并不断更新这些索引处的最小值。步骤如下:
- 将索引i处的元素设置为最小值。迭代[i,j]并将该范围内的每个值与arr [i]比较,如果范围内的值小于arr [i],则在arr [i]和当前元素之间交换该值。增加i的值。
- 将索引j处的元素设置为最小值。迭代[i,j]并将该范围内的每个值与arr [j]比较,如果范围内的值小于arr [j],则在arr [j]与当前元素之间交换值。减小j的值。
- 在第一次迭代中将最小的元素放在第i个位置,将第二个最小的元素放在第j个位置。这样做直到i和j都指向相同的位置。
下面是上述方法的实现:
C++
// C++ program for the above approach
#include
using namespace std;
// Function to perform the rearrangement
void rearrange(int a[], int N)
{
// Initialize variables
int i = 0, j = N - 1;
int min = 0, k, x = 0, temp;
// Loop until i crosses j
while (i < j)
{
// This check is to find the
// minimum values in the
// ascending order
for(k = i; k <= j; k++)
{
if (a[k] < a[min])
min = k;
}
// Condition to alternatively
// iterate variable i and j
if (x % 2 == 0)
{
// Perform swap operation
temp = a[i];
a[i] = a[min];
a[min] = temp;
// Increment i
i++;
// Assign the value of min
min = i;
}
else
{
// Perform swap
temp = a[j];
a[j] = a[min];
a[min] = temp;
// Decrement i
j--;
// Assign the value of min
min = j;
}
x++;
}
// Print the array
for(i = 0; i < N; i++)
cout << a[i] << " ";
}
// Driver Code
int main()
{
// Given Array arr[]
int arr[] = { 1, 3, 3, 4, 5 };
int N = sizeof(arr) / sizeof(arr[0]);
// Function call
rearrange(arr, N);
return 0;
}
// This code is contributed by divyeshrabadiya07
Java
// Java program for the above approach
import java.io.*;
import java.util.*;
public class GFG {
// Function to perform the rearrangement
static void rearrange(int[] a)
{
// Initialize variables
int i = 0, j = a.length - 1;
int min = 0, k, x = 0, temp;
// Loop until i crosses j
while (i < j) {
// This check is to find the
// minimum values in the
// ascending order
for (k = i; k <= j; k++) {
if (a[k] < a[min])
min = k;
}
// Condition to alternatively
// iterate variable i and j
if (x % 2 == 0) {
// Perform swap operation
temp = a[i];
a[i] = a[min];
a[min] = temp;
// Increment i
i++;
// Assign the value of min
min = i;
}
else {
// Perform swap
temp = a[j];
a[j] = a[min];
a[min] = temp;
// Decrement i
j--;
// Assign the value of min
min = j;
}
x++;
}
// Print the array
for (i = 0; i < a.length; i++)
System.out.print(a[i] + " ");
}
// Driver Code
public static void main(String[] args)
{
// Given Array arr[]
int arr[] = { 1, 3, 3, 4, 5 };
// Function Call
rearrange(arr);
}
}
Python3
# Python3 program for
# the above approach
# Function to perform
# the rearrangement
def rearrange(a, N):
# Initialize variables
i = 0
j = N - 1
min = 0
x = 0
# Loop until i crosses j
while (i < j):
# This check is to find the
# minimum values in the
# ascending order
for k in range (i, j + 1):
if (a[k] < a[min]):
min = k
# Condition to alternatively
# iterate variable i and j
if (x % 2 == 0):
# Perform swap operation
temp = a[i]
a[i] = a[min]
a[min] = temp
# Increment i
i += 1
# Assign the value of min
min = i
else:
# Perform swap
temp = a[j]
a[j] = a[min]
a[min] = temp
# Decrement i
j -= 1
# Assign the value of min
min = j
x += 1
# Print the array
for i in range (N):
print (a[i] ,end = " ")
# Driver Code
if __name__ == "__main__":
# Given Array arr[]
arr = [1, 3, 3, 4, 5]
N = len(arr)
# Function call
rearrange(arr, N)
# This code is contributed by Chitranayal
C#
// C# program for the above approach
using System;
class GFG{
// Function to perform the rearrangement
static void rearrange(int[] a)
{
// Initialize variables
int i = 0, j = a.Length - 1;
int min = 0, k, x = 0, temp;
// Loop until i crosses j
while (i < j)
{
// This check is to find the
// minimum values in the
// ascending order
for(k = i; k <= j; k++)
{
if (a[k] < a[min])
min = k;
}
// Condition to alternatively
// iterate variable i and j
if (x % 2 == 0)
{
// Perform swap operation
temp = a[i];
a[i] = a[min];
a[min] = temp;
// Increment i
i++;
// Assign the value of min
min = i;
}
else
{
// Perform swap
temp = a[j];
a[j] = a[min];
a[min] = temp;
// Decrement i
j--;
// Assign the value of min
min = j;
}
x++;
}
// Print the array
for(i = 0; i < a.Length; i++)
Console.Write(a[i] + " ");
}
// Driver Code
public static void Main(string[] args)
{
// Given array arr[]
int []arr = { 1, 3, 3, 4, 5 };
// Function call
rearrange(arr);
}
}
// This code is contributed by rutvik_56
输出:
1 3 5 4 3
时间复杂度: O(N 2 )
辅助空间: O(1)