Prerequisite: Cycle Sort
Given an array arr[] of elements from 1 to N, the task is to sort the given array in O(N) time.
Examples:
Input: arr[] = { 2, 1, 5, 4, 3}
Output: 1 2 3 4 5
Explanation:
Since arr[0] = 2 is not at correct position, then swap arr[0] with arr[arr[0] – 1]
Now array becomes: arr[] = {1, 2, 5, 4, 3}
Now arr[2] = 5 is not at correct position, then swap arr[2] with arr[arr[2] – 1]
Now the array becomes: arr[] = {1, 2, 3, 4, 5}
Now the array is sorted.
Input: arr[] = {1, 2, 3, 4, 5, 6}
Output: 1 2 3 4 5 6
Explanation:
The array is already sorted.
Approach: This problem can be solved using Greedy Approach. Below are the steps:
- Traverse the given array arr[].
- If the current element is not at the correct position i.e., arr[i] is not equal to i+1 then, swap the current element with the element with its correct position.
For Example:
Let arr[] = {2, 1, 4, 5, 3}
Since, arr[0] = 2, which is not at it’s correct position 1.
Then swap this element with it’s correct position, i.e., swap(arr[0], arr[2-1])
Then array becomes: arr[] = {1, 2, 4, 5, 3}
- If the current element is at the correct position then check for the next element.
- Repeat the above steps until we reach the end of the array.
Below is the implementation of the above approach:
C++
#include "bits/stdc++.h"
using namespace std;
void swap( int * a, int * b)
{
int temp = *a;
*a = *b;
*b = temp;
}
void printArray( int arr[], int N)
{
for ( int i = 0; i < N; i++) {
cout << arr[i] << ' ' ;
}
}
void sortArray( int arr[], int N)
{
for ( int i = 0; i < N;) {
if (arr[i] == i + 1) {
i++;
}
else {
swap(&arr[i], &arr[arr[i] - 1]);
}
}
}
int main()
{
int arr[] = { 2, 1, 5, 3, 4 };
int N = sizeof (arr) / sizeof (arr[0]);
sortArray(arr, N);
printArray(arr, N);
return 0;
}
|
Java
class Main{
public static void printArray( int arr[], int N)
{
for ( int i = 0 ; i < N; i++)
{
System.out.print(arr[i] + " " );
}
}
public static void sortArray( int arr[], int N)
{
for ( int i = 0 ; i < N;)
{
if (arr[i] == i + 1 )
{
i++;
}
else
{
int temp1 = arr[i];
int temp2 = arr[arr[i] - 1 ];
arr[i] = temp2;
arr[temp1 - 1 ] = temp1;
}
}
}
public static void main(String[] args)
{
int arr[] = { 2 , 1 , 5 , 3 , 4 };
int N = arr.length;
sortArray(arr, N);
printArray(arr, N);
}
}
|
Python3
def printArray(arr, N):
for i in range (N):
print (arr[i], end = ' ' )
def sortArray(arr, N):
i = 0
while i < N:
if arr[i] = = i + 1 :
i + = 1
else :
temp1 = arr[i]
temp2 = arr[arr[i] - 1 ]
arr[i] = temp2
arr[temp1 - 1 ] = temp1
if __name__ = = '__main__' :
arr = [ 2 , 1 , 5 , 3 , 4 ]
N = len (arr)
sortArray(arr, N)
printArray(arr, N)
|
C#
using System;
class GFG{
public static void printArray( int []arr, int N)
{
for ( int i = 0; i < N; i++)
{
Console.Write(arr[i] + " " );
}
}
public static void sortArray( int []arr, int N)
{
for ( int i = 0; i < N; )
{
if (arr[i] == i + 1)
{
i++;
}
else
{
int temp1 = arr[i];
int temp2 = arr[arr[i] - 1];
arr[i] = temp2;
arr[temp1 - 1] = temp1;
}
}
}
public static void Main(String[] args)
{
int []arr = {2, 1, 5, 3, 4};
int N = arr.Length;
sortArray(arr, N);
printArray(arr, N);
}
}
|
Javascript
<script>
function printArray(arr, N)
{
for ( var i = 0; i < N; i++) {
document.write( arr[i] + ' ' );
}
}
function sortArray(arr, N)
{
for ( var i = 0; i < N;) {
if (arr[i] == i + 1) {
i++;
}
else {
var temp1 = arr[i];
var temp2 = arr[arr[i] - 1];
arr[i] = temp2;
arr[temp1 - 1] = temp1;
}
}
}
var arr = [ 2, 1, 5, 3, 4 ];
var N = arr.length;
sortArray(arr, N);
printArray(arr, N);
</script>
|
Time Complexity: O(N), where N is the length of the array.
Auxiliary Space: O(1)