Java Arrays.sort()与实例
数组类 是一个包含静态方法的类,这些方法用于数组的搜索、排序、比较、插入元素,或返回数组的字符串表示。所以,让我们先明确这些函数,以后我们再讨论这些函数。它们如下,存在于java.util.Arrays类中。这里我们将讨论使用Arrays类的sort()方法的不同图。
Arrays.sort()方法包括两种变化,一种是我们不传递任何参数,它对整个数组进行排序,不管是整数数组还是字符数组,但如果我们要使用Arrays类的这个方法对特定的部分进行排序,那么我们要对它进行重载,并传递数组的起始和最后索引。
语法: sort() 方法
Arrays.sort();
语法: 重载排序()方法
public static void sort(int[] arr, int from_Index, int to_Index) ;
参数: 从语法上可以看出,它需要三个参数,如下所示。
- 要排序的数组
- 要排序的第一个元素的索引,包括在内,(称为from_index)
- 被排序的最后一个元素的索引,不包括在内(称为last_index)。
返回类型: NA
复杂度分析
- 时间复杂度: O(N log N)
- 辅助空间: O(1)
现在让我们看看sort()函数在Arrays类的不同场景下的实现,如下。
例1 :
import java.util.Arrays;
class GFG {
public static void main(String args[])
{
int[] arr = { 5, -2, 23, 7, 87, -42, 509 };
System.out.println("The original array is: ");
for (int num : arr) {
System.out.print(num + " ");
}
Arrays.sort(arr);
System.out.println("\nThe sorted array is: ");
for (int num : arr) {
System.out.print(num + " ");
}
}
}
输出
The original array is:
5 -2 23 7 87 -42 509
The sorted array is:
-42 -2 5 7 23 87 509
时间复杂度: O(nlog(n)),因为它是arrays.sort()的复杂度
辅助空间: O(1)
例2 :
// Java Program to Sort Array of Integers
// by Default Sorts in an Ascending Order
// using Arrays.sort() Method
// Importing Arrays class from the utility class
import java.util.Arrays;
// Main class
public class GFG {
// Main driver method
public static void main(String[] args)
{
// Custom input array
int[] arr = { 13, 7, 6, 45, 21, 9, 101, 102 };
// Applying sort() method over to above array
// by passing the array as an argument
Arrays.sort(arr);
// Printing the array after sorting
System.out.println("Modified arr[] : "
+ Arrays.toString(arr));
}
}
输出
Modified arr[] : [6, 7, 9, 13, 21, 45, 101, 102]
时间复杂度。O(N log N)
辅助空间。O(1)
例3 :
// Java program to Sort a Subarray in Array
// Using Arrays.sort() method
// 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)
{
// Custom input array
// It contains 8 elements as follows
int[] arr = { 13, 7, 6, 45, 21, 9, 2, 100 };
// Sort subarray from index 1 to 4, i.e.,
// only sort subarray {7, 6, 45, 21} and
// keep other elements as it is.
Arrays.sort(arr, 1, 5);
// Printing the updated array which is
// sorted after 2 index inclusive till 5th index
System.out.println("Modified arr[] : "
+ Arrays.toString(arr));
}
}
输出
Modified arr[] : [13, 6, 7, 21, 45, 9, 2, 100]
时间复杂度。O(nlog(n)),因为它是arrays.sort()的复杂性
辅助空间 : O(1 )
例4 :
// Java program to Sort a Subarray in Descending order
// Using Arrays.sort()
// Importing Collections class and arrays classes
// from java.util package
import java.util.Arrays;
import java.util.Collections;
// Main class
public class GFG {
// Main driver method
public static void main(String[] args)
{
// Note that we have Integer here instead of
// int[] as Collections.reverseOrder doesn't
// work for primitive types.
Integer[] arr = { 13, 7, 6, 45, 21, 9, 2, 100 };
// Sorts arr[] in descending order using
// reverseOrder() method of Collections class
// in Array.sort() as an argument to it
Arrays.sort(arr, Collections.reverseOrder());
// Printing the array as generated above
System.out.println("Modified arr[] : "
+ Arrays.toString(arr));
}
}
输出
Modified arr[] : [100, 45, 21, 13, 9, 7, 6, 2]
时间复杂度。O(nlog(n)),因为它是arrays.sort()的复杂性
辅助空间 : O(1 )
例5 :
// Java program to sort an array of strings
// in ascending and descending alphabetical order
// Using Arrays.sort()
// Importing arrays and Collections class
// from java.util class
import java.util.Arrays;
import java.util.Collections;
// Main class
public class GFG {
// Main driver method
public static void main(String[] args)
{
// Custom input string
String arr[] = { "practice.geeksforgeeks.org",
"www.geeksforgeeks.org",
"code.geeksforgeeks.org" };
// Sorts arr[] in ascending order
Arrays.sort(arr);
System.out.println("Modified arr[] : "
+ Arrays.toString(arr));
// Sorts arr[] in descending order
Arrays.sort(arr, Collections.reverseOrder());
// Lastly printing the above array
System.out.println("Modified arr[] :"
+ Arrays.toString(arr));
}
}
输出
Modified arr[] :
Modified arr[] :[www.geeksforgeeks.org, practice.geeksforgeeks.org, code.geeksforgeeks.org]
时间复杂度。O(nlog(n)),因为它是arrays.sort()的复杂性
辅助空间:O(1 )
最后,我们将充分实现sort()方法,因为在这里我们将在比较器接口的帮助下声明我们自己定义的标准。
例6 :
// Java program to demonstrate Working of
// Comparator interface
// Importing required classes
import java.io.*;
import java.lang.*;
import java.util.*;
// Class 1
// A class to represent a student.
class Student {
int rollno;
String name, address;
// Constructor
public Student(int rollno, String name, String address)
{
// This keyword refers to current object itself
this.rollno = rollno;
this.name = name;
this.address = address;
}
// Used to print student details in main()
public String toString()
{
return this.rollno + " " + this.name + " "
+ this.address;
}
}
// Class 2
// Helper class extending Comparator interface
class Sortbyroll implements Comparator<Student> {
// Used for sorting in ascending order of
// roll number
public int compare(Student a, Student b)
{
return a.rollno - b.rollno;
}
}
// Class 3
// Main class
class GFG {
// Main driver method
public static void main(String[] args)
{
Student[] arr
= { new Student(111, "bbbb", "london"),
new Student(131, "aaaa", "nyc"),
new Student(121, "cccc", "jaipur") };
System.out.println("Unsorted");
for (int i = 0; i < arr.length; i++)
System.out.println(arr[i]);
// Sorting on basic as per class 1 created
// (user-defined)
Arrays.sort(arr, new Sortbyroll());
System.out.println("\nSorted by rollno");
for (int i = 0; i < arr.length; i++)
System.out.println(arr[i]);
}
}
输出
Unsorted
111 bbbb london
131 aaaa nyc
121 cccc jaipur
Sorted by rollno
111 bbbb london
121 cccc jaipur
131 aaaa nyc
时间复杂度。O(nlog(n)),因为它是Arrays.sort()的复杂性
辅助空间:O(1 )
记住: Arrays.sort()与Collections.sort()之间有一点区别。Arrays.sort()适用于数组,数组也可以是原始数据类型。Collections.sort()适用于对象集合,如ArrayList, LinkedList等。
使用反向排序方法: 该方法将对数组进行降序排序。在Java集合类中也提供了reverseOrder()方法来对数组进行反向排序。 因为是静态方法,它不解析任何参数,所以我们可以通过使用类的名称直接调用它。它将通过sort()方法对数组进行升序排序,然后reverse order()方法将给我们提供自然排序,我们将得到降序排序的数组。
语法
Arrays.sort(a, Collections.reverseOrder());
例7 :
// This will sort the array in the descending order
/*package whatever //do not write package name here */
import java.util.Arrays;
import java.util.Collections;
public class GFG {
public static void main(String[] args)
{
Integer[] array
= { 99, 12, -8, 12, 34, 110, 0, 121, 66, -110 };
Arrays.sort(array, Collections.reverseOrder());
System.out.println(
"Array in descending order: "
+ Arrays.toString(array));
}
}
输出
Array in descending order: [121, 110, 99, 66, 34, 12, 12, 0, -8, -110]
时间复杂度。O(nlog(n)),因为它是arrays.sort()的复杂性
辅助空间:O(1 )