📜  排序3个数字

📅  最后修改于: 2021-04-29 13:16:48             🧑  作者: Mango

给定三个数字,如何对它们进行排序?

例子

Input  : arr[] = {3, 2, 1}
Output : arr[] = {1, 2, 3}

Input : arr[] = {6, 5, 0}
Output :arr[] = {0, 5, 6}

一种简单的解决方案是使用sort函数。

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


C++
// C++ program to sort an array of size 3
#include 
#include 
using namespace std;
  
int sort3(int arr[])
{
    // Insert arr[1]
    if (arr[1] < arr[0])
       swap(arr[0], arr[1]);
  
    // Insert arr[2]
    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
// 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[1]
    if (arr[1] < arr[0])
        {
            temp[0] = arr[0];
            arr[0] = arr[1];
            arr[1] = temp[0];
        }
  
    // Insert arr[2]
    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)


Python3
# Python3 program to sort an array of size 3
def sort3(arr):
      
    # Insert arr[1]
    if (arr[1] < arr[0]):
        arr[0], arr[1] = arr[1], arr[0]
          
    # Insert arr[2]
    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]
      
# Driver code
a = [10, 12, 5]
  
sort3(a)
  
for i in range(3):
    print(a[i],end=" ")
      
# This code is contributed by shubhamsingh10


C#
// C# program to sort 
// an array of size 3
using System;
  
class GFG {
      
static void sort3(int []arr, int []temp)
{
      
    // Insert arr[1]
    if (arr[1] < arr[0])
        {
            temp[0] = arr[0];
            arr[0] = arr[1];
            arr[1] = temp[0];
        }
  
    // Insert arr[2]
    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++) 
            Console.Write( a[i] + " ");
    }
}
  
// This code is contributed
// by Akanksha Rai(Abby_akku)


PHP


输出:
5 10 12


如何编写我们自己的排序函数,该函数执行最小限度的比较并且不使用额外的变量?

这个想法是使用插入排序,因为插入排序最适合小型数组。

C++

// C++ program to sort an array of size 3
#include 
#include 
using namespace std;
  
int sort3(int arr[])
{
    // Insert arr[1]
    if (arr[1] < arr[0])
       swap(arr[0], arr[1]);
  
    // Insert arr[2]
    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

// 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[1]
    if (arr[1] < arr[0])
        {
            temp[0] = arr[0];
            arr[0] = arr[1];
            arr[1] = temp[0];
        }
  
    // Insert arr[2]
    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)

Python3

# Python3 program to sort an array of size 3
def sort3(arr):
      
    # Insert arr[1]
    if (arr[1] < arr[0]):
        arr[0], arr[1] = arr[1], arr[0]
          
    # Insert arr[2]
    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]
      
# Driver code
a = [10, 12, 5]
  
sort3(a)
  
for i in range(3):
    print(a[i],end=" ")
      
# This code is contributed by shubhamsingh10

C#

// C# program to sort 
// an array of size 3
using System;
  
class GFG {
      
static void sort3(int []arr, int []temp)
{
      
    // Insert arr[1]
    if (arr[1] < arr[0])
        {
            temp[0] = arr[0];
            arr[0] = arr[1];
            arr[1] = temp[0];
        }
  
    // Insert arr[2]
    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++) 
            Console.Write( a[i] + " ");
    }
}
  
// This code is contributed
// by Akanksha Rai(Abby_akku)

的PHP


输出:
5 10 12