# Sort the Array having one misplaced number by swapping elements any number of times

• Last Updated : 02 Sep, 2021

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++

 `// 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

 ``

Output

`Sorted Array: 0 1 2 3 4 9 15 `

Time Complexity: O(N)
Auxiliary Space: O(1)

My Personal Notes arrow_drop_up