Skip to main content

Sort array elements using builtin method

Here given code implementation process.

/*
   Java Program for
   Sort array elements using inbuilt method
*/
import java.util.Arrays;
public class SortArray
{
    // Display array elements
	public void display(int[] arr, int n)
	{
		for (int i = 0; i < n; ++i)
		{
			System.out.print(" " + arr[i]);
		}
		System.out.print("\n");
	}
	public static void main(String[] args)
	{
		SortArray task = new SortArray();
		int[] arr = {
			6 , 2 , -9 , 1 , 2 , 5 , 3
		};
		int n = arr.length;
		System.out.print("Before sort : ");
		task.display(arr, n);
		// Sort element
		Arrays.sort(arr);
		System.out.print("After sort : ");
		task.display(arr, n);
	}
}

input

Before sort :  6 2 -9 1 2 5 3
After sort :  -9 1 2 2 3 5 6
// Include header file
#include <iostream>
#include <algorithm>
using namespace std;
class SortArray
{
	public:
		// Display array elements
		void display(int arr[], int n)
		{
			for (int i = 0; i < n; ++i)
			{
				cout << " " << arr[i];
			}
			cout << "\n";
		}
};
int main()
{
	SortArray *task = new SortArray();
	int arr[] = {
		6 , 2 , -9 , 1 , 2 , 5 , 3
	};
	int n = sizeof(arr) / sizeof(arr[0]);
	cout << "Before sort : ";
	task->display(arr, n);
	// Sort element
	sort(arr, arr + n);
	cout << "After sort : ";
	task->display(arr, n);
	return 0;
}

input

Before sort :  6 2 -9 1 2 5 3
After sort :  -9 1 2 2 3 5 6
// Include namespace system
using System;
public class SortArray
{
	// Display array elements
	public void display(int[] arr, int n)
	{
		for (int i = 0; i < n; ++i)
		{
			Console.Write(" " + arr[i]);
		}
		Console.Write("\n");
	}
	public static void Main(String[] args)
	{
		SortArray task = new SortArray();
		int[] arr = {
			6 , 2 , -9 , 1 , 2 , 5 , 3
		};
		int n = arr.Length;
		Console.Write("Before sort : ");
		task.display(arr, n);
		// Sort element
		Array.Sort(arr);
		Console.Write("After sort : ");
		task.display(arr, n);
	}
}

input

Before sort :  6 2 -9 1 2 5 3
After sort :  -9 1 2 2 3 5 6
<?php
class SortArray
{
	// Display array elements
	public	function display($arr, $n)
	{
		for ($i = 0; $i < $n; ++$i)
		{
			echo(" ".$arr[$i]);
		}
		echo("\n");
	}
}

function main()
{
	$task = new SortArray();
	$arr = array(6, 2, -9, 1, 2, 5, 3);
	$n = count($arr);
	echo("Before sort : ");
	$task->display($arr, $n);
	// Sort element
	sort($arr);
	echo("After sort : ");
	$task->display($arr, $n);
}
main();

input

Before sort :  6 2 -9 1 2 5 3
After sort :  -9 1 2 2 3 5 6
/*
   Node JS Program for
   Sort array elements using inbuilt method
*/
class SortArray
{
	// Display array elements
	display(arr, n)
	{
		for (var i = 0; i < n; ++i)
		{
			process.stdout.write(" " + arr[i]);
		}
		process.stdout.write("\n");
	}
}

function main()
{
	var task = new SortArray();
	var arr = [6, 2, -9, 1, 2, 5, 3];
	var n = arr.length;
	process.stdout.write("Before sort : ");
	task.display(arr, n);
	// Sort element
	arr.sort(function(a, b)
	{
		return a - b;
	});
	process.stdout.write("After sort : ");
	task.display(arr, n);
}
main();

input

Before sort :  6 2 -9 1 2 5 3
After sort :  -9 1 2 2 3 5 6
#   Python 3 Program for
#   Sort array elements using inbuilt method
class SortArray :
	#  Display list elements
	def display(self, arr, n) :
		i = 0
		while (i < n) :
			print(" ", arr[i], end = "")
			i += 1
		
		print(end = "\n")
	

def main() :
	task = SortArray()
	arr = [6, 2, -9, 1, 2, 5, 3]
	n = len(arr)
	print("Before sort : ", end = "")
	task.display(arr, n)
	#  Sort element
	arr.sort()
	print("After sort : ", end = "")
	task.display(arr, n)

if __name__ == "__main__": main()

input

Before sort :   6  2  -9  1  2  5  3
After sort :   -9  1  2  2  3  5  6
#   Ruby Program for
#   Sort array elements using inbuilt method
class SortArray 
	#  Display array elements
	def display(arr, n) 
		i = 0
		while (i < n) 
			print(" ", arr[i])
			i += 1
		end

		print("\n")
	end

end

def main() 
	task = SortArray.new()
	arr = [6, 2, -9, 1, 2, 5, 3]
	n = arr.length
	print("Before sort : ")
	task.display(arr, n)
	#  Sort element
	arr = arr.sort
	print("After sort : ")
	task.display(arr, n)
end

main()

input

Before sort :  6 2 -9 1 2 5 3
After sort :  -9 1 2 2 3 5 6
import scala.collection.mutable._;
/*
   Scala Program for
   Sort array elements using inbuilt method
*/
class SortArray()
{
	// Display array elements
	def display(arr: Array[Int], n: Int): Unit = {
		var i: Int = 0;
		while (i < n)
		{
			print(" " + arr(i));
			i += 1;
		}
		print("\n");
	}
}
object Main
{
	def main(args: Array[String]): Unit = {
		var task: SortArray = new SortArray();
		var arr: Array[Int] = Array(6, 2, -9, 1, 2, 5, 3);
		var n: Int = arr.length;
		print("Before sort : ");
		task.display(arr, n);
		// Sort element
		arr = arr.sorted;
		print("After sort : ");
		task.display(arr, n);
	}
}

input

Before sort :  6 2 -9 1 2 5 3
After sort :  -9 1 2 2 3 5 6
import Foundation;
/*
   Swift 4 Program for
   Sort array elements using inbuilt method
*/
class SortArray
{
	// Display array elements
	func display(_ arr: [Int], _ n: Int)
	{
		var i = 0;
		while (i < n)
		{
			print(" ", arr[i], terminator: "");
			i += 1;
		}
		print(terminator: "\n");
	}
}
func main()
{
	let task = SortArray();
	var arr = [6, 2, -9, 1, 2, 5, 3];
	let n = arr.count;
	print("Before sort : ", terminator: "");
	task.display(arr, n);
	// Sort element
	arr = arr.sorted();
	print("After sort : ", terminator: "");
	task.display(arr, n);
}
main();

input

Before sort :   6  2  -9  1  2  5  3
After sort :   -9  1  2  2  3  5  6
/*
   Kotlin Program for
   Sort array elements using inbuilt method
*/
class SortArray
{
	// Display array elements
	fun display(arr: Array < Int > , n: Int): Unit
	{
		var i: Int = 0;
		while (i < n)
		{
			print(" " + arr[i]);
			i += 1;
		}
		print("\n");
	}
}
fun main(args: Array < String > ): Unit
{
	val task: SortArray = SortArray();
	var arr: Array < Int > = arrayOf(6, 2, -9, 1, 2, 5, 3);
	val n: Int = arr.count();
	print("Before sort : ");
	task.display(arr, n);
	// Sort element
	arr.sort();
	print("After sort : ");
	task.display(arr, n);
}

input

Before sort :  6 2 -9 1 2 5 3
After sort :  -9 1 2 2 3 5 6
package main
import "sort"
import "fmt"
/*
   Go Program for
   Sort array elements using inbuilt method
*/

func main() {
	
	var arr = [] int {
		6,
		2,
		-9,
		1,
		2,
		5,
		3,
	}
	fmt.Print("Before sort : ")
	fmt.Println(arr)
	// Sort element
	sort.Ints(arr)
	fmt.Print("After sort : ")
	fmt.Println(arr)
}

input

Before sort : [6 2 -9 1 2 5 3]
After sort : [-9 1 2 2 3 5 6]




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.

New Comment