在数组中的相对位置对完美正方形进行排序
给定一个整数数组 ,任务是只对数组中相对位置的完全正方形的元素进行排序(其他元素的位置不能受到影响)。
例子:
Input: arr[] = {2, 64, 9, 8, 1, 4}
Output: 2 1 4 8 9 64
1, 4, 9 and 64 are the only perfect squares from the array.
Input: arr[] = {1, 49, 2, 36}
Output: 1 36 2 49
方法:
- 初始化两个空向量,从左到右遍历数组。
- 取一个整数和一个浮点变量,数组的每个元素都将其平方根存储在这两个变量中。
- 如果两个变量相等,则将该元素的索引推入第一个向量中,并将元素本身推入第二个向量中。
- 对第二个向量进行排序。
- 现在,我们有了第一个向量中所有必需元素的索引,以及第二个向量中按排序顺序排列的所有必需元素。
- 因此,将第二个向量的元素一个接一个地插入到第一个向量中存在的索引处的数组中。
下面是上述方法的实现:
C++
// C++ program to sort all the elements that are
// perfect squares in their relative positions
#include
using namespace std;
// function to sort all the elements that are
// perfect squares in their relative positions
void sortPerfectSquare(int arr[], int n)
{
int a;
float b;
// v1 will contain index of perfect squares
// v2 will contain each perfect square
vector v1;
vector v2;
for (int i = 0; i < n; i++) {
b = sqrt(arr[i]);
a = b;
// if both a and b are equal then
// arr[i] is a perfect square
if (a == b) {
v1.push_back(i);
v2.push_back(arr[i]);
}
}
// sort second vector
sort(v2.begin(), v2.end());
// put the sorted perfect square
// back into the array
int j = 0;
for (int i = 0; i < n; i++) {
if (v1[j] == i) {
arr[i] = v2[j];
j++;
}
}
// print final array
for (int i = 0; i < n; i++)
cout << arr[i] << " ";
}
// Driver code
int main()
{
int arr[] = { 9, 44, 100, 81, 21, 64 };
int n = sizeof(arr) / sizeof(arr[0]);
sortPerfectSquare(arr, n);
return 0;
}
Java
// Java program to sort all the elements that are
// perfect squares in their relative positions
import java.util.*;
class GFG
{
// function to sort all the elements that are
// perfect squares in their relative positions
static void sortPerfectSquare(int arr[], int n)
{
int a;
float b;
// v1 will contain index of perfect squares
// v2 will contain each perfect square
Vector v1 = new Vector();
Vector v2 = new Vector();
for (int i = 0; i < n; i++)
{
b = (float) Math.sqrt(arr[i]);
a = (int) b;
// if both a and b are equal then
// arr[i] is a perfect square
if (a == b)
{
v1.add(i);
v2.add(arr[i]);
}
}
// sort second vector
Collections.sort(v2);
// put the sorted perfect square
// back into the array
int j = 0;
for (int i = 0; i < n; i++)
{
if (v1.get(j) == i)
{
arr[i] = v2.get(j);
j++;
}
}
// print final array
for (int i = 0; i < n; i++)
System.out.print(arr[i]+" ");
}
// Driver code
public static void main(String[] args)
{
int arr[] = { 9, 44, 100, 81, 21, 64 };
int n = arr.length;
sortPerfectSquare(arr, n);
}
}
// This code is contributed by 29AjayKumar
Python3
# Python 3 program to sort all
# the elements that are perfect
# squares in their relative positions
# import sqrt() from math lib
from math import sqrt
# function to sort all the elements
# that are perfect squares in their
# relative positions
def sortPerfectSquare(arr, n) :
# v1 will contain index of
# perfect squares and v2 will
# contain each perfect square
v1 = []
v2 = []
for i in range(n):
b = sqrt(arr[i])
a = int(b)
# if both a and b are equal then
# arr[i] is a perfect square
if a == b :
v1.append(i)
v2.append(arr[i])
# sort second list
v2.sort()
j = 0
# put the sorted perfect square
# back into the array
for i in range(n) :
if v1[j] == i :
arr[i] = v2[j]
j += 1
# print final array
for i in range(n) :
print(arr[i], end = " ")
# Driver code
if __name__ == "__main__" :
arr = [9, 44, 100, 81, 21, 64]
n = len(arr)
sortPerfectSquare(arr, n);
# This code is contributed by ANKITRAI1
C#
// C# program to sort all the elements that are
// perfect squares in their relative positions
using System;
using System.Collections.Generic;
class GFG
{
// function to sort all the elements that are
// perfect squares in their relative positions
static void sortPerfectSquare(int []arr, int n)
{
int a;
float b;
// v1 will contain index of perfect squares
// v2 will contain each perfect square
List v1 = new List();
Listv2 = new List();
for (int i = 0; i < n; i++)
{
b = (float) Math.Sqrt(arr[i]);
a = (int) b;
// if both a and b are equal then
// arr[i] is a perfect square
if (a == b)
{
v1.Add(i);
v2.Add(arr[i]);
}
}
// sort second vector
v2.Sort();
// put the sorted perfect square
// back into the array
int j = 0;
for (int i = 0; i < n; i++)
{
if (v1[j] == i)
{
arr[i] = v2[j];
j++;
}
}
// print final array
for (int i = 0; i < n; i++)
Console.Write(arr[i] + " ");
}
// Driver code
public static void Main(String[] args)
{
int []arr = { 9, 44, 100, 81, 21, 64 };
int n = arr.Length;
sortPerfectSquare(arr, n);
}
}
// This code is contributed by
// PrinciRaj1992
PHP
Javascript
输出:
9 44 64 81 21 100