📌  相关文章
📜  在任何子数组上使用最多 N 个循环移位对给定数组进行排序

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

在任何子数组上使用最多 N 个循环移位对给定数组进行排序

给定一个数组arr[]包含N个整数,有重复。任务是在任何子数组上使用最多 N个循环移位以升序对数组进行排序。

打印排序数组所需的这种移位次数。可以有多种可能性。

例子:

方法:可能有两种情况:

  1. 数组已经排好序的情况:那么我们不需要进行任何移位操作
  2. 数组未排序的情况:请按照以下步骤操作:
    • 创建一个变量count = 0来存储计数的总数。
    • i = 0迭代到i = N-2 。并且对于每次迭代执行以下操作:
      • 创建一个变量minVal以存储在此迭代中找到的最小值并使用arr[i]的值启动它。
      • 开始从 i+1迭代到N-1 。如果当前值小于minVal,则更新 minVal
      • 标记该位置以执行从 i向右偏移 1 的循环移位。
    • 如果不存在这样的正确值,则只需移动到下一次迭代
    • 否则,将数组从i向右旋转1 个位置,并将count增加 1。
    • 返回 count 的值作为您的答案。

以下是上述方法的 C++ 实现:

C++
// C++ code to implement the above approach
 
#include 
using namespace std;
 
// Function to Sort given Array using
// at most N cyclic shift on any subarray
int ShiftingSort(vector& arr, int n)
{
    vector temp(arr.begin(), arr.end());
    sort(temp.begin(), temp.end());
 
    // Variable to store count of
    // shifting operations
    int count = 0;
 
    // If the vector arr is already sorted
    // the 0 operations shift required
    if (arr == temp) {
        return 0;
    }
    else {
        // Run a loop from 0 to n-2 index
        for (int index1 = 0; index1 < n - 1; index1++) {
            int minval = arr[index1];
            int left = index1;
            int right = -1;
 
            // Loop from i+1 to n-1
            // to find the minimum value
            for (int index2 = index1 + 1; index2 < n;
                 index2++) {
                if (minval > arr[index2]) {
                    minval = arr[index2];
                    right = index2;
                }
            }
 
            // Check if the shifting is required
            if (right != -1) {
                // Increment count operations
                count++;
 
                int index = right;
                int tempval = arr[right];
 
                // Loop for shifting the array arr
                // from index = left to index = right
                while (index > left) {
                    arr[index] = arr[index - 1];
                    index--;
                }
                arr[index] = tempval;
            }
        }
    }
 
    // Return the total operations
    return count;
}
 
// Driver code
int main()
{
    vector arr{ 1, 3, 2, 8, 5 };
    int N = 5;
 
    cout << ShiftingSort(arr, N) << "\n";
 
    return 0;
}


Java
// Java code to implement the above approach
import java.util.*;
 
public class GFG
{
   
// Function to Sort given Array using
// at most N cyclic shift on any subarray
static int ShiftingSort(ArrayList arr, int n)
{
    ArrayList temp = new ArrayList();
    for(int i = 0; i < arr.size(); i++) {
        temp.add(arr.get(i));
    }
     
    Collections.sort(temp);
     
    // Variable to store count of
    // shifting operations
    int count = 0;
 
    // If the vector arr is already sorted
    // the 0 operations shift required
    if (arr.equals(temp)) {
        return 0;
    }
    else {
        // Run a loop from 0 to n-2 index
        for (int index1 = 0; index1 < n - 1; index1++) {
            int minval = arr.get(index1);
            int left = index1;
            int right = -1;
 
            // Loop from i+1 to n-1
            // to find the minimum value
            for (int index2 = index1 + 1; index2 < n;
                 index2++) {
                if (minval > arr.get(index2)) {
                    minval = arr.get(index2);
                    right = index2;
                }
            }
 
            // Check if the shifting is required
            if (right != -1) {
                // Increment count operations
                count++;
 
                int index = right;
                int tempval = arr.get(right);
 
                // Loop for shifting the array arr
                // from index = left to index = right
                while (index > left) {
                    arr.set(index, arr.get(index - 1));
                    index--;
                }
                arr.set(index, tempval);
            }
        }
    }
 
    // Return the total operations
    return count;
}
 
// Driver code
public static void main(String args[])
{
    ArrayList arr = new ArrayList();
     
    arr.add(1);
    arr.add(3);
    arr.add(2);
    arr.add(8);
    arr.add(5);
     
    int N = 5;
 
    System.out.println(ShiftingSort(arr, N));
}
}
 
// This code is contributed by Samim Hossain Mondal.


Python3
# Python Program to implement
# the above approach
 
# Function to Sort given Array using
# at most N cyclic shift on any subarray
def ShiftingSort(arr, n):
    temp = arr.copy()
    temp.sort()
 
    # Variable to store count of
    # shifting operations
    count = 0
 
    # If the vector arr is already sorted
    # the 0 operations shift required
    if (arr == temp):
        return 0
    else:
 
        # Run a loop from 0 to n-2 index
        for index1 in range(n - 1):
            minval = arr[index1]
            left = index1
            right = -1
 
            # Loop from i+1 to n-1
            # to find the minimum value
            for index2 in range(index1 + 1, n):
                if (minval > arr[index2]):
                    minval = arr[index2]
                    right = index2
 
            # Check if the shifting is required
            if (right != -1):
 
                # Increment count operations
                count += 1
                index = right
                tempval = arr[right]
 
                # Loop for shifting the array arr
                # from index = left to index = right
                while (index > left):
                    arr[index] = arr[index - 1]
                    index -= 1
                arr[index] = tempval
 
    # Return the total operations
    return count
 
# Driver code
arr = [1, 3, 2, 8, 5]
N = 5
print(ShiftingSort(arr, N))
 
# This code is contributed by gfgking


C#
// C# code to implement the above approach
using System;
using System.Collections;
using System.Collections.Generic;
 
class GFG
{
   
// Function to Sort given Array using
// at most N cyclic shift on any subarray
static int ShiftingSort(ArrayList arr, int n)
{
    ArrayList temp = new ArrayList();
    for(int i = 0; i < arr.Count; i++) {
        temp.Add(arr[i]);
    }
     
    temp.Sort();
     
    // Variable to store count of
    // shifting operations
    int count = 0;
 
    // If the vector arr is already sorted
    // the 0 operations shift required
    if (arr.Equals(temp)) {
        return 0;
    }
    else
    {
       
        // Run a loop from 0 to n-2 index
        for (int index1 = 0; index1 < n - 1; index1++) {
            int minval = (int)arr[index1];
            int left = index1;
            int right = -1;
 
            // Loop from i+1 to n-1
            // to find the minimum value
            for (int index2 = index1 + 1; index2 < n;
                 index2++) {
                if (minval > (int)arr[index2]) {
                    minval = (int)arr[index2];
                    right = index2;
                }
            }
 
            // Check if the shifting is required
            if (right != -1)
            {
               
                // Increment count operations
                count++;
 
                int index = right;
                int tempval = (int)arr[right];
 
                // Loop for shifting the array arr
                // from index = left to index = right
                while (index > left) {
                    arr[index] = arr[index - 1];
                    index--;
                }
                arr[index] = tempval;
            }
        }
    }
 
    // Return the total operations
    return count;
}
 
// Driver code
public static void Main()
{
    ArrayList arr = new ArrayList();
     
    arr.Add(1);
    arr.Add(3);
    arr.Add(2);
    arr.Add(8);
    arr.Add(5);
     
    int N = 5;
 
    Console.Write(ShiftingSort(arr, N));
}
}
 
// This code is contributed by Samim Hossain Mondal.


Javascript


输出
2

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