logo

Kako sortirati niz u Javi

Sortiranje je način raspoređivanja elemenata popisa ili niza određenim redoslijedom. Redoslijed može biti uzlazni ili silazni. The numerički i leksikografski (abecedni) red je redoslijed koji se široko koristi.

U ovom odjeljku naučit ćemo kako sortirati niz u Java u uzlazni i silazni naručiti pomoću vrsta() metoda i bez korištenja metode sort(). . Uz to ćemo i učiti kako sortirati podniz u Java .

java veza mysql

Poredaj niz uzlaznim redoslijedom

The uzlazni redoslijed slaže elemente od najnižeg do najvišeg reda. Također je poznat kao prirodni poredak ili numerički poredak . Sortiranje možemo izvršiti na sljedeće načine:

  • Korištenje metode sort().
  • Bez korištenja metode
    • Korištenje petlje for
    • Korištenje korisnički definirane metode

Korištenje metode sort().

U Javi, Nizovi je klasa definirana ujava.utilpaket koji pruža vrsta() metoda za sortiranje niza uzlaznim redoslijedom. Koristi se Dual-Pivot Quicksort algoritam za sortiranje. Njegova složenost je O(n log(n)) . To je statički metoda koja analizira an niz kao parametar i ne vraća ništa. Možemo ga pozvati izravno koristeći naziv klase. Prihvaća niz tipa int, float, double, long, char, byte.

Sintaksa:

 public static void sort(int[] a) 

Gdje a je niz koji treba biti kratak.

Napomena: Poput klase Arrays, klasa Collections također nudi metodu sort() za sortiranje niza. Ali među njima postoji razlika. Metoda sort() klase Arrays radi za primitivni tip, dok metoda sort() klase Collections radi za objekte Collections, kao što su LinkedList, ArrayList, itd.

Razvrstajmo niz koristeći metodu sort() klase Arrays.

U sljedećem programu definirali smo niz tipa integer. Nakon toga, pozvali smo metodu sort() klase Arrays i raščlanili niz za sortiranje. Za ispis sortiranog niza upotrijebili smo for petlju.

SortArrayExample1.java

 import java.util.Arrays; public class SortArrayExample1 { public static void main(String[] args) { //defining an array of integer type int [] array = new int [] {90, 23, 5, 109, 12, 22, 67, 34}; //invoking sort() method of the Arrays class Arrays.sort(array); System.out.println(&apos;Elements of array sorted in ascending order: &apos;); //prints array using the for loop for (int i = 0; i <array.length; i++) { system.out.println(array[i]); } < pre> <p> <strong>Output:</strong> </p> <pre> Array elements in ascending order: 5 12 22 23 34 67 90 109 </pre> <p>In the above program, we can also use the toSting() method of the Arrays class to print the array, as shown in the following statement. It returns a string representation of the specified array.</p> <pre> System.out.printf(Arrays.toString(array)); </pre> <h3>Without Using the Method</h3> <h3>Using the for Loop</h3> <p>In the following example, we have initialized an array of integer type and sort the array in ascending order.</p> <p> <strong>SortArrayExample2.java</strong> </p> <pre> public class SortArrayExample2 { public static void main(String[] args) { //creating an instance of an array int[] arr = new int[] {78, 34, 1, 3, 90, 34, -1, -4, 6, 55, 20, -65}; System.out.println(&apos;Array elements after sorting:&apos;); //sorting logic for (int i = 0; i <arr.length; i++) { for (int j="i" + 1; arr[j]) tmp="arr[i];" arr[i]="arr[j];" arr[j]="tmp;" } prints the sorted element of array system.out.println(arr[i]); < pre> <p> <strong>Output:</strong> </p> <pre> Array elements after sorting: -65 -4 -1 1 3 6 20 34 34 55 78 90 </pre> <h3>Using the User Defined Method</h3> <p>In the following example, we have defined a method named <strong>sortArray()</strong> that contains the logic to sort an array in natural order.</p> <p> <strong>SortArrayExample3.java</strong> </p> <pre> public class SortArrayExample3 { public static void main(String[] args) { int i; //initializing an array int array[] = {12, 45, 1, -1, 0, 4, 56, 23, 89, -21, 56, 27}; System.out.print(&apos;Array elements before sorting: 
&apos;); for(i = 0; i <array.length; i++) system.out.println(array[i]); invoking user defined method sortarray(array, array.length); system.out.print('array elements after sorting: 
'); accessing of the sorted array for(i="0;" i <array.length; { } to sort an in ascending order private static void sortarray(int array[], int n) for (int <n; j="i;" a="array[i];" while ((j> 0) &amp;&amp; (array[j-1] &gt; a)) //returns true when both conditions are true { array[j] = array[j-1]; j--; } array[j] = a; } } } </array.length;></pre> <p> <strong>Output:</strong> </p> <pre> Array elements before sorting: 12 45 1 -1 0 4 56 23 89 -21 56 27 Array elements after sorting: -21 -1 0 1 4 12 23 27 45 56 56 89 </pre> <h2>Sort Array in Descending Order</h2> <p>The <strong>descending order</strong> arranges the elements in the highest to lowest order. We can perform sorting in the following ways:</p> <ul> <li>Using the <strong>reverseOrder()</strong> Method</li> <li>Without using the method <ul> <li>Using the <strong>for</strong> Loop</li> <li>Using the <strong>User Defined</strong> Method</li> </ul></li> </ul> <h3>Using the reverseOrder() Method</h3> <p> <a href="/java-collections-class">Java <strong>Collections</strong> class</a> provides the <strong>reverseOrder()</strong> method to sort the array in reverse-lexicographic order. It is a static method, so we can invoke it directly by using the class name. It does not parse any parameter. It returns a <strong>comparator</strong> that imposes the reverse of the natural ordering (ascending order).</p> <p>It means that the array sorts elements in the ascending order by using the sort() method, after that the reverseOrder() method reverses the natural ordering, and we get the sorted array in descending order.</p> <p> <strong>Syntax:</strong> </p> <pre> public static Comparator reverseOrder() </pre> <p>Suppose, a[] is an array to be sort in the descending order. We will use the reverseOrder() method in the following way:</p> <pre> Arrays.sort(a, Collections.reverseOrder()); </pre> <p>Let&apos;s sorts an array in the descending order.</p> <p>In the following program, a point to be noticed that we have defined an array as <strong>Integer</strong> . Because the reverseOrder() method does not work for the primitive data type.</p> <p> <strong>SortArrayExample4.java</strong> </p> <pre> import java.util.Arrays; import java.util.Collections; public class SortArrayExample4 { public static void main(String[] args) { Integer [] array = {23, -9, 78, 102, 4, 0, -1, 11, 6, 110, 205}; // sorts array[] in descending order Arrays.sort(array, Collections.reverseOrder()); System.out.println(&apos;Array elements in descending order: &apos; +Arrays.toString(array)); } } </pre> <p> <strong>Output:</strong> </p> <pre> Array elements in descending order: [205, 110, 102, 78, 23, 11, 6, 4, 0, -1, -9] </pre> <p>Let&apos;s see another program that sorts array elements in alphabetical order.</p> <p> <strong>SortArrayExample5.java</strong> </p> <pre> import java.util.Arrays; import java.util.Collections; public class SortArrayExample5 { public static void main(String[] args) { String [] strarray = {&apos;Mango&apos;, &apos;Apple&apos;, &apos;Grapes&apos;, &apos;Papaya&apos;, &apos;Pineapple&apos;, &apos;Banana&apos;, &apos;Orange&apos;}; // sorts array[] in descending order Arrays.sort(strarray, Collections.reverseOrder()); System.out.println(&apos;Array elements in descending order: &apos; +Arrays.toString(strarray)); } } </pre> <p> <strong>Output:</strong> </p> <pre> Array elements in descending order: [Papaya, Pineapple, Orange, Mango, Grapes, Banana, Apple] </pre> <h3>Without Using the Method</h3> <h3>Using the for Loop</h3> <p>In the following example, we have initialized an integer array and perform sorting in descending order.</p> <p> <strong>SortArrayExample6.java</strong> </p> <pre> public class SortArrayExample6 { public static void main(String[] args) { int temp; //initializing an array int a[]={12,5,56,-2,32,2,-26,9,43,94,-78}; for (int i = 0; i <a.length; i++) { for (int j="i" + 1; < a.length; j++) if (a[i] a[j]) temp="a[i];" a[i]="a[j];" a[j]="temp;" } system.out.println('array elements in descending order:'); accessing element of the array i="0;" - system.out.println(a[i]); pre> <p> <strong>Output:</strong> </p> <pre> Array elements in descending order: 94 56 43 32 12 9 5 2 -2 -26 -78 </pre> <h3>Using the User Defined Method</h3> <p> <strong>SortArrayExample7.java</strong> </p> <pre> import java.util.Scanner; public class SortArrayExample7 { public static void main(String[] args) { int n, temp; Scanner s = new Scanner(System.in); System.out.print(&apos;Enter the number of elements: &apos;); n = s.nextInt(); int a[] = new int[n]; System.out.println(&apos;Enter the elements of the array: &apos;); for (int i = 0; i <n; i++) { a[i]="s.nextInt();" } for (int i="0;" < n; j="i" + 1; j++) if (a[i] a[j]) temp="a[i];" a[j]="temp;" system.out.println('array elements in descending order:'); n - system.out.println(a[i]); system.out.print(a[n 1]); pre> <p> <strong>Output:</strong> </p> <pre> Enter the number of elements: 7 Enter the elements of the array: 12 5 56 -2 32 2 -26 Array elements in descending order: 56 32 12 5 2 -2 -26 </pre> <h2>How to Sort Subarray</h2> <p>An array derived from the array is known as <strong>subarray</strong> . Suppose, <strong>a[]</strong> is an array having the elements [12, 90, 34, 2, 45, 3, 22, 18, 5, 78] and we want to sort array elements from 34 to 18. It will sort the subarray <strong>[34, 2, 45, 3, 22, 18]</strong> and keep the other elements as it is.</p> <p>To sort the subarray, the Arrays class provides the static method named <strong>sort()</strong> . It sorts the specified range of the array into ascending order. We can also sort the array of type <strong>long, double, float, char, byte,</strong> etc.</p> <p> <strong>Syntax:</strong> </p> <pre> public static void sort(int[] a, int fromIndex, int toIndex) </pre> <p>The method parses the following three parameters:</p> <ul> <tr><td>a:</td> An array to be sort. </tr><tr><td>fromIndex:</td> The index of the first element of the subarray. It participates in the sorting. </tr><tr><td>toIndex:</td> The index of the last element of the subarray. It does not participate in the sorting. </tr></ul> <p>If formIndex is equal to the toIndex, the range to be sorted is empty. It throws IllegalArgumentException if <strong>fomIndex is greater than toIndex</strong> . It also throws ArrayIndexOutOfBoundsException if <strong>fromIndex a.length</strong> .</p> <p>Let&apos;s sort a subarray through a Java program.</p> <p> <strong>SortSubarrayExample.java</strong> </p> <pre> import java.util.Arrays; public class SortSubarrayExample { public static void main(String[] args) { //defining an array int[] a = {12, 90, 34, 2, 45, 3, 22, 18, 5, 78}; // sorts subarray form index 2 to 7 Arrays.sort(a, 2, 7); //prints array using the for loop for (int i = 0; i <a.length; i++) { system.out.println(a[i]); } < pre> <p> <strong>Output:</strong> </p> <pre> Sorted Subarray: 12 90 2 3 22 34 45 18 5 78 </pre> <hr></a.length;></pre></n;></pre></a.length;></pre></arr.length;></pre></array.length;>

U gornjem programu također možemo koristiti metodu toSting() klase Arrays za ispis niza, kao što je prikazano u sljedećoj izjavi. Vraća string reprezentaciju navedenog niza.

 System.out.printf(Arrays.toString(array)); 

Bez korištenja metode

Korištenje petlje for

U sljedećem smo primjeru inicijalizirali niz cjelobrojnog tipa i sortirali niz uzlaznim redoslijedom.

SortArrayExample2.java

 public class SortArrayExample2 { public static void main(String[] args) { //creating an instance of an array int[] arr = new int[] {78, 34, 1, 3, 90, 34, -1, -4, 6, 55, 20, -65}; System.out.println(&apos;Array elements after sorting:&apos;); //sorting logic for (int i = 0; i <arr.length; i++) { for (int j="i" + 1; arr[j]) tmp="arr[i];" arr[i]="arr[j];" arr[j]="tmp;" } prints the sorted element of array system.out.println(arr[i]); < pre> <p> <strong>Output:</strong> </p> <pre> Array elements after sorting: -65 -4 -1 1 3 6 20 34 34 55 78 90 </pre> <h3>Using the User Defined Method</h3> <p>In the following example, we have defined a method named <strong>sortArray()</strong> that contains the logic to sort an array in natural order.</p> <p> <strong>SortArrayExample3.java</strong> </p> <pre> public class SortArrayExample3 { public static void main(String[] args) { int i; //initializing an array int array[] = {12, 45, 1, -1, 0, 4, 56, 23, 89, -21, 56, 27}; System.out.print(&apos;Array elements before sorting: 
&apos;); for(i = 0; i <array.length; i++) system.out.println(array[i]); invoking user defined method sortarray(array, array.length); system.out.print(\'array elements after sorting: 
\'); accessing of the sorted array for(i="0;" i <array.length; { } to sort an in ascending order private static void sortarray(int array[], int n) for (int <n; j="i;" a="array[i];" while ((j> 0) &amp;&amp; (array[j-1] &gt; a)) //returns true when both conditions are true { array[j] = array[j-1]; j--; } array[j] = a; } } } </array.length;></pre> <p> <strong>Output:</strong> </p> <pre> Array elements before sorting: 12 45 1 -1 0 4 56 23 89 -21 56 27 Array elements after sorting: -21 -1 0 1 4 12 23 27 45 56 56 89 </pre> <h2>Sort Array in Descending Order</h2> <p>The <strong>descending order</strong> arranges the elements in the highest to lowest order. We can perform sorting in the following ways:</p> <ul> <li>Using the <strong>reverseOrder()</strong> Method</li> <li>Without using the method <ul> <li>Using the <strong>for</strong> Loop</li> <li>Using the <strong>User Defined</strong> Method</li> </ul></li> </ul> <h3>Using the reverseOrder() Method</h3> <p> <a href="/java-collections-class">Java <strong>Collections</strong> class</a> provides the <strong>reverseOrder()</strong> method to sort the array in reverse-lexicographic order. It is a static method, so we can invoke it directly by using the class name. It does not parse any parameter. It returns a <strong>comparator</strong> that imposes the reverse of the natural ordering (ascending order).</p> <p>It means that the array sorts elements in the ascending order by using the sort() method, after that the reverseOrder() method reverses the natural ordering, and we get the sorted array in descending order.</p> <p> <strong>Syntax:</strong> </p> <pre> public static Comparator reverseOrder() </pre> <p>Suppose, a[] is an array to be sort in the descending order. We will use the reverseOrder() method in the following way:</p> <pre> Arrays.sort(a, Collections.reverseOrder()); </pre> <p>Let&apos;s sorts an array in the descending order.</p> <p>In the following program, a point to be noticed that we have defined an array as <strong>Integer</strong> . Because the reverseOrder() method does not work for the primitive data type.</p> <p> <strong>SortArrayExample4.java</strong> </p> <pre> import java.util.Arrays; import java.util.Collections; public class SortArrayExample4 { public static void main(String[] args) { Integer [] array = {23, -9, 78, 102, 4, 0, -1, 11, 6, 110, 205}; // sorts array[] in descending order Arrays.sort(array, Collections.reverseOrder()); System.out.println(&apos;Array elements in descending order: &apos; +Arrays.toString(array)); } } </pre> <p> <strong>Output:</strong> </p> <pre> Array elements in descending order: [205, 110, 102, 78, 23, 11, 6, 4, 0, -1, -9] </pre> <p>Let&apos;s see another program that sorts array elements in alphabetical order.</p> <p> <strong>SortArrayExample5.java</strong> </p> <pre> import java.util.Arrays; import java.util.Collections; public class SortArrayExample5 { public static void main(String[] args) { String [] strarray = {&apos;Mango&apos;, &apos;Apple&apos;, &apos;Grapes&apos;, &apos;Papaya&apos;, &apos;Pineapple&apos;, &apos;Banana&apos;, &apos;Orange&apos;}; // sorts array[] in descending order Arrays.sort(strarray, Collections.reverseOrder()); System.out.println(&apos;Array elements in descending order: &apos; +Arrays.toString(strarray)); } } </pre> <p> <strong>Output:</strong> </p> <pre> Array elements in descending order: [Papaya, Pineapple, Orange, Mango, Grapes, Banana, Apple] </pre> <h3>Without Using the Method</h3> <h3>Using the for Loop</h3> <p>In the following example, we have initialized an integer array and perform sorting in descending order.</p> <p> <strong>SortArrayExample6.java</strong> </p> <pre> public class SortArrayExample6 { public static void main(String[] args) { int temp; //initializing an array int a[]={12,5,56,-2,32,2,-26,9,43,94,-78}; for (int i = 0; i <a.length; i++) { for (int j="i" + 1; < a.length; j++) if (a[i] a[j]) temp="a[i];" a[i]="a[j];" a[j]="temp;" } system.out.println(\'array elements in descending order:\'); accessing element of the array i="0;" - system.out.println(a[i]); pre> <p> <strong>Output:</strong> </p> <pre> Array elements in descending order: 94 56 43 32 12 9 5 2 -2 -26 -78 </pre> <h3>Using the User Defined Method</h3> <p> <strong>SortArrayExample7.java</strong> </p> <pre> import java.util.Scanner; public class SortArrayExample7 { public static void main(String[] args) { int n, temp; Scanner s = new Scanner(System.in); System.out.print(&apos;Enter the number of elements: &apos;); n = s.nextInt(); int a[] = new int[n]; System.out.println(&apos;Enter the elements of the array: &apos;); for (int i = 0; i <n; i++) { a[i]="s.nextInt();" } for (int i="0;" < n; j="i" + 1; j++) if (a[i] a[j]) temp="a[i];" a[j]="temp;" system.out.println(\'array elements in descending order:\'); n - system.out.println(a[i]); system.out.print(a[n 1]); pre> <p> <strong>Output:</strong> </p> <pre> Enter the number of elements: 7 Enter the elements of the array: 12 5 56 -2 32 2 -26 Array elements in descending order: 56 32 12 5 2 -2 -26 </pre> <h2>How to Sort Subarray</h2> <p>An array derived from the array is known as <strong>subarray</strong> . Suppose, <strong>a[]</strong> is an array having the elements [12, 90, 34, 2, 45, 3, 22, 18, 5, 78] and we want to sort array elements from 34 to 18. It will sort the subarray <strong>[34, 2, 45, 3, 22, 18]</strong> and keep the other elements as it is.</p> <p>To sort the subarray, the Arrays class provides the static method named <strong>sort()</strong> . It sorts the specified range of the array into ascending order. We can also sort the array of type <strong>long, double, float, char, byte,</strong> etc.</p> <p> <strong>Syntax:</strong> </p> <pre> public static void sort(int[] a, int fromIndex, int toIndex) </pre> <p>The method parses the following three parameters:</p> <ul> <tr><td>a:</td> An array to be sort. </tr><tr><td>fromIndex:</td> The index of the first element of the subarray. It participates in the sorting. </tr><tr><td>toIndex:</td> The index of the last element of the subarray. It does not participate in the sorting. </tr></ul> <p>If formIndex is equal to the toIndex, the range to be sorted is empty. It throws IllegalArgumentException if <strong>fomIndex is greater than toIndex</strong> . It also throws ArrayIndexOutOfBoundsException if <strong>fromIndex a.length</strong> .</p> <p>Let&apos;s sort a subarray through a Java program.</p> <p> <strong>SortSubarrayExample.java</strong> </p> <pre> import java.util.Arrays; public class SortSubarrayExample { public static void main(String[] args) { //defining an array int[] a = {12, 90, 34, 2, 45, 3, 22, 18, 5, 78}; // sorts subarray form index 2 to 7 Arrays.sort(a, 2, 7); //prints array using the for loop for (int i = 0; i <a.length; i++) { system.out.println(a[i]); } < pre> <p> <strong>Output:</strong> </p> <pre> Sorted Subarray: 12 90 2 3 22 34 45 18 5 78 </pre> <hr></a.length;></pre></n;></pre></a.length;></pre></arr.length;>

Korištenje korisnički definirane metode

U sljedećem primjeru definirali smo metodu pod nazivom sortirajniz() koji sadrži logiku sortiranja niza prirodnim redoslijedom.

SortArrayExample3.java

mylivericket
 public class SortArrayExample3 { public static void main(String[] args) { int i; //initializing an array int array[] = {12, 45, 1, -1, 0, 4, 56, 23, 89, -21, 56, 27}; System.out.print(&apos;Array elements before sorting: 
&apos;); for(i = 0; i <array.length; i++) system.out.println(array[i]); invoking user defined method sortarray(array, array.length); system.out.print(\'array elements after sorting: 
\'); accessing of the sorted array for(i="0;" i <array.length; { } to sort an in ascending order private static void sortarray(int array[], int n) for (int <n; j="i;" a="array[i];" while ((j> 0) &amp;&amp; (array[j-1] &gt; a)) //returns true when both conditions are true { array[j] = array[j-1]; j--; } array[j] = a; } } } </array.length;>

Izlaz:

 Array elements before sorting: 12 45 1 -1 0 4 56 23 89 -21 56 27 Array elements after sorting: -21 -1 0 1 4 12 23 27 45 56 56 89 

Poredaj niz silaznim redoslijedom

The silazni redoslijed slaže elemente od najvišeg do najnižeg reda. Sortiranje možemo izvršiti na sljedeće načine:

  • Koristiti obrnuti redoslijed() metoda
  • Bez korištenja metode
    • Koristiti za Petlja
    • Koristiti Korisnik definiran metoda

Korištenje metode reverseOrder().

Java Zbirke razreda pruža obrnuti redoslijed() metoda za sortiranje niza obrnutim leksikografskim redoslijedom. To je statična metoda, tako da je možemo pozvati izravno korištenjem naziva klase. Ne analizira niti jedan parametar. Vraća a komparator koji nameće obrnuti prirodni poredak (uzlazni poredak).

To znači da niz razvrstava elemente u uzlaznom redoslijedu metodom sort(), nakon toga metoda reverseOrder() obrće prirodni poredak, te dobivamo sortirani niz u silaznom redoslijedu.

Sintaksa:

 public static Comparator reverseOrder() 

Pretpostavimo da je a[] polje koje treba sortirati silaznim redoslijedom. Koristit ćemo metodu reverseOrder() na sljedeći način:

 Arrays.sort(a, Collections.reverseOrder()); 

Razvrstajmo niz silaznim redoslijedom.

U sljedećem programu treba primijetiti da smo definirali niz kao Cijeli broj . Budući da metoda reverseOrder() ne radi za primitivni tip podataka.

SortArrayExample4.java

 import java.util.Arrays; import java.util.Collections; public class SortArrayExample4 { public static void main(String[] args) { Integer [] array = {23, -9, 78, 102, 4, 0, -1, 11, 6, 110, 205}; // sorts array[] in descending order Arrays.sort(array, Collections.reverseOrder()); System.out.println(&apos;Array elements in descending order: &apos; +Arrays.toString(array)); } } 

Izlaz:

 Array elements in descending order: [205, 110, 102, 78, 23, 11, 6, 4, 0, -1, -9] 

Pogledajmo još jedan program koji razvrstava elemente niza abecednim redom.

SortArrayExample5.java

 import java.util.Arrays; import java.util.Collections; public class SortArrayExample5 { public static void main(String[] args) { String [] strarray = {&apos;Mango&apos;, &apos;Apple&apos;, &apos;Grapes&apos;, &apos;Papaya&apos;, &apos;Pineapple&apos;, &apos;Banana&apos;, &apos;Orange&apos;}; // sorts array[] in descending order Arrays.sort(strarray, Collections.reverseOrder()); System.out.println(&apos;Array elements in descending order: &apos; +Arrays.toString(strarray)); } } 

Izlaz:

 Array elements in descending order: [Papaya, Pineapple, Orange, Mango, Grapes, Banana, Apple] 

Bez korištenja metode

Korištenje petlje for

U sljedećem primjeru, inicijalizirali smo niz cijelih brojeva i izvršili sortiranje silaznim redoslijedom.

kako pretvoriti char u string java

SortArrayExample6.java

 public class SortArrayExample6 { public static void main(String[] args) { int temp; //initializing an array int a[]={12,5,56,-2,32,2,-26,9,43,94,-78}; for (int i = 0; i <a.length; i++) { for (int j="i" + 1; < a.length; j++) if (a[i] a[j]) temp="a[i];" a[i]="a[j];" a[j]="temp;" } system.out.println(\'array elements in descending order:\'); accessing element of the array i="0;" - system.out.println(a[i]); pre> <p> <strong>Output:</strong> </p> <pre> Array elements in descending order: 94 56 43 32 12 9 5 2 -2 -26 -78 </pre> <h3>Using the User Defined Method</h3> <p> <strong>SortArrayExample7.java</strong> </p> <pre> import java.util.Scanner; public class SortArrayExample7 { public static void main(String[] args) { int n, temp; Scanner s = new Scanner(System.in); System.out.print(&apos;Enter the number of elements: &apos;); n = s.nextInt(); int a[] = new int[n]; System.out.println(&apos;Enter the elements of the array: &apos;); for (int i = 0; i <n; i++) { a[i]="s.nextInt();" } for (int i="0;" < n; j="i" + 1; j++) if (a[i] a[j]) temp="a[i];" a[j]="temp;" system.out.println(\'array elements in descending order:\'); n - system.out.println(a[i]); system.out.print(a[n 1]); pre> <p> <strong>Output:</strong> </p> <pre> Enter the number of elements: 7 Enter the elements of the array: 12 5 56 -2 32 2 -26 Array elements in descending order: 56 32 12 5 2 -2 -26 </pre> <h2>How to Sort Subarray</h2> <p>An array derived from the array is known as <strong>subarray</strong> . Suppose, <strong>a[]</strong> is an array having the elements [12, 90, 34, 2, 45, 3, 22, 18, 5, 78] and we want to sort array elements from 34 to 18. It will sort the subarray <strong>[34, 2, 45, 3, 22, 18]</strong> and keep the other elements as it is.</p> <p>To sort the subarray, the Arrays class provides the static method named <strong>sort()</strong> . It sorts the specified range of the array into ascending order. We can also sort the array of type <strong>long, double, float, char, byte,</strong> etc.</p> <p> <strong>Syntax:</strong> </p> <pre> public static void sort(int[] a, int fromIndex, int toIndex) </pre> <p>The method parses the following three parameters:</p> <ul> <tr><td>a:</td> An array to be sort. </tr><tr><td>fromIndex:</td> The index of the first element of the subarray. It participates in the sorting. </tr><tr><td>toIndex:</td> The index of the last element of the subarray. It does not participate in the sorting. </tr></ul> <p>If formIndex is equal to the toIndex, the range to be sorted is empty. It throws IllegalArgumentException if <strong>fomIndex is greater than toIndex</strong> . It also throws ArrayIndexOutOfBoundsException if <strong>fromIndex a.length</strong> .</p> <p>Let&apos;s sort a subarray through a Java program.</p> <p> <strong>SortSubarrayExample.java</strong> </p> <pre> import java.util.Arrays; public class SortSubarrayExample { public static void main(String[] args) { //defining an array int[] a = {12, 90, 34, 2, 45, 3, 22, 18, 5, 78}; // sorts subarray form index 2 to 7 Arrays.sort(a, 2, 7); //prints array using the for loop for (int i = 0; i <a.length; i++) { system.out.println(a[i]); } < pre> <p> <strong>Output:</strong> </p> <pre> Sorted Subarray: 12 90 2 3 22 34 45 18 5 78 </pre> <hr></a.length;></pre></n;></pre></a.length;>

Korištenje korisnički definirane metode

SortArrayExample7.java

 import java.util.Scanner; public class SortArrayExample7 { public static void main(String[] args) { int n, temp; Scanner s = new Scanner(System.in); System.out.print(&apos;Enter the number of elements: &apos;); n = s.nextInt(); int a[] = new int[n]; System.out.println(&apos;Enter the elements of the array: &apos;); for (int i = 0; i <n; i++) { a[i]="s.nextInt();" } for (int i="0;" < n; j="i" + 1; j++) if (a[i] a[j]) temp="a[i];" a[j]="temp;" system.out.println(\'array elements in descending order:\'); n - system.out.println(a[i]); system.out.print(a[n 1]); pre> <p> <strong>Output:</strong> </p> <pre> Enter the number of elements: 7 Enter the elements of the array: 12 5 56 -2 32 2 -26 Array elements in descending order: 56 32 12 5 2 -2 -26 </pre> <h2>How to Sort Subarray</h2> <p>An array derived from the array is known as <strong>subarray</strong> . Suppose, <strong>a[]</strong> is an array having the elements [12, 90, 34, 2, 45, 3, 22, 18, 5, 78] and we want to sort array elements from 34 to 18. It will sort the subarray <strong>[34, 2, 45, 3, 22, 18]</strong> and keep the other elements as it is.</p> <p>To sort the subarray, the Arrays class provides the static method named <strong>sort()</strong> . It sorts the specified range of the array into ascending order. We can also sort the array of type <strong>long, double, float, char, byte,</strong> etc.</p> <p> <strong>Syntax:</strong> </p> <pre> public static void sort(int[] a, int fromIndex, int toIndex) </pre> <p>The method parses the following three parameters:</p> <ul> <tr><td>a:</td> An array to be sort. </tr><tr><td>fromIndex:</td> The index of the first element of the subarray. It participates in the sorting. </tr><tr><td>toIndex:</td> The index of the last element of the subarray. It does not participate in the sorting. </tr></ul> <p>If formIndex is equal to the toIndex, the range to be sorted is empty. It throws IllegalArgumentException if <strong>fomIndex is greater than toIndex</strong> . It also throws ArrayIndexOutOfBoundsException if <strong>fromIndex a.length</strong> .</p> <p>Let&apos;s sort a subarray through a Java program.</p> <p> <strong>SortSubarrayExample.java</strong> </p> <pre> import java.util.Arrays; public class SortSubarrayExample { public static void main(String[] args) { //defining an array int[] a = {12, 90, 34, 2, 45, 3, 22, 18, 5, 78}; // sorts subarray form index 2 to 7 Arrays.sort(a, 2, 7); //prints array using the for loop for (int i = 0; i <a.length; i++) { system.out.println(a[i]); } < pre> <p> <strong>Output:</strong> </p> <pre> Sorted Subarray: 12 90 2 3 22 34 45 18 5 78 </pre> <hr></a.length;></pre></n;>

Kako sortirati podniz

Niz izveden iz niza poznat je kao podniz . Pretpostavimo, a[] je niz koji ima elemente [12, 90, 34, 2, 45, 3, 22, 18, 5, 78] i želimo sortirati elemente niza od 34 do 18. To će sortirati podniz [34, 2, 45, 3, 22, 18] a ostale elemente zadržati onakvima kakvi jesu.

Za sortiranje podniza, klasa Arrays pruža statičku metodu pod nazivom vrsta() . Razvrstava navedeni raspon niza prema uzlaznom redoslijedu. Također možemo sortirati niz tipa dugo, dvostruko, float, char, bajt, itd.

Sintaksa:

 public static void sort(int[] a, int fromIndex, int toIndex) 

Metoda analizira sljedeća tri parametra:

    a:Niz koji treba sortirati.fromIndex:Indeks prvog elementa podniza. Sudjeluje u sortiranju.toIndex:Indeks posljednjeg elementa podniza. Ne sudjeluje u sortiranju.

Ako je formIndex jednak toIndexu, raspon koji se sortira je prazan. Izbacuje IllegalArgumentException if fomIndex je veći od toIndex . Također izbacuje ArrayIndexOutOfBoundsException if odIndex a.duljina .

Razvrstajmo podmatricu kroz Java program.

SortSubarrayExample.java

 import java.util.Arrays; public class SortSubarrayExample { public static void main(String[] args) { //defining an array int[] a = {12, 90, 34, 2, 45, 3, 22, 18, 5, 78}; // sorts subarray form index 2 to 7 Arrays.sort(a, 2, 7); //prints array using the for loop for (int i = 0; i <a.length; i++) { system.out.println(a[i]); } < pre> <p> <strong>Output:</strong> </p> <pre> Sorted Subarray: 12 90 2 3 22 34 45 18 5 78 </pre> <hr></a.length;>