给定一个由N 个整数组成的数组arr[] ,任务是重新排列数组元素,使得最小的元素在第0 个位置,第二小的元素在第(N-1) 个位置,第三小的元素在第 1 个位置,第 4 个最小元素位于第(N-2) 个位置,对于arr[] 中的所有整数,依此类推。
例子:
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
Javascript
1 3 5 4 3
时间复杂度: O(N 2 )
辅助空间: O(1)
如果您想与行业专家一起参加直播课程,请参阅Geeks Classes Live