📌  相关文章
📜  Java程序以升序对数组的元素进行排序

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

Java程序以升序对数组的元素进行排序

问题陈述:将给定的数组按升序排序,使元素从小到大排列。结合下图:

让原始数组如下:

-5-981213

对上述数组排序后生成的数组如下

-9-513812

  • 元素的排序方式是最小的元素将出现在最左边,在这种情况下是 -9。最大的元素将出现在最右边,在这种情况下是 12。

方法:

  1. 使用冒泡排序(天真)
  2. 使用数组类的 sort() 方法(最优)

方法一:使用冒泡排序

算法:

  1. 将相邻元素相互比较。
  2. 使用嵌套的 for 循环来跟踪。
  3. 如果第一个元素大于第二个元素,则交换元素。

例子

Java
// Java Program to Sort Elements of an Array
// in Ascending Order
  
// Main class
class GFG {
  
    // Declaration global variable length
    static int length;
  
    // Method 1
    // To print the array
    public static void printArray(int[] array)
    {
        // Iterating using for loops
        for (int i = 0; i < length; i++) {
            System.out.print(array[i] + " ");
        }
        System.out.println();
    }
  
    // Method 2
    // To sort an array
    public static void sortArray(int[] array)
    {
        int temporary = 0;
  
        // Sort the array 'arr' elements in ascending order
        // using nested for loops
        for (int i = 0; i < length; i++) {
            for (int j = i + 1; j < length; j++) {
                if (array[i] > array[j]) {
                    temporary = array[i];
                    array[i] = array[j];
                    array[j] = temporary;
                }
            }
        }
  
        // Displaying elements of array after sorting
        System.out.println(
            "Elements of array sorted in ascending order: ");
        printArray(array);
    }
  
    // Method 3
    // Main driver method
    public static void main(String[] args)
    {
        // Initializing custom array elements
        // The array contains 6 elements.
        int[] array = new int[] { -5, -9, 8, 12, 1, 3 };
  
        // Initialize length
        length = array.length;
  
        // Displaying elements of original array
        System.out.print("Elements of original array: ");
  
        // Call printArray method
        printArray(array);
  
        // Call sortArray method
        sortArray(array);
    }
}


Java
// Java Program to sort the elements of an array
// in Ascending Order by Inbuilt Methods
  
// Importing Arrays class from java.util package
import java.util.Arrays;
  
// Main class
public class GFG {
  
    // Main driver method
    public static void main(String[] args)
    {
        // Initialize array
        // The array contains 6 elements.
        int[] array = new int[] { -5, -9, 8, 12, 1, 3 };
  
        // Displaying elements of original array
        System.out.print("Elements of original array: ");
        for (int i = 0; i < array.length; i++) {
            System.out.print(array[i] + " ");
        }
  
        // Using Arrays.sort() method to sort array
        // elements in ascending order.
        Arrays.sort(array);
  
        System.out.println();
  
        // Displaying elements of array after sorting
        System.out.println(
            "Elements of array sorted in ascending order : "
            + Arrays.toString(array));
    }
}


输出
Elements of original array: -5 -9 8 12 1 3 
Elements of array sorted in ascending order: 
-9 -5 1 3 8 12 

方法二:使用Arrays类的sort()方法

sort() 方法是一个Java.util.Arrays 类方法,用于对数组元素进行排序。默认情况下,它按升序对数组元素进行排序。

句法:

Arrays.sort(arrayName);

参数:要排序的数组

返回类型:不适用

例子

Java

// Java Program to sort the elements of an array
// in Ascending Order by Inbuilt Methods
  
// Importing Arrays class from java.util package
import java.util.Arrays;
  
// Main class
public class GFG {
  
    // Main driver method
    public static void main(String[] args)
    {
        // Initialize array
        // The array contains 6 elements.
        int[] array = new int[] { -5, -9, 8, 12, 1, 3 };
  
        // Displaying elements of original array
        System.out.print("Elements of original array: ");
        for (int i = 0; i < array.length; i++) {
            System.out.print(array[i] + " ");
        }
  
        // Using Arrays.sort() method to sort array
        // elements in ascending order.
        Arrays.sort(array);
  
        System.out.println();
  
        // Displaying elements of array after sorting
        System.out.println(
            "Elements of array sorted in ascending order : "
            + Arrays.toString(array));
    }
}
输出
Elements of original array: -5 -9 8 12 1 3 
Elements of array sorted in ascending order : [-9, -5, 1, 3, 8, 12]