📜  对出现在K的倍数之间的数组元素进行排序

📅  最后修改于: 2021-04-22 06:36:36             🧑  作者: Mango

给定一个数组arr []和一个整数K。任务是对介于K的任意两个倍数之间的元素进行排序。

例子:

方法:遍历数组并跟踪K的倍数,从K第二倍数开始,对K的当前倍数与先前K的倍数之间的每个元素进行排序。最后打印更新的数组。

下面是上述方法的实现:

C++
// C++ implementation of the approach
#include
using namespace std;
  
// Utility function to print
// the contents of an array
void printArr(int arr[], int n)
{
    for (int i = 0; i < n; i++)
        cout << (arr[i]) << " ";
}
  
// Function to sort elements
// in between multiples of k
void sortArr(int arr[], int n, int k)
{
  
    // To store the index of
    // previous multiple of k
    int prev = -1;
    for (int i = 0; i < n; i++) 
    {
        if (arr[i] % k == 0)
        {
  
            // If it is not the
            // first multiple of k
            if (prev != -1)
  
                // Sort the elements in between 
                // the previous and the current 
                // multiple of k
                sort(arr + prev + 1, arr + i);
  
            // Update previous to be current
            prev = i;
        }
    }
  
    // Print the updated array
    printArr(arr, n);
}
  
// Driver code
int main()
{
    int arr[] = {2, 1, 13, 3, 7, 
                 8, 21, 13, 12};
    int n = sizeof(arr) / sizeof(arr[0]);
    int k = 2;
    sortArr(arr, n, k);
}
  
// This code is contributed by
// Surendra_Gangwar


Java
// Java implementation of the approach
import java.util.Arrays;
class GFG {
  
    // Utility function to print
    // the contents of an array
    static void printArr(int arr[], int n)
    {
        for (int i = 0; i < n; i++)
            System.out.print(arr[i] + " ");
    }
  
    // Function to sort elements
    // in between multiples of k
    static void sortArr(int arr[], int n, int k)
    {
  
        // To store the index of
        // previous multiple of k
        int prev = -1;
        for (int i = 0; i < n; i++) {
            if (arr[i] % k == 0) {
  
                // If it is not the
                // first multiple of k
                if (prev != -1)
  
                    // Sort the elements in between 
                    // the previous and the current 
                    // multiple of k
                    Arrays.sort(arr, prev + 1, i);
  
                // Update previous to be current
                prev = i;
            }
        }
  
        // Print the updated array
        printArr(arr, n);
    }
  
    // Driver code
    public static void main(String[] args)
    {
        int arr[] = { 2, 1, 13, 3, 7, 8, 21, 13, 12 };
        int n = arr.length;
        int k = 2;
        sortArr(arr, n, k);
    }
}


Python3
# Python3 implementation of the approach
  
# Utility function to print
# the contents of an array
def printArr(arr, n) :
    for i in range(n) :
        print(arr[i], end = " ");
  
# Function to sort elements
# in between multiples of k
def sortArr(arr, n, k) :
      
    # To store the index of
    # previous multiple of k
    prev = -1;
    for i in range(n) :
        if (arr[i] % k == 0) :
              
            # If it is not the first
            # multiple of k
            if (prev != -1) :
                  
                # Sort the elements in between 
                #the previous and the current 
                # multiple of k
                temp = arr[prev + 1:i];
                temp.sort();
                arr = arr[ : prev + 1] + temp + arr[i : ];
                  
            # Update previous to be current
            prev = i;
  
    # Print the updated array
    printArr(arr, n);
  
# Driver code
if __name__ == "__main__" :
      
    arr = [ 2, 1, 13, 3, 7, 8, 21, 13, 12 ];
    n = len(arr);
    k = 2;
      
    sortArr(arr, n, k);
  
# This code is contributed by Ryuga


C#
// C# implementation of the approach 
using System.Collections;
using System;
class GFG { 
  
    // Utility function to print 
    // the contents of an array 
    static void printArr(int []arr, int n) 
    { 
        for (int i = 0; i < n; i++) 
            Console.Write(arr[i] + " "); 
    } 
  
    // Function to sort elements 
    // in between multiples of k 
    static void sortArr(int []arr, int n, int k) 
    { 
  
        // To store the index of 
        // previous multiple of k 
        int prev = -1; 
        for (int i = 0; i < n; i++) { 
            if (arr[i] % k == 0) { 
  
                // If it is not the 
                // first multiple of k 
                if (prev != -1) 
  
                    // Sort the elements in between 
                    // the previous and the current 
                    // multiple of k 
                    Array.Sort(arr, prev + 1, i-(prev + 1)); 
  
                // Update previous to be current 
                prev = i; 
            } 
        } 
  
        // Print the updated array 
        printArr(arr, n); 
    } 
  
    // Driver code 
    public static void Main(String []args) 
    { 
        int []arr = { 2, 1, 13, 3, 7, 8, 21, 13, 12 }; 
        int n = arr.Length; 
        int k = 2; 
        sortArr(arr, n, k); 
    } 
} 
//contributed by Arnab Kundu


输出:
2 1 3 7 13 8 13 21 12