Sort 3 numbers
Last Updated :
23 Jun, 2022
Given three numbers, how to sort them?
Examples:
Input : arr[] = {3, 2, 1}
Output : arr[] = {1, 2, 3}
Input : arr[] = {6, 5, 0}
Output :arr[] = {0, 5, 6}
One simple solution is to use sort function.
C++
#include <algorithm>
#include <iostream>
using namespace std;
int main()
{
int a[] = {10, 12, 5};
sort(a, a + 3);
for ( int i = 0; i < 3; i++)
cout << a[i] << " " ;
return 0;
}
|
Java
import java.io.*;
import java .util.*;
class GFG
{
public static void main (String[] args)
{
int a[] = { 10 , 12 , 5 };
Arrays.sort(a);
for ( int i = 0 ; i < 3 ; i++)
System.out.print( a[i] + " " );
}
}
|
Python3
a = [ 10 , 12 , 5 ]
a.sort()
for i in range ( len (a)):
print (a[i], end = ' ' )
|
C#
using System;
class GFG
{
public static void Main ()
{
int []a = {10, 12, 5};
Array.Sort(a);
for ( int i = 0; i < 3; i++)
Console.Write( a[i] + " " );
}
}
|
PHP
<?php
$a = array (10, 12, 5);
sort( $a );
for ( $i = 0; $i < 3; $i ++)
echo $a [ $i ] , " " ;
?>
|
Javascript
<script>
let arr = [10, 12, 5];
arr.sort((a, b) => a - b);
for (let i = 0; i < 3; i++)
document.write(arr[i] + " " );
</script>
|
Time Complexity: O(1)
Auxiliary Space: O(1)
How to write our own sort function that does minimum comparison and does not use extra variables?
The idea is to use insertion sort as insertion sort works best for small arrays.
C++
#include <algorithm>
#include <iostream>
using namespace std;
int sort3( int arr[])
{
if (arr[1] < arr[0])
swap(arr[0], arr[1]);
if (arr[2] < arr[1])
{
swap(arr[1], arr[2]);
if (arr[1] < arr[0])
swap(arr[1], arr[0]);
}
}
int main()
{
int a[] = {10, 12, 5};
sort3(a);
for ( int i = 0; i < 3; i++)
cout << a[i] << " " ;
return 0;
}
|
Java
import java.io.*;
import java.util.*;
class GFG
{
static void sort3( int arr[],
int temp[])
{
if (arr[ 1 ] < arr[ 0 ])
{
temp[ 0 ] = arr[ 0 ];
arr[ 0 ] = arr[ 1 ];
arr[ 1 ] = temp[ 0 ];
}
if (arr[ 2 ] < arr[ 1 ])
{
temp[ 0 ] = arr[ 1 ];
arr[ 1 ] = arr[ 2 ];
arr[ 2 ] = temp[ 0 ];
if (arr[ 1 ] < arr[ 0 ])
{
temp[ 0 ] = arr[ 0 ];
arr[ 0 ] = arr[ 1 ];
arr[ 1 ] = temp[ 0 ];
}
}
}
public static void main(String args[])
{
int a[] = new int []{ 10 , 12 , 5 };
int temp1[] = new int [ 10 ];
sort3(a, temp1);
for ( int i = 0 ; i < 3 ; i++)
System.out.print( a[i] + " " );
}
}
|
Python3
def sort3(arr):
if (arr[ 1 ] < arr[ 0 ]):
arr[ 0 ], arr[ 1 ] = arr[ 1 ], arr[ 0 ]
if (arr[ 2 ] < arr[ 1 ]):
arr[ 1 ], arr[ 2 ] = arr[ 2 ], arr[ 1 ]
if (arr[ 1 ] < arr[ 0 ]):
arr[ 1 ], arr[ 0 ] = arr[ 0 ], arr[ 1 ]
a = [ 10 , 12 , 5 ]
sort3(a)
for i in range ( 3 ):
print (a[i],end = " " )
|
C#
using System;
class GFG {
static void sort3( int []arr, int []temp)
{
if (arr[1] < arr[0])
{
temp[0] = arr[0];
arr[0] = arr[1];
arr[1] = temp[0];
}
if (arr[2] < arr[1])
{
temp[0] = arr[1];
arr[1] = arr[2];
arr[2] = temp[0];
if (arr[1] < arr[0])
{
temp[0] = arr[0];
arr[0] = arr[1];
arr[1] = temp[0];
}
}
}
public static void Main(String []args)
{
int []a= new int []{10, 12, 5};
int []temp1 = new int [10];
sort3(a, temp1);
for ( int i = 0; i < 3; i++)
Console.Write( a[i] + " " );
}
}
|
PHP
<?php
function sort3(& $arr , $temp )
{
if ( $arr [1] < $arr [0])
{
$temp [0] = $arr [0];
$arr [0] = $arr [1];
$arr [1] = $temp [0];
}
if ( $arr [2] < $arr [1])
{
$temp [0] = $arr [1];
$arr [1] = $arr [2];
$arr [2] = $temp [0];
}
if ( $arr [1] < $arr [0])
{
$temp [0] = $arr [0];
$arr [0] = $arr [1];
$arr [1] = $temp [0];
}
}
$a = array (10, 12, 5);
$temp1 = array (10);
sort3( $a , $temp1 );
for ( $i = 0; $i < 3; $i ++)
echo ( $a [ $i ] . " " );
?>
|
Javascript
<script>
function sort3(arr, temp)
{
if (arr[1] < arr[0])
{
temp[0] = arr[0];
arr[0] = arr[1];
arr[1] = temp[0];
}
if (arr[2] < arr[1])
{
temp[0] = arr[1];
arr[1] = arr[2];
arr[2] = temp[0];
if (arr[1] < arr[0])
{
temp[0] = arr[0];
arr[0] = arr[1];
arr[1] = temp[0];
}
}
}
let a = [ 10, 12, 5 ];
let temp1 = [10];
sort3(a, temp1);
for (let i = 0; i < 3; i++)
document.write( a[i] + " " );
</script>
|
Time Complexity: O(1)
Auxiliary Space: O(1)
Like Article
Suggest improvement
Share your thoughts in the comments
Please Login to comment...