logo

Ako triediť pole v Jave

Triedenie je spôsob, ako usporiadať prvky zoznamu alebo poľa v určitom poradí. Poradie môže byť vzostupné alebo zostupné. The číselné a lexikografický (abecedné) poradie je široko používané poradie.

V tejto časti sa naučíme ako triediť pole v Java v vzostupne a zostupne objednať pomocou zoradiť () metóda a bez použitia metódy sort(). . Spolu s tým sa budeme aj učiť ako triediť podpolie v Java .

Zoradiť pole vo vzostupnom poradí

The vzostupné poradie usporiada prvky v poradí od najnižšieho po najvyššie. Je tiež známy ako prirodzený poriadok alebo číselné poradie . Triedenie môžeme vykonať nasledujúcimi spôsobmi:

  • Pomocou metódy sort().
  • Bez použitia metódy
    • Pomocou slučky for
    • Použitie metódy definovanej používateľom

Pomocou metódy sort().

v Jave Polia je trieda definovaná vjava.utilbalík, ktorý poskytuje zoradiť () metóda na zoradenie poľa vo vzostupnom poradí. Používa sa Algoritmus rýchleho triedenia s dvojitým pivotom na triedenie. Jeho zložitosť je O(n log(n)) . Je to a statické metóda, ktorá analyzuje an pole ako parameter a nič nevracia. Môžeme ho vyvolať priamo pomocou názvu triedy. Akceptuje pole typu int, float, double, long, char, byte.

Syntax:

 public static void sort(int[] a) 

Kde a je pole, ktoré má byť krátke.

Poznámka: Podobne ako trieda Arrays, aj trieda Collections poskytuje metódu sort() na triedenie poľa. Ale je medzi nimi rozdiel. Metóda sort() triedy Arrays funguje pre primitívny typ, zatiaľ čo metóda sort() triedy Collections funguje pre objekty Collections, ako sú LinkedList, ArrayList atď.

Zoraďme pole pomocou metódy sort() triedy Arrays.

V nasledujúcom programe sme definovali pole typu integer. Potom sme vyvolali metódu sort() triedy Arrays a analyzujeme pole, ktoré sa má triediť. Na tlač triedeného poľa sme použili slučku for.

avl strom

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;>

Vo vyššie uvedenom programe môžeme na tlač poľa použiť aj metódu toSting() triedy Arrays, ako je znázornené v nasledujúcom príkaze. Vráti reťazcovú reprezentáciu zadaného poľa.

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

Bez použitia metódy

Pomocou slučky for

V nasledujúcom príklade sme inicializovali pole typu celé číslo a zoradili sme pole vzostupne.

java arraylist

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;>

Použitie metódy definovanej používateľom

V nasledujúcom príklade sme definovali metódu s názvom sortArray() ktorý obsahuje logiku na zoradenie poľa v prirodzenom poradí.

SortArrayExample3.java

 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;>

Výkon:

 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 

Zoradiť pole v zostupnom poradí

The zostupnom poradí usporiada prvky od najvyššieho po najnižšie. Triedenie môžeme vykonať nasledujúcimi spôsobmi:

  • Pomocou obrátené poradie() Metóda
  • Bez použitia metódy
    • Pomocou pre Slučka
    • Pomocou Definované užívateľom Metóda

Použitie metódy reverseOrder().

Java zbierky trieda poskytuje obrátené poradie() metóda na zoradenie poľa v obrátenom lexikografickom poradí. Je to statická metóda, takže ju môžeme vyvolať priamo pomocou názvu triedy. Neanalyzuje žiadny parameter. Vracia a komparátor ktorý ukladá opak prirodzeného usporiadania (vzostupné poradie).

Znamená to, že pole triedi prvky vo vzostupnom poradí pomocou metódy sort(), potom metóda reverseOrder() obráti prirodzené zoradenie a dostaneme zoradené pole v zostupnom poradí.

Syntax:

 public static Comparator reverseOrder() 

Predpokladajme, že a[] je pole, ktoré sa má zoradiť v zostupnom poradí. Metódu reverseOrder() použijeme nasledujúcim spôsobom:

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

Zoraďme pole v zostupnom poradí.

V nasledujúcom programe si treba všimnúť bod, že sme definovali pole ako Celé číslo . Pretože metóda reverseOrder() nefunguje pre primitívny typ údajov.

metódy arraylist

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)); } } 

Výkon:

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

Pozrime sa na ďalší program, ktorý triedi prvky poľa v abecednom poradí.

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)); } } 

Výkon:

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

Bez použitia metódy

Pomocou slučky for

V nasledujúcom príklade sme inicializovali celočíselné pole a vykonali triedenie v zostupnom poradí.

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;>

Použitie metódy definovanej používateľom

SortArrayExample7.java

prenosová rýchlosť v arduino
 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;>

Ako triediť Subarray

Pole odvodené od poľa je známe ako subarray . Predpokladajme, a[] je pole s prvkami [12, 90, 34, 2, 45, 3, 22, 18, 5, 78] a my chceme zoradiť prvky poľa od 34 do 18. Zoradí podpole [34, 2, 45, 3, 22, 18] a ostatné prvky ponechajte tak, ako sú.

Ak chcete triediť podpole, trieda Arrays poskytuje statickú metódu s názvom zoradiť () . Zoradí zadaný rozsah poľa vo vzostupnom poradí. Pole môžeme tiež triediť podľa typu long, double, float, char, byte, atď.

Syntax:

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

Metóda analyzuje nasledujúce tri parametre:

    a:Pole, ktoré sa má zoradiť.z indexu:Index prvého prvku podpola. Podieľa sa na triedení.toIndex:Index posledného prvku podpola. Nezúčastňuje sa triedenia.

Ak sa formIndex rovná toIndex, rozsah, ktorý sa má zoradiť, je prázdny. Vyhodí IllegalArgumentException if formIndex je väčší ako toIndex . Vyhodí tiež ArrayIndexOutOfBoundsException if z Index a.dĺžka .

Zoraďme podpolie pomocou programu Java.

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;>