Sort the Array having one misplaced number by swapping elements any number of times
Given an array arr[ ] of size N, The array is sorted except one element. position pos of misplaced element is given, the task is to make the complete array sorted by swapping any two elements any number of times.
Examples:
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
Approach: This problem can be solved using greedy approach. Since the final array is to be sorted, elements on the left side of misplaced element must be smaller and elements on the right side must be greater. follow the steps given below to solve the problem:
- Traverse the array arr[ ], if element on left from pos arr[i], is greater than arr[pos], swap arr[i] and arr[pos].
- If element on right from pos arr[j], is smaller than arr[pos], swap arr[j] and arr[pos].
- Printing arr[ ] will be the last step.
Below is the implementation of the above approach.
C++
#include <bits/stdc++.h>
using namespace std;
void PrintSortedArr( int n, int pos, int arr[])
{
for ( int i = 0; i < n; ++i) {
if (i < pos) {
if (arr[pos] < arr[i]) {
swap(arr[pos], arr[i]);
}
}
else if (i > pos) {
if (arr[pos] > arr[i]) {
swap(arr[pos], arr[i]);
pos = i;
}
}
}
cout << "Sorted Array: " ;
for ( int i = 0; i < n; ++i) {
cout << arr[i] << " " ;
}
}
int main()
{
int N = 7, pos = 6;
int arr[] = { 1, 2, 3, 4, 9, 15, 0 };
PrintSortedArr(N, pos, arr);
return 0;
}
|
Java
import java.io.*;
import java.util.*;
class GFG{
static void PrintSortedArr( int n, int pos, int arr[])
{
for ( int i = 0 ; i < n; ++i) {
if (i < pos) {
if (arr[pos] < arr[i]) {
int t = arr[pos];
arr[pos] = arr[i];
arr[i] = t;
}
}
else if (i > pos) {
if (arr[pos] > arr[i]) {
int t = arr[pos];
arr[pos] = arr[i];
arr[i] = t;
pos = i;
}
}
}
System.out.print( "Sorted Array: " );
for ( int i = 0 ; i < n; ++i) {
System.out.print( arr[i] + " " );
}
}
public static void main(String args[])
{
int N = 7 , pos = 6 ;
int arr[] = { 1 , 2 , 3 , 4 , 9 , 15 , 0 };
PrintSortedArr(N, pos, arr);
}
}
|
Python3
def PrintSortedArr(n, pos, arr):
for i in range (n):
if (i < pos):
if (arr[pos] < arr[i]):
temp = arr[pos]
arr[pos] = arr[i]
arr[i] = temp
elif (i > pos):
if (arr[pos] > arr[i]):
temp = arr[pos]
arr[pos] = arr[i]
arr[i] = temp
pos = i
print ( "Sorted Array: " ,end = "")
for i in range (n):
print (arr[i],end = " " )
if __name__ = = '__main__' :
N = 7
pos = 6
arr = [ 1 , 2 , 3 , 4 , 9 , 15 , 0 ]
PrintSortedArr(N, pos, arr)
|
C#
using System;
class GFG{
static void PrintSortedArr( int n, int pos, int []arr)
{
for ( int i = 0; i < n; ++i) {
if (i < pos) {
if (arr[pos] < arr[i]) {
int t = arr[pos];
arr[pos] = arr[i];
arr[i] = t;
}
}
else if (i > pos) {
if (arr[pos] > arr[i]) {
int t = arr[pos];
arr[pos] = arr[i];
arr[i] = t;
pos = i;
}
}
}
Console.Write( "Sorted Array: " );
for ( int i = 0; i < n; ++i) {
Console.Write( arr[i] + " " );
}
}
public static void Main(String []args)
{
int N = 7, pos = 6;
int []arr = { 1, 2, 3, 4, 9, 15, 0 };
PrintSortedArr(N, pos, arr);
}
}
|
Javascript
<script>
function PrintSortedArr(n, pos, arr) {
for (let i = 0; i < n; ++i) {
if (i < pos) {
if (arr[pos] < arr[i]) {
let temp = arr[pos];
arr[pos] = arr[i];
arr[i] = temp;
}
}
else if (i > pos) {
if (arr[pos] > arr[i]) {
let temp = arr[pos];
arr[pos] = arr[i];
arr[i] = temp;
pos = i;
}
}
}
document.write( "Sorted Array: " );
for (let i = 0; i < n; ++i) {
document.write(arr[i] + " " );
}
}
let N = 7, pos = 6;
let arr = [1, 2, 3, 4, 9, 15, 0];
PrintSortedArr(N, pos, arr);
</script>
|
Output
Sorted Array: 0 1 2 3 4 9 15
Time Complexity: O(N)
Auxiliary Space: O(1)
Last Updated :
02 Sep, 2021
Like Article
Save Article
Share your thoughts in the comments
Please Login to comment...