# Sort an array which contains 1 to n values

Here given code implementation process.

``````/*
C program for
Sort an array which contains 1 to n values
*/
#include <stdio.h>

// Display array elements
void displayElement(int arr[], int n)
{
for (int i = 0; i < n; ++i)
{
printf("  %d",arr[i]);
}
}
void sortElement(int arr[], int n)
{
// Executes the loop through by size of array
for (int i = 0; i < n; ++i)
{
if(arr[i] < 1 || arr[i] > n)
{
// Element not between 1..n
return;
}
// Set sort element
arr[i] = i + 1;
}
}

int main()
{
// Input array elements
int arr[] = { 4, 10, 12, 1, 2, 7, 11, 5,  8, 9, 6, 3 } ;

// Get the number of elements
int n = sizeof(arr)/sizeof(arr[0]);

printf("\n Before Sort : ");
displayElement(arr,n);
sortElement(arr,n);
printf("\n After Sort  : ");
displayElement(arr,n);
return 0;
}``````

#### Output

`````` Before Sort :   4  10  12  1  2  7  11  5  8  9  6  3
After Sort  :   1  2  3  4  5  6  7  8  9  10  11  12``````
``````/*
Java program for
Sort an array which contains 1 to n values
*/
public class Sorting
{
// Display array elements
public void displayElement(int[] arr, int n)
{
for (int i = 0; i < n; ++i)
{
System.out.print(" " + arr[i]);
}
}
public void sortElement(int[] arr, int n)
{
// Executes the loop through by size of array
for (int i = 0; i < n; ++i)
{
if (arr[i] < 1 || arr[i] > n)
{
// Element not between 1..n
return;
}
// Set sort element
arr[i] = i + 1;
}
}
public static void main(String[] args)
{
// Input array elements
int[] arr = {
4 , 10 , 12 , 1 , 2 , 7 , 11 , 5 , 8 , 9 , 6 , 3
};
// Get the number of elements
int n = arr.length;
System.out.print("\n Before Sort : ");
System.out.print("\n After Sort : ");
}
}``````

#### Output

`````` Before Sort :  4 10 12 1 2 7 11 5 8 9 6 3
After Sort :  1 2 3 4 5 6 7 8 9 10 11 12``````
``````// Include header file
#include <iostream>
using namespace std;

/*
C++ program for
Sort an array which contains 1 to n values
*/
class Sorting
{
public:
// Display array elements
void displayElement(int arr[], int n)
{
for (int i = 0; i < n; ++i)
{
cout << " " << arr[i];
}
}
void sortElement(int arr[], int n)
{
// Executes the loop through by size of array
for (int i = 0; i < n; ++i)
{
if (arr[i] < 1 || arr[i] > n)
{
// Element not between 1..n
return;
}
// Set sort element
arr[i] = i + 1;
}
}
};
int main()
{
// Input array elements
int arr[] = {
4 , 10 , 12 , 1 , 2 , 7 , 11 , 5 , 8 , 9 , 6 , 3
};
// Get the number of elements
int n = sizeof(arr) / sizeof(arr[0]);
cout << "\n Before Sort : ";
cout << "\n After Sort : ";
return 0;
}``````

#### Output

`````` Before Sort :  4 10 12 1 2 7 11 5 8 9 6 3
After Sort :  1 2 3 4 5 6 7 8 9 10 11 12``````
``````// Include namespace system
using System;
/*
Csharp program for
Sort an array which contains 1 to n values
*/
public class Sorting
{
// Display array elements
public void displayElement(int[] arr, int n)
{
for (int i = 0; i < n; ++i)
{
Console.Write(" " + arr[i]);
}
}
public void sortElement(int[] arr, int n)
{
// Executes the loop through by size of array
for (int i = 0; i < n; ++i)
{
if (arr[i] < 1 || arr[i] > n)
{
// Element not between 1..n
return;
}
// Set sort element
arr[i] = i + 1;
}
}
public static void Main(String[] args)
{
// Input array elements
int[] arr = {
4 , 10 , 12 , 1 , 2 , 7 , 11 , 5 , 8 , 9 , 6 , 3
};
// Get the number of elements
int n = arr.Length;
Console.Write("\n Before Sort : ");
Console.Write("\n After Sort : ");
}
}``````

#### Output

`````` Before Sort :  4 10 12 1 2 7 11 5 8 9 6 3
After Sort :  1 2 3 4 5 6 7 8 9 10 11 12``````
``````package main
import "fmt"
/*
Go program for
Sort an array which contains 1 to n values
*/

// Display array elements
func displayElement(arr[] int, n int) {
for i := 0 ; i < n ; i++ {
fmt.Print(" ", arr[i])
}
}
func sortElement(arr[] int, n int) {
// Executes the loop through by size of array
for i := 0 ; i < n ; i++ {
if arr[i] < 1 || arr[i] > n {
// Element not between 1..n
return
}
// Set sort element
arr[i] = i + 1
}
}
func main() {

// Input array elements
var arr = [] int { 4 , 10 , 12 , 1 , 2 , 7 , 11 , 5 , 8 , 9 , 6 , 3 }
// Get the number of elements
var n int = len(arr)
fmt.Print("\n Before Sort : ")
displayElement(arr, n)
sortElement(arr, n)
fmt.Print("\n After Sort : ")
displayElement(arr, n)
}``````

#### Output

`````` Before Sort :  4 10 12 1 2 7 11 5 8 9 6 3
After Sort :  1 2 3 4 5 6 7 8 9 10 11 12``````
``````<?php
/*
Php program for
Sort an array which contains 1 to n values
*/
class Sorting
{
// Display array elements
public	function displayElement(\$arr, \$n)
{
for (\$i = 0; \$i < \$n; ++\$i)
{
echo(" ".\$arr[\$i]);
}
}
public	function sortElement(&\$arr, \$n)
{
// Executes the loop through by size of array
for (\$i = 0; \$i < \$n; ++\$i)
{
if (\$arr[\$i] < 1 || \$arr[\$i] > \$n)
{
// Element not between 1..n
return;
}
// Set sort element
\$arr[\$i] = \$i + 1;
}
}
}

function main()
{
// Input array elements
\$arr = array(4, 10, 12, 1, 2, 7, 11, 5, 8, 9, 6, 3);
// Get the number of elements
\$n = count(\$arr);
echo("\n Before Sort : ");
echo("\n After Sort : ");
}
main();``````

#### Output

`````` Before Sort :  4 10 12 1 2 7 11 5 8 9 6 3
After Sort :  1 2 3 4 5 6 7 8 9 10 11 12``````
``````/*
Node JS program for
Sort an array which contains 1 to n values
*/
class Sorting
{
// Display array elements
displayElement(arr, n)
{
for (var i = 0; i < n; ++i)
{
process.stdout.write(" " + arr[i]);
}
}
sortElement(arr, n)
{
// Executes the loop through by size of array
for (var i = 0; i < n; ++i)
{
if (arr[i] < 1 || arr[i] > n)
{
// Element not between 1..n
return;
}
// Set sort element
arr[i] = i + 1;
}
}
}

function main()
{
// Input array elements
var arr = [4, 10, 12, 1, 2, 7, 11, 5, 8, 9, 6, 3];
// Get the number of elements
var n = arr.length;
process.stdout.write("\n Before Sort : ");
process.stdout.write("\n After Sort : ");
}
main();``````

#### Output

`````` Before Sort :  4 10 12 1 2 7 11 5 8 9 6 3
After Sort :  1 2 3 4 5 6 7 8 9 10 11 12``````
``````#  Python 3 program for
#  Sort an array which contains 1 to n values
class Sorting :
#  Display list elements
def displayElement(self, arr, n) :
i = 0
while (i < n) :
print(" ", arr[i], end = "")
i += 1

def sortElement(self, arr, n) :
i = 0
#  Executes the loop through by size of list
while (i < n) :
if (arr[i] < 1 or arr[i] > n) :
#  Element not between 1..n
return

#  Set sort element
arr[i] = i + 1
i += 1

def main() :
#  Input list elements
arr = [4, 10, 12, 1, 2, 7, 11, 5, 8, 9, 6, 3]
#  Get the number of elements
n = len(arr)
print("\n Before Sort : ", end = "")
print("\n After Sort : ", end = "")

if __name__ == "__main__": main()``````

#### Output

`````` Before Sort :   4  10  12  1  2  7  11  5  8  9  6  3
After Sort :   1  2  3  4  5  6  7  8  9  10  11  12``````
``````#  Ruby program for
#  Sort an array which contains 1 to n values
class Sorting
#  Display array elements
def displayElement(arr, n)
i = 0
while (i < n)
print(" ", arr[i])
i += 1
end

end

def sortElement(arr, n)
i = 0
#  Executes the loop through by size of array
while (i < n)
if (arr[i] < 1 || arr[i] > n)
#  Element not between 1..n
return
end

#  Set sort element
arr[i] = i + 1
i += 1
end

end

end

def main()
#  Input array elements
arr = [4, 10, 12, 1, 2, 7, 11, 5, 8, 9, 6, 3]
#  Get the number of elements
n = arr.length
print("\n Before Sort : ")
print("\n After Sort : ")
end

main()``````

#### Output

`````` Before Sort :  4 10 12 1 2 7 11 5 8 9 6 3
After Sort :  1 2 3 4 5 6 7 8 9 10 11 12``````
``````/*
Scala program for
Sort an array which contains 1 to n values
*/
class Sorting()
{
// Display array elements
def displayElement(arr: Array[Int], n: Int): Unit = {
var i: Int = 0;
while (i < n)
{
print(" " + arr(i));
i += 1;
}
}
def sortElement(arr: Array[Int], n: Int): Unit = {
var i: Int = 0;
// Executes the loop through by size of array
while (i < n)
{
if (arr(i) < 1 || arr(i) > n)
{
// Element not between 1..n
return;
}
// Set sort element
arr(i) = i + 1;
i += 1;
}
}
}
object Main
{
def main(args: Array[String]): Unit = {
var task: Sorting = new Sorting();
// Input array elements
var arr: Array[Int] = Array(4, 10, 12, 1, 2, 7, 11, 5, 8, 9, 6, 3);
// Get the number of elements
var n: Int = arr.length;
print("\n Before Sort : ");
print("\n After Sort : ");
}
}``````

#### Output

`````` Before Sort :  4 10 12 1 2 7 11 5 8 9 6 3
After Sort :  1 2 3 4 5 6 7 8 9 10 11 12``````
``````import Foundation;
/*
Swift 4 program for
Sort an array which contains 1 to n values
*/
class Sorting
{
// Display array elements
func displayElement(_ arr: [Int], _ n: Int)
{
var i: Int = 0;
while (i < n)
{
print(" ", arr[i], terminator: "");
i += 1;
}
}
func sortElement(_ arr: inout[Int], _ n: Int)
{
var i: Int = 0;
// Executes the loop through by size of array
while (i < n)
{
if (arr[i] < 1 || arr[i] > n)
{
// Element not between 1..n
return;
}
// Set sort element
arr[i] = i + 1;
i += 1;
}
}
}
func main()
{
// Input array elements
var arr: [Int] = [4, 10, 12, 1, 2, 7, 11, 5, 8, 9, 6, 3];
// Get the number of elements
let n: Int = arr.count;
print("\n Before Sort : ", terminator: "");
print("\n After Sort : ", terminator: "");
}
main();``````

#### Output

`````` Before Sort :   4  10  12  1  2  7  11  5  8  9  6  3
After Sort :   1  2  3  4  5  6  7  8  9  10  11  12``````
``````/*
Kotlin program for
Sort an array which contains 1 to n values
*/
class Sorting
{
// Display array elements
fun displayElement(arr: Array < Int > , n: Int): Unit
{
var i: Int = 0;
while (i < n)
{
print(" " + arr[i]);
i += 1;
}
}
fun sortElement(arr: Array < Int > , n: Int): Unit
{
var i: Int = 0;
// Executes the loop through by size of array
while (i < n)
{
if (arr[i] < 1 || arr[i] > n)
{
// Element not between 1..n
return;
}
// Set sort element
arr[i] = i + 1;
i += 1;
}
}
}
fun main(args: Array < String > ): Unit
{
// Input array elements
val arr: Array < Int > = arrayOf(4, 10, 12, 1, 2, 7, 11, 5, 8, 9, 6, 3);
// Get the number of elements
val n: Int = arr.count();
print("\n Before Sort : ");
print("\n After Sort : ");
}``````

#### Output

`````` Before Sort :  4 10 12 1 2 7 11 5 8 9 6 3
After Sort :  1 2 3 4 5 6 7 8 9 10 11 12``````

## Comment

Please share your knowledge to improve code and content standard. Also submit your doubts, and test case. We improve by your feedback. We will try to resolve your query as soon as possible.