📜  交换两个数字的Java程序

📅  最后修改于: 2022-05-13 01:55:00.896000             🧑  作者: Mango

交换两个数字的Java程序

问题陈述:给定两个整数 m 和 n。目标只是在内存块中交换它们的值并编写演示方法的Java代码。

插图:

Input  : m=9, n=5
Output : m=5, n=9
 
Input  : m=15, n=5
Output : m=5, n=15
Here 'm' and 'n' are integer value

方法:

有 3 种标准方法可以根据空间和时间复杂度来交换数字。

  1. 在内存中创建一个辅助内存单元。
  2. 无需创建任何辅助(附加)存储单元
  3. 使用异或(按位异或)运算符

下面按照与上面列出的顺序相同的顺序单独描述方法:

方法 1:使用第三个变量交换值

将在内存堆栈区域中占用相同内存的相同类型的内存中创建一个内存单元。在执行期间,它保留一个值以替换其他值,一旦完成所需的执行,其值将分配给已经存在的第二个变量。一旦变量的范围是三个变量,就会从内存单元中释放出来。这个变量被称为临时变量或有时被称为催化剂,因为在输出中的参与甚至没有被追踪,但执行将停止以产生上面目睹的预期结果。

Java
// Java Program to Swap Two values using third variable
// using temp variable
 
// Importing generic libraries
import java.util.*;
 
class GFG {
 
    // Function to swap two numbers
    // Using temporary variable
    static void swapValuesUsingThirdVariable(int m, int n)
    {
        // Swapping the values
        int temp = m;
        m = n;
        n = temp;
        System.out.println("Value of m is " + m
                           + " and Value of n is " + n);
    }
 
    // Main driver code
    public static void main(String[] args)
    {
        // Random integerslues
        int m = 9, n = 5;
 
        // Calling above function to
        // reverse the numbers
        swapValuesUsingThirdVariable(m, n);
    }
}


Java
// Java Program to swap the two values
// without using third variable
 
// Importing generic Java libraries
import java.util.*;
 
class GFG {
 
    // Function to swap values of two numbers
    // without creating temp variable
    static void swapValuesWithoutUsingThirdVariable(int m,
                                                    int n)
    {
        // Steps as listed in algorithm
 
        // Difference of 2nd from 1st
        // is stored in first variable
        m = m - n;
 
        // Sum is stored in second variable
        n = m + n;
 
        // Difference of 1st from 2nd
        // is replaced in first variable
        m = n - m;
 
        // Print numbers
        System.out.println("Value of m is " + m
                           + " and Value of n is " + n);
    }
 
    //  Main driver method
    public static void main(String[] args)
    {
        // Random numbers of integer type
        int m = 9, n = 5;
 
        // Above function is called in main
        // to print swapped values of numbers
        swapValuesWithoutUsingThirdVariable(m, n);
    }
}


Java
// Java Program to swap the two values
// using XOR Operator
 
// Importing generic Java libraries
import java.io.*;
 
class GFG {
 
    // Function to swap values of two numbers
    // using XOR operator
    static void swapValuesUsingXOROperator(int m, int n)
    {
        // Logic of XOR operator
        m = m ^ n;
        n = m ^ n;
        m = m ^ n;
 
        System.out.println("Value of m is " + m
                           + " and Value of n is " + n);
    }
 
    // Main driver method
    public static void main(String[] args)
    {
        // Random two integer numbers
        // to get swapped
        int m = 9, n = 5;
 
        // Calling the function in main method
        // to get above integer numbers swapped
        swapValuesUsingXOROperator(m, n);
    }
}


输出
Value of m is 5 and Value of n is 9

方法 2:使用数学的求和和差概念在不使用第三变量的情况下交换值。

算法:有 3 个标准步骤,如下所示:

  1. 第二个数字与第一个数字的差值存储在第一个数字已经存储的存储单元中。
  2. 两个数字的总和存储在第二个存储单元(数字)中。
  3. 计算第一个数字与第二个数字的差值并将其存储在存储初始第一个值的存储单元中。

下面是交换数字而不在内存中创建任何辅助空间的实现:

Java

// Java Program to swap the two values
// without using third variable
 
// Importing generic Java libraries
import java.util.*;
 
class GFG {
 
    // Function to swap values of two numbers
    // without creating temp variable
    static void swapValuesWithoutUsingThirdVariable(int m,
                                                    int n)
    {
        // Steps as listed in algorithm
 
        // Difference of 2nd from 1st
        // is stored in first variable
        m = m - n;
 
        // Sum is stored in second variable
        n = m + n;
 
        // Difference of 1st from 2nd
        // is replaced in first variable
        m = n - m;
 
        // Print numbers
        System.out.println("Value of m is " + m
                           + " and Value of n is " + n);
    }
 
    //  Main driver method
    public static void main(String[] args)
    {
        // Random numbers of integer type
        int m = 9, n = 5;
 
        // Above function is called in main
        // to print swapped values of numbers
        swapValuesWithoutUsingThirdVariable(m, n);
    }
}
输出
Value of m is 5 and Value of n is 9

方法 3:使用运算符交换值

按位运算符用于对数字的各个位进行操作。它们可以与任何整数类型(char、short、int 等)一起使用。它们在执行二叉索引树的更新和查询操作时使用。

这个运算符是二元运算符,用'^'表示。它返回输入值的逐位异或,即如果对应的位不同,则返回 1,否则返回 0。

插图:

a = 5 = 0101 (In Binary)
b = 7 = 0111 (In Binary)

Bitwise XOR Operation of 5 and 7
  0101
^ 0111
 ________
  0010  = 2 (In decimal)

这是最优化的方法,因为这里直接计算是通过位而不是字节进行的,如上述两种方法中所见。这是一个显示内部工作的Java程序——

Java

// Java Program to swap the two values
// using XOR Operator
 
// Importing generic Java libraries
import java.io.*;
 
class GFG {
 
    // Function to swap values of two numbers
    // using XOR operator
    static void swapValuesUsingXOROperator(int m, int n)
    {
        // Logic of XOR operator
        m = m ^ n;
        n = m ^ n;
        m = m ^ n;
 
        System.out.println("Value of m is " + m
                           + " and Value of n is " + n);
    }
 
    // Main driver method
    public static void main(String[] args)
    {
        // Random two integer numbers
        // to get swapped
        int m = 9, n = 5;
 
        // Calling the function in main method
        // to get above integer numbers swapped
        swapValuesUsingXOROperator(m, n);
    }
}
输出
Value of m is 5 and Value of n is 9