📜  对以 K 为模产生 P 的数组元素排序

📅  最后修改于: 2022-05-13 01:57:46.790000             🧑  作者: Mango

对以 K 为模产生 P 的数组元素排序

给定一个整数数组和一个数字 K。任务是仅对数组中除以 K 产生余数 P 的元素进行排序。排序必须仅在它们的相对位置进行,而不影响任何其他元素。
例子

方法:

  • 初始化两个空向量。
  • 从左到右遍历数组,并检查每个元素与 K 的模数。
  • 在第一个向量中,插入产生余数 P 的所有元素的索引。
  • 在第二个向量中,插入产生余数 P 的元素。
  • 对第二个向量进行排序。
  • 现在,我们有了所有必需元素的索引,以及排序顺序的所有必需元素。
  • 因此,将第二个向量的元素一个接一个地插入到第一个向量中存在的索引处的数组中。

下面是上述方法的实现:

C++
// C++ program for sorting array elements
// whose modulo with K yields P
 
#include 
using namespace std;
 
// Function to sort elements
// whose modulo with K yields P
void sortWithRemainderP(int arr[], int n, int k, int p)
{
    // initialise two vectors
    vector v1, v2;
 
    for (int i = 0; i < n; i++) {
        if (arr[i] % k == p) {
 
            // first vector contains indices of
            // required element
            v1.push_back(i);
 
            // second vector contains
            // required elements
            v2.push_back(arr[i]);
        }
    }
 
    // sorting the elements in second vector
    sort(v2.begin(), v2.end());
 
    // replacing the elements whose modulo with K yields P
    // with the sorted elements
    for (int i = 0; i < v1.size(); i++)
        arr[v1[i]] = v2[i];
 
    // printing the new sorted array elements
    for (int i = 0; i < n; i++)
        cout << arr[i] << " ";
}
 
// Driver code
int main()
{
    int arr[] = { 8, 255, 16, 2, 4, 0 };
    int n = sizeof(arr) / sizeof(arr[0]);
    int k = 2;
    int p = 0;
 
    sortWithRemainderP(arr, n, k, p);
 
    return 0;
}


Java
// Java program for sorting array elements
// whose modulo with K yields P
import java.util.*;
class GFG
{
 
// Function to sort elements
// whose modulo with K yields P
static void sortWithRemainderP(int arr[], int n, int k, int p)
{
    // initialise two vectors
    Vector v1 = new Vector();
    Vector v2 = new Vector();
 
    for (int i = 0; i < n; i++)
    {
        if (arr[i] % k == p)
        {
 
            // first vector contains indices of
            // required element
            v1.add(i);
 
            // second vector contains
            // required elements
            v2.add(arr[i]);
        }
    }
 
    // sorting the elements in second vector
    Collections.sort(v2);
 
    // replacing the elements whose modulo with K yields P
    // with the sorted elements
    for (int i = 0; i < v1.size(); i++)
        arr[v1.get(i)] = v2.get(i);
 
    // printing the new sorted array elements
    for (int i = 0; i < n; i++)
            System.out.print(arr[i]+" ");
}
 
// Driver code
public static void main(String[] args)
{
    int arr[] = { 8, 255, 16, 2, 4, 0 };
    int n = arr.length;
    int k = 2;
    int p = 0;
 
    sortWithRemainderP(arr, n, k, p);
    }
}
 
// This code is contributed by 29AjayKumar


Python3
# Python 3 program for sorting array
# elements whose modulo with K yields P
 
# Function to sort elements whose modulo
# with K yields P
def sortWithRemainderP(arr, n, k, p):
     
    # initialise two vectors
    v1 = []
    v2 = []
 
    for i in range(0, n, 1):
        if (arr[i] % k == p):
             
            # first vector contains indices
            # of required element
            v1.append(i)
 
            # second vector contains
            # required elements
            v2.append(arr[i])
 
    # sorting the elements in second vector
    v2.sort(reverse = False)
 
    # replacing the elements whose modulo
    # with K yields P with the sorted elements
    for i in range(0, len(v1), 1):
        arr[v1[i]] = v2[i]
 
    # printing the new sorted array elements
    for i in range(0, n, 1):
        print(arr[i], end = " ")
 
# Driver code
if __name__ == '__main__':
    arr = [8, 255, 16, 2, 4, 0]
    n = len(arr)
    k = 2
    p = 0
 
    sortWithRemainderP(arr, n, k, p)
     
# This code is contributed by
# Sahil_Shelangia


C#
// C# program for sorting array elements
// whose modulo with K yields P
using System;
using System.Collections.Generic;
 
class GFG
{
 
// Function to sort elements
// whose modulo with K yields P
static void sortWithRemainderP(int []arr, int n,
                               int k, int p)
{
    // initialise two vectors
    List v1 = new List();
    List v2 = new List();
 
    for (int i = 0; i < n; i++)
    {
        if (arr[i] % k == p)
        {
 
            // first vector contains indices of
            // required element
            v1.Add(i);
 
            // second vector contains
            // required elements
            v2.Add(arr[i]);
        }
    }
 
    // sorting the elements in second vector
    v2.Sort();
 
    // replacing the elements whose modulo with
    // K yields P with the sorted elements
    for (int i = 0; i < v1.Count; i++)
        arr[v1[i]] = v2[i];
 
    // printing the new sorted array elements
    for (int i = 0; i < n; i++)
        Console.Write(arr[i] + " ");
}
 
// Driver code
public static void Main(String[] args)
{
    int []arr = { 8, 255, 16, 2, 4, 0 };
    int n = arr.Length;
    int k = 2;
    int p = 0;
 
    sortWithRemainderP(arr, n, k, p);
}
}
 
// This code is contributed by PrinciRaj1992


PHP


Javascript


输出:
0 255 2 4 8 16

时间复杂度: O(nlogn)