📌  相关文章
📜  通过任意次数交换元素对具有一个错位数字的数组进行排序

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

通过任意次数交换元素对具有一个错位数字的数组进行排序

给定一个大小为N的数组arr[ ] ,除一个元素外,该数组已排序。给定错位元素的位置pos ,任务是通过交换任意两个元素任意次数来使整个数组排序。

例子:

方法:这个问题可以使用贪心方法来解决。由于要对最终数组进行排序,错位元素左侧的元素必须更小,右侧的元素必须更大。请按照以下步骤解决问题:

  • 遍历数组arr[ ] ,如果pos arr[i]左边的元素大于arr[pos] ,则交换 arr[i] 和 arr[pos]
  • 如果pos arr[j]右边的元素小于arr[pos] ,则交换 arr[j] 和 arr[pos]
  • 打印arr[ ]将是最后一步。

下面是上述方法的实现。

C++
// C++ program for the above approach.
#include 
 
using namespace std;
 
// Function to print sorted array
void PrintSortedArr(int n, int pos, int arr[])
{
 
    // Traversing element of array.
    for (int i = 0; i < n; ++i) {
 
        // For the element on left of pos
        if (i < pos) {
            if (arr[pos] < arr[i]) {
                swap(arr[pos], arr[i]);
            }
        }
 
        // For the element on right of pos
        else if (i > pos) {
            if (arr[pos] > arr[i]) {
                swap(arr[pos], arr[i]);
                pos = i;
            }
        }
    }
 
    // Printing the sorted array
    cout << "Sorted Array: ";
    for (int i = 0; i < n; ++i) {
        cout << arr[i] << " ";
    }
}
 
// Driver Code
int main()
{
 
    // Given Input
    int N = 7, pos = 6;
    int arr[] = { 1, 2, 3, 4, 9, 15, 0 };
 
    // Function Call
    PrintSortedArr(N, pos, arr);
 
    return 0;
}


Java
// Java program for the above approach
import java.io.*;
import java.util.*;
 
class GFG{
 
// Function to print sorted array
static void PrintSortedArr(int n, int pos, int arr[])
{
     
    // Traversing element of array.
    for (int i = 0; i < n; ++i) {
 
        // For the element on left of pos
        if (i < pos) {
            if (arr[pos] < arr[i]) {
                int t = arr[pos];
                arr[pos] = arr[i];
                arr[i] = t;
            }
        }
 
        // For the element on right of pos
        else if (i > pos) {
            if (arr[pos] > arr[i]) {
                int t = arr[pos];
                arr[pos] = arr[i];
                arr[i] = t;
                pos = i;
            }
        }
    }
 
    // Printing the sorted array
    System.out.print("Sorted Array: ");
    for (int i = 0; i < n; ++i) {
        System.out.print( arr[i] + " ");
    }
}
 
 
// Driver code
public static void main(String args[])
{
    // Given Input
    int N = 7, pos = 6;
    int arr[] = { 1, 2, 3, 4, 9, 15, 0 };
 
    // Function Call
    PrintSortedArr(N, pos, arr);
 
}
}
 
// This code is contributed by code_hunt.


Python3
# Python 3 program for the above approach.
 
# Function to print sorted array
def PrintSortedArr(n, pos, arr):
    # Traversing element of array.
    for i in range(n):
        # For the element on left of pos
        if (i < pos):
            if (arr[pos] < arr[i]):
                temp = arr[pos]
                arr[pos] = arr[i]
                arr[i] = temp
 
        # For the element on right of pos
        elif (i > pos):
            if (arr[pos] > arr[i]):
                temp = arr[pos]
                arr[pos] = arr[i]
                arr[i] = temp
                pos = i
 
    # Printing the sorted array
    print("Sorted Array: ",end="")
    for i in range(n):
        print(arr[i],end = " ")
 
# Driver Code
if __name__ == '__main__':
   
    # Given Input
    N = 7
    pos = 6
    arr = [1, 2, 3, 4, 9, 15, 0]
 
    # Function Call
    PrintSortedArr(N, pos, arr)
     
    # This code is contributed by ipg2016107.


C#
// C# program for the above approach
using System;
 
class GFG{
 
// Function to print sorted array
static void PrintSortedArr(int n, int pos, int []arr)
{
     
    // Traversing element of array.
    for (int i = 0; i < n; ++i) {
 
        // For the element on left of pos
        if (i < pos) {
            if (arr[pos] < arr[i]) {
                int t = arr[pos];
                arr[pos] = arr[i];
                arr[i] = t;
            }
        }
 
        // For the element on right of pos
        else if (i > pos) {
            if (arr[pos] > arr[i]) {
                int t = arr[pos];
                arr[pos] = arr[i];
                arr[i] = t;
                pos = i;
            }
        }
    }
 
    // Printing the sorted array
    Console.Write("Sorted Array: ");
    for (int i = 0; i < n; ++i) {
        Console.Write( arr[i] + " ");
    }
}
 
 
// Driver code
public static void Main(String []args)
{
    // Given Input
    int N = 7, pos = 6;
    int []arr = { 1, 2, 3, 4, 9, 15, 0 };
 
    // Function Call
    PrintSortedArr(N, pos, arr);
 
}
}
 
// This code is contributed by shivanisinghss2110


Javascript



输出
Sorted Array: 0 1 2 3 4 9 15 

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