通过任意次数交换元素对具有一个错位数字的数组进行排序
给定一个大小为N的数组arr[ ] ,除一个元素外,该数组已排序。给定错位元素的位置pos ,任务是通过交换任意两个元素任意次数来使整个数组排序。
例子:
Input : N = 7, pos = 6, arr = {1, 2, 3, 4, 9, 15, 0}
Output: 0 1 2 3 4 9 15
Explanation:
Applying following swap arr can be sorted:
1. swap element 0 and 1, Now arr is {0, 2, 3, 4, 9, 15, 1}
2. swap element 1 and 2, Now arr is {0, 1, 3, 4, 9, 15, 2}
3. swap element 2 and 3, Now arr is {0, 1, 2, 4, 9, 15, 3}
4. swap element 3 and 4, Now arr is {0, 1, 2, 3, 9, 15, 4}
5. swap element 4 and 9, Now arr is {0, 1, 2, 3, 4, 15, 9}
6. swap element 9 and 15, Now arr is {0, 1, 2, 3, 4, 9, 15}
We can see now, the array arr[ ] has been sorted.
Input: N = 4, pos = 2, arr = {2, 3, 1, 4, }
Output: 1 2 3 4
方法:这个问题可以使用贪心方法来解决。由于要对最终数组进行排序,错位元素左侧的元素必须更小,右侧的元素必须更大。请按照以下步骤解决问题:
- 遍历数组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)