logo

Kaip rūšiuoti masyvą Java

Rūšiavimas yra būdas išdėstyti sąrašo ar masyvo elementus tam tikra tvarka. Tvarka gali būti didėjimo arba mažėjimo tvarka. The skaitinis ir leksikografinis (abėcėlės) tvarka yra plačiai naudojama tvarka.

mia khalifa amžius

Šiame skyriuje mes išmoksime kaip rūšiuoti masyvą in Java in kylantis ir nusileidžiantis užsisakykite naudodami Rūšiuoti () metodas ir nenaudodami rūšiavimo () metodo . Be to, mes taip pat mokysimės kaip rūšiuoti subarray in Java .

Rūšiuoti masyvą didėjančia tvarka

The Didėjančia tvarka išdėsto elementus nuo mažiausios iki aukščiausios. Jis taip pat žinomas kaip natūrali tvarka arba skaitine tvarka . Rūšiavimą galime atlikti šiais būdais:

  • Naudojant sort() metodą
  • Nenaudojant metodo
    • Naudojant for Loop
    • Vartotojo nustatyto metodo naudojimas

Naudojant sort() metodą

Java programoje Masyvai yra klasė, apibrėžtajava.utilpaketą, kuris suteikia Rūšiuoti () būdas rūšiuoti masyvą didėjančia tvarka. Tai naudoja Dual-Pivot Quicksort algoritmas rūšiavimui. Jo sudėtingumas yra O(n log(n)) . Tai yra statinis metodas, kuris analizuoja an masyvas kaip parametras ir nieko negrąžina. Ją galime iškviesti tiesiogiai naudodami klasės pavadinimą. Jis priima int, float, double, long, char, baitų tipų masyvą.

Sintaksė:

 public static void sort(int[] a) 

Kur a yra trumpas masyvas.

Pastaba: Kaip ir klasėje Arrays, kolekcijų klasėje taip pat pateikiamas būdas rūšiuoti () masyvui rūšiuoti. Tačiau tarp jų yra skirtumas. Masyvų klasės metodas sort() veikia primityviajam tipui, o kolekcijų klasės metodas sort() veikia objektų kolekcijoms, pvz., LinkedList, ArrayList ir kt.

Rūšiuokime masyvą naudodami klasės Arrays metodą sort().

Šioje programoje apibrėžėme sveikojo skaičiaus tipo masyvą. Po to mes iškvietėme Arrays klasės metodą sort () ir analizuojame masyvą, kad būtų galima rūšiuoti. Norėdami spausdinti surūšiuotą masyvą, naudojome kilpą.

RūšiuotiArrayExample1.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;>

Aukščiau pateiktoje programoje taip pat galime naudoti masyvo klasės metodą toSting() masyvo spausdinimui, kaip parodyta kitame teiginyje. Jis grąžina nurodyto masyvo eilutę.

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

Nenaudojant metodo

Naudojant for Loop

Toliau pateiktame pavyzdyje inicijavome sveikųjų skaičių tipo masyvą ir surūšiavome masyvą didėjančia tvarka.

RūšiuotiArrayExample2.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;>

Vartotojo nustatyto metodo naudojimas

Šiame pavyzdyje apibrėžėme metodą pavadinimu sortArray() kuriame yra logika rūšiuoti masyvą natūralia tvarka.

RūšiuotiArrayExample3.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;>

Išvestis:

 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 

Rūšiuoti masyvą mažėjančia tvarka

The Mažėjančia tvarka išdėsto elementus nuo aukščiausios iki žemiausės. Rūšiavimą galime atlikti šiais būdais:

  • Naudojant Atvirkštinė tvarka() Metodas
  • Nenaudojant metodo
    • Naudojant dėl Kilpa
    • Naudojant Nustatyta vartotojo Metodas

Naudojant reverseOrder() metodą

Java Kolekcijos klasė suteikia Atvirkštinė tvarka() būdas rūšiuoti masyvą atvirkštine leksikografine tvarka. Tai yra statinis metodas, todėl jį galime iškviesti tiesiogiai naudodami klasės pavadinimą. Jis neanalizuoja jokių parametrų. Tai grąžina a lyginamoji priemonė kuri nustato atvirkštinę natūralios eilės tvarką (didėjančia tvarka).

Tai reiškia, kad masyvas rūšiuoja elementus didėjančia tvarka naudojant sort() metodą, po to metodas reverseOrder() apverčia natūralią tvarką ir gauname surūšiuotą masyvą mažėjančia tvarka.

Sintaksė:

 public static Comparator reverseOrder() 

Tarkime, a[] yra masyvas, kurį reikia rūšiuoti mažėjančia tvarka. ReverseOrder() metodą naudosime tokiu būdu:

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

Surūšiuokime masyvą mažėjančia tvarka.

Šioje programoje reikia pastebėti, kad masyvą apibrėžėme kaip Sveikasis skaičius . Kadangi reverseOrder() metodas neveikia primityviam duomenų tipui.

RūšiuotiArrayExample4.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)); } } 

Išvestis:

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

Pažiūrėkime kitą programą, kuri rūšiuoja masyvo elementus abėcėlės tvarka.

RūšiuotiArrayExample5.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)); } } 

Išvestis:

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

Nenaudojant metodo

Naudojant for Loop

Toliau pateiktame pavyzdyje inicijavome sveikųjų skaičių masyvą ir atliekame rūšiavimą mažėjančia tvarka.

RūšiuotiArrayExample6.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;>

Vartotojo nustatyto metodo naudojimas

RūšiuotiArrayExample7.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;>

Kaip rūšiuoti „Subarray“.

Masyvas, gautas iš masyvo, yra žinomas kaip subbarray . Tarkime, a[] yra masyvas, turintis elementus [12, 90, 34, 2, 45, 3, 22, 18, 5, 78] ir norime rūšiuoti masyvo elementus nuo 34 iki 18. Jis surūšiuos pomasyvą [34, 2, 45, 3, 22, 18] o kitus elementus laikykite tokius, kokie yra.

ką reiškia google

Norėdami rūšiuoti pogrupį, klasėje Arrays pateikiamas statinis metodas, pavadintas Rūšiuoti () . Jis surūšiuoja nurodytą masyvo diapazoną didėjančia tvarka. Taip pat galime rūšiuoti masyvą pagal tipą ilgas, dvigubas, plūduriuojantis, simbolis, baitas, ir tt

Sintaksė:

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

Metodas analizuoja šiuos tris parametrus:

    a:Masyvas, kurį reikia rūšiuoti.iš indekso:Pirmojo pogrupio elemento indeksas. Dalyvauja rūšiavime.indeksuoti:Paskutinio posistemio elemento indeksas. Rūšiavime nedalyvauja.

Jei formIndex yra lygus toIndex, rūšiuojamas diapazonas yra tuščias. Ji išmeta IllegalArgumentException, jei fomIndex yra didesnis nei toIndex . Ji taip pat išmeta ArrayIndexOutOfBoundsException, jei išIndeksas a.ilgis .

Surūšiuokime pogrupį per Java programą.

RūšiuotiSubarrayExample.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;>