配列クラス 要素の検索、並べ替え、比較、挿入、または配列の文字列表現を返すために配列で使用される静的メソッドを含むクラスです。したがって、最初に関数を指定し、後で同じことについて説明します。に存在するのは以下の通りです java.util.Arrays クラス。ここでは、を使用したさまざまなプロットについて説明します。 Arrays クラスの sort() メソッド 。
Arrays.sort() メソッドは 2 つのバリエーションで構成されています。 1 つは引数を渡さず、整数配列または文字配列である完全な配列をソートしますが、Arrays クラスのこのメソッドを使用して特定の部分をソートする場合は、それをオーバーロードし、開始インデックスと最後のインデックスを配列に渡します。
構文: sort() メソッド
Arrays.sort();>
構文: オーバーロードされたsort()メソッド
public static void sort(int[] arr, int from_Index, int to_Index) ;>
パラメーター: 次のような構文から分かるように、これには 3 つのパラメータが必要です。
- ソートされる配列
- ソートされる最初の要素のインデックス (from_index と呼ばれます)
- ソートされる最後の要素のインデックス (これを除く) (last_index と呼ばれます)
戻り値の型: それ
複雑さの分析:
時間計算量: O(N log N)
補助スペース: ○(1)
ここで、次のように Arrays クラスのさまざまなシナリオでの sort() 関数の実装を見てみましょう。
例 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(> '
The 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>
時間計算量: arrays.sort() の複雑さとして O(nlog(n))
補助スペース: ○(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)
補助スペース: ○(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]>
上記のメソッドの複雑さ:
時間計算量: arrays.sort() の複雑さとして O(nlog(n))
補助スペース: ○(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]>
上記のメソッドの複雑さ:
時間計算量: arrays.sort() の複雑さとして O(nlog(n))
補助スペース: ○(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 .techcodeview.com> ,> > 'www .techcodeview.com> ,> > 'code .techcodeview.com> };> > // 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));> > }> }> |
Javaの抽象化
>
>出力
Modified arr[] : Modified arr[] :[www.techcodeview.com, practice.techcodeview.com, code.techcodeview.com]>
上記のメソッドの複雑さ:
時間計算量: arrays.sort() の複雑さとして O(nlog(n))
補助スペース: ○(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 {> > // 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 System.out.println(arr[i]); // Sorting on basic as per class 1 created // (user-defined) Arrays.sort(arr, new Sortbyroll()); System.out.println('
Sorted by rollno'); for (int i = 0; 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>
上記のメソッドの複雑さ:
時間計算量: arrays.sort() の複雑さとして O(nlog(n))
補助スペース: ○(1)
覚えて: Arrays.sort() と Arrays.sort() の間にはわずかな違いがあります。 コレクション.sort() 。 Arrays.sort() は、プリミティブ データ型の配列でも機能します。 コレクション .sort() はオブジェクトのコレクションに対して機能します。 配列リスト 、 リンクリスト 、など。
逆順序の方法を使用すると、次のようになります。 このメソッドは配列を降順に並べ替えます。 Java Collections クラスでは、 reverseOrder() 配列を逆辞書順に並べ替えるメソッド。静的メソッドであるためパラメータは解析されないため、クラス名を使用して直接呼び出すことができます。 sort() メソッドによって配列を昇順にソートし、その後 reverse order() メソッドによって自然な順序が与えられ、降順にソートされた配列が得られます。
構文:
Arrays.sort(a, Collections.reverseOrder());>
例 7:
ジャワ
Javaで日付をフォーマットする
// 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]>
上記のメソッドの複雑さ:
時間計算量: arrays.sort() の複雑さとして O(nlog(n))
補助スペース: ○(1)