# Sort 3 numbers

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}
```

## Recommended: Please try your approach on {IDE} first, before moving on to the solution.

One simple solution is to use sort function.

## C++

 `// C++ program to sort an array of size 3 ` `#include ` `#include ` `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

 `// Java program to sort ` `// an array of size 3 ` `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] + ``" "``); ` `    ``} ` `} ` ` `  `// This code is contributed ` `// by inder_verma. `

## Python3

 `# Python3 program to sort ` `# an array of size 3 ` `a ``=` `[``10``, ``12``, ``5``] ` `a.sort() ` `for` `i ``in` `range``(``len``(a)): ` `    ``print``(a[i], end ``=` `' '``) ` ` `  `# This code is contributed ` `# by Samyukta S Hegde `

## C#

 `// C# program to sort ` `// an array of size 3 ` `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] + ``" "``); ` `    ``} ` `} ` ` `  `// This code is contributed  ` `// by chandan_jnu. `

## PHP

 ` `

Output:

```5 10 12
```

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++

 `// C++ program to sort an array of size 3 ` `#include ` `#include ` `using` `namespace` `std; ` ` `  `int` `sort3(``int` `arr[]) ` `{ ` `    ``// Insert arr ` `    ``if` `(arr < arr) ` `       ``swap(arr, arr); ` ` `  `    ``// Insert arr ` `    ``if` `(arr < arr) ` `    ``{ ` `       ``swap(arr, arr); ` `       ``if` `(arr < arr) ` `          ``swap(arr, arr); ` `    ``} ` `} ` ` `  `int` `main() ` `{  ` `    ``int` `a[] = {10, 12, 5}; ` `     `  `    ``sort3(a); ` ` `  `    ``for` `(``int` `i = 0; i < 3; i++)  ` `        ``cout << a[i] << ``" "``; ` ` `  `    ``return` `0; ` `} `

## Java

 `// Java program to sort  ` `// an array of size 3 ` `import` `java.io.*; ` `import` `java.util.*; ` ` `  `class` `GFG ` `{ ` `static` `void` `sort3(``int` `arr[], ` `                  ``int` `temp[]) ` `{ ` `    ``// Insert arr ` `    ``if` `(arr[``1``] < arr[``0``]) ` `        ``{ ` `            ``temp[``0``] = arr[``0``]; ` `            ``arr[``0``] = arr[``1``]; ` `            ``arr[``1``] = temp[``0``]; ` `        ``} ` ` `  `    ``// Insert arr ` `    ``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``]; ` `        ``} ` `    ``} ` `} ` ` `  `// Driver Code ` `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] + ``" "``); ` `} ` `} ` ` `  `// This code is contributed ` `// by Akanksha Rai(Abby_akku) `

## C#

 `// C# program to sort  ` `// an array of size 3 ` `using` `System; ` ` `  `class` `GFG { ` `     `  `static` `void` `sort3(``int` `[]arr, ``int` `[]temp) ` `{ ` `     `  `    ``// Insert arr ` `    ``if` `(arr < arr) ` `        ``{ ` `            ``temp = arr; ` `            ``arr = arr; ` `            ``arr = temp; ` `        ``} ` ` `  `    ``// Insert arr ` `    ``if` `(arr < arr) ` `    ``{ ` `        ``temp = arr; ` `        ``arr = arr; ` `        ``arr = temp; ` `             `  `        ``if` `(arr < arr) ` `        ``{ ` `            ``temp = arr; ` `            ``arr = arr; ` `            ``arr = temp; ` `        ``} ` `    ``} ` `} ` ` `  `    ``// Driver Code ` `    ``public` `static` `void` `Main(String []args) ` `    ``{  ` `        ``int` `[]a= ``new` `int``[]{10, 12, 5}; ` `        ``int` `[]temp1 = ``new` `int``; ` `        ``sort3(a, temp1); ` `     `  `        ``for` `(``int` `i = 0; i < 3; i++)  ` `            ``Console.Write( a[i] + ``" "``); ` `    ``} ` `} ` ` `  `// This code is contributed ` `// by Akanksha Rai(Abby_akku) `

## PHP

 ` `

Output:

```5 10 12
```

My Personal Notes arrow_drop_up Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.

Article Tags :
Practice Tags :

Be the First to upvote.

Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.