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 header file
#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)
	{
		Sorting task = new Sorting();
		// 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 : ");
		task.displayElement(arr, n);
		task.sortElement(arr, n);
		System.out.print("\n After Sort : ");
		task.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
// 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()
{
	Sorting *task = new Sorting();
	// 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 : ";
	task->displayElement(arr, n);
	task->sortElement(arr, n);
	cout << "\n After Sort : ";
	task->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
// 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)
	{
		Sorting task = new Sorting();
		// 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 : ");
		task.displayElement(arr, n);
		task.sortElement(arr, n);
		Console.Write("\n After Sort : ");
		task.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
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()
{
	$task = new Sorting();
	// 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 : ");
	$task->displayElement($arr, $n);
	$task->sortElement($arr, $n);
	echo("\n After Sort : ");
	$task->displayElement($arr, $n);
}
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()
{
	var task = new Sorting();
	// 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 : ");
	task.displayElement(arr, n);
	task.sortElement(arr, n);
	process.stdout.write("\n After Sort : ");
	task.displayElement(arr, n);
}
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() :
	task = Sorting()
	#  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 = "")
	task.displayElement(arr, n)
	task.sortElement(arr, n)
	print("\n After Sort : ", end = "")
	task.displayElement(arr, n)

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() 
	task = Sorting.new()
	#  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 : ")
	task.displayElement(arr, n)
	task.sortElement(arr, n)
	print("\n After Sort : ")
	task.displayElement(arr, n)
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 : ");
		task.displayElement(arr, n);
		task.sortElement(arr, n);
		print("\n After Sort : ");
		task.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
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()
{
	let task: Sorting = Sorting();
	// 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: "");
	task.displayElement(arr, n);
	task.sortElement(&arr, n);
	print("\n After Sort : ", terminator: "");
	task.displayElement(arr, n);
}
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
{
	val task: Sorting = Sorting();
	// 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 : ");
	task.displayElement(arr, n);
	task.sortElement(arr, n);
	print("\n After Sort : ");
	task.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


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.

New Comment







© 2021, kalkicode.com, All rights reserved