📌  相关文章
📜  从索引 K 开始对给定循环数组的 M 个元素进行排序

📅  最后修改于: 2021-10-27 03:26:04             🧑  作者: Mango

给定一个大小为N的圆形数组arr[]和两个整数KM ,任务是从索引K开始对M数组元素进行排序。

例子:

方法:这个想法是如果它们的元素顺序不正确,则交换圆形数组中的相邻元素。请按照以下步骤解决给定的问题:

  • 使用变量i[0, M – 1]范围内遍历给定数组arr[]
    • 使用变量j[K, K + M – 1]范围内再次遍历数组arr[] ,如果arr[j%n]大于arr[(j + 1)%n],则交换当前相邻对值。
  • 完成上述步骤后,打印数组arr[]

下面是上述方法的实现:

C++
// C++ program for the above approach
#include 
using namespace std;
 
// Function to print the circular array
void printCircularArray(int arr[], int n)
{
    // Print the array
    for (int i = 0; i < n; i++) {
        cout << arr[i] << " ";
    }
}
 
// Function to sort m elements of diven
// circular array starting from index k
void sortCircularArray(int arr[], int n,
                       int k, int m)
{
    // Traverse M elements
    for (int i = 0; i < m; i++) {
 
        // Iterate from index k to k + m - 1
        for (int j = k; j < k + m - 1; j++) {
 
            // Check if the next element
            // in the circular array is
            // less than the current element
            if (arr[j % n]
                > arr[(j + 1) % n]) {
 
                // Swap current element
                // with the next element
                swap(arr[j % n], arr[(j + 1) % n]);
            }
        }
    }
 
    // Print the circular array
    printCircularArray(arr, n);
}
 
// Driver Code
int main()
{
    int arr[] = { 4, 1, 6, 5, 3 };
    int K = 2, M = 3;
    int N = sizeof(arr) / sizeof(arr[0]);
 
    // Function Call
    sortCircularArray(arr, N, K, M);
 
    return 0;
}


Java
// Java program for the above approach
class GFG{
      
// Function to print the circular array
static void printCircularArray(int arr[], int n)
{
    // Print the array
    for (int i = 0; i < n; i++) {
        System.out.print(arr[i] + " ");
    }
}
  
// Function to sort m elements of diven
// circular array starting from index k
static void sortCircularArray(int arr[], int n,
                       int k, int m)
{
    // Traverse M elements
    for (int i = 0; i < m; i++) {
  
        // Iterate from index k to k + m - 1
        for (int j = k; j < k + m - 1; j++) {
  
            // Check if the next element
            // in the circular array is
            // less than the current element
            if (arr[j % n]
                > arr[(j + 1) % n]) {
  
                // Swap current element
                // with the next element
                int t = arr[j % n];
                arr[j % n] = arr[(j + 1) % n];
                arr[(j + 1) % n] = t;
            }
        }
    }
  
    // Print the circular array
    printCircularArray(arr, n);
}
  
// Driver Code
public static void main (String[] args)
{  
    int[] arr = { 4, 1, 6, 5, 3 };
    int K = 2, M = 3;
    int N = arr.length;
  
    // Function Call
    sortCircularArray(arr, N, K, M);
}
}
 
// This code is contributed by susmitakundugoaldanga


Python3
# Python3 program for the above approach
 
# Function to print the circular array
def printCircularArray(arr, n):
     
    # Print the array
    for i in range(n):
        print(arr[i], end = " ")
 
# Function to sort m elements of diven
# circular array starting from index k
def sortCircularArray(arr, n, k, m):
 
    # Traverse M elements
    for i in range(m):
 
        # Iterate from index k to k + m - 1
        for j in range(k, k + m - 1):
 
            # Check if the next element
            # in the circular array is
            # less than the current element
            if (arr[j % n] > arr[(j + 1) % n]):
 
                # Swap current element
                # with the next element
                arr[j % n], arr[(j + 1) % n] = (arr[(j + 1) % n],
                                                arr[j % n])
                 
    # Print the circular array
    printCircularArray(arr, n)
     
# Driver Code
if __name__ == "__main__" :
 
    arr = [ 4, 1, 6, 5, 3 ]
    K = 2
    M = 3
    N = len(arr)
     
    # Function Call
    sortCircularArray(arr, N, K, M)
 
# This code is contributed by AnkThon


C#
// C# program for the above approach
using System;
 
class GFG
{
          
    // Function to print the circular array
    static void printCircularArray(int []arr, int n)
    {
        // Print the array
        for (int i = 0; i < n; i++)
        {
            Console.Write(arr[i] + " ");
        }
    }
      
    // Function to sort m elements of diven
    // circular array starting from index k
    static void sortCircularArray(int []arr, int n,
                           int k, int m)
    {
       
        // Traverse M elements
        for (int i = 0; i < m; i++)
        {
      
            // Iterate from index k to k + m - 1
            for (int j = k; j < k + m - 1; j++)
            {
      
                // Check if the next element
                // in the circular array is
                // less than the current element
                if (arr[j % n]
                    > arr[(j + 1) % n]) {
      
                    // Swap current element
                    // with the next element
                    int t = arr[j % n];
                    arr[j % n] = arr[(j + 1) % n];
                    arr[(j + 1) % n] = t;
                }
            }
        }
      
        // Print the circular array
        printCircularArray(arr, n);
    }
      
    // Driver Code
    public static void Main (string[] args)
    {  
        int[] arr = { 4, 1, 6, 5, 3 };
        int K = 2, M = 3;
        int N = arr.Length;
      
        // Function Call
        sortCircularArray(arr, N, K, M);
    }
}
 
// This code is contributed by AnkThon


Javascript


输出:

4 1 3 5 6

时间复杂度: O(N 2 )
辅助空间: O(1)

如果您希望与专家一起参加现场课程,请参阅DSA 现场工作专业课程学生竞争性编程现场课程