You have given an array which contain 1 to n element, your task is to sort this array in an efficient way and without replace with 1 to n numbers.
Examples :
Input : arr[] = {10, 7, 9, 2, 8, 3, 5, 4, 6, 1};
Output : 1 2 3 4 5 6 7 8 9 10
Native approach :
Sort this array with the use of any type of sorting method. it takes O(nlogn) minimum time.
Efficient Approach (Using Cyclic Sort):
Idea:
The given array contains number in the range [1 to n] so we can use cyclic sort
Follow the steps mentioned below to solve the problem
- Traverse the array
- Check if the array is at correct position
- Else swap the element to the element at its correct position
Below is the code implementation of the above approach:
C++
#include <bits/stdc++.h>
using namespace std;
void sort( int arr[], int n)
{
int i = 0;
while (i < n) {
int correct = arr[i] - 1;
if (arr[correct] != arr[i]) {
swap(arr[i], arr[correct]);
}
else {
i++;
}
}
}
void swap( int & a, int & b)
{
int temp = a;
a = b;
b = temp;
}
int main()
{
int arr[] = {3, 2, 5, 6, 1, 4};
int n = sizeof (arr) / sizeof (arr[0]);
sort(arr, n);
for ( int i = 0; i < n; i++)
cout << arr[i] << " " ;
return 0;
}
|
Java
import java.io.*;
import java.util.Arrays;
class GFG {
public static void main(String[] args)
{
int [] arr = { 3 , 2 , 5 , 6 , 1 , 4 };
sort(arr);
System.out.println(Arrays.toString(arr));
}
static void sort( int [] arr)
{
int i = 0 ;
while (i < arr.length) {
int correct = arr[i] - 1 ;
if (arr[correct] != arr[i]) {
swap(arr, i, correct);
}
else {
i++;
}
}
}
static void swap( int [] arr, int first, int second)
{
int temp = arr[first];
arr[first] = arr[second];
arr[second] = temp;
}
}
|
Python3
def sort(arr, n):
i = 0
while (i < n):
correct = arr[i] - 1
if arr[correct] ! = arr[i]:
swap(arr, i, correct)
else :
i = i + 1
def swap(arr, first, second):
temp = arr[first]
arr[first] = arr[second]
arr[second] = temp
arr = [ 3 , 2 , 5 , 6 , 1 , 4 ]
n = len (arr)
sort(arr, n)
for i in range ( 0 , n):
print (arr[i], end = " " )
|
Javascript
function sort(arr, n) {
var i = 0;
while (i < n) {
var correct = arr[i] - 1;
if (arr[correct] != arr[i]) {
swap(arr, i, correct);
} else {
i++;
}
}
}
function swap(arr, i, correct) {
var temp = arr[i];
arr[i] = arr[correct];
arr[correct] = temp;
}
var arr = [3, 2, 5, 6, 1, 4];
var n = 6;
sort(arr, n);
for ( var i = 0; i < n; i++) {
console.log(arr[i]);
}
|
C#
using System;
class MainClass
{
static void sort( int [] arr, int n)
{
int i = 0;
while (i < n)
{
int correct = arr[i] - 1;
if (arr[correct] != arr[i])
{
swap( ref arr[i], ref arr[correct]);
}
else
{
i++;
}
}
}
static void swap( ref int a, ref int b)
{
int temp = a;
a = b;
b = temp;
}
public static void Main()
{
int [] arr = { 3, 2, 5, 6, 1, 4 };
int n = arr.Length;
sort(arr, n);
for ( int i = 0; i < n; i++)
Console.Write(arr[i] + " " );
}
}
|
Output
[1, 2, 3, 4, 5, 6]
Time Complexity: O(n)
Auxiliary Space: O(1)
Feeling lost in the world of random DSA topics, wasting time without progress? It's time for a change! Join our DSA course, where we'll guide you on an exciting journey to master DSA efficiently and on schedule.
Ready to dive in? Explore our Free Demo Content and join our DSA course, trusted by over 100,000 geeks!