logo

Leksikogrāfiskais pasūtījums Java

Termins Leksikogrāfiskā secība ir matemātisks termins, kas pazīstams ar nosaukumiem: leksikas secība, leksikogrāfijas (al) produkts, alfabētiskā secība vai vārdnīcas secība.

Šajā sadaļā tiks apskatīta tēmas leksikogrāfiskā secība, tās definīcija un cita detalizēta informācija. Pēc tam mēs iemācīsimies lietot leksikogrāfiskās kārtības jēdzienu Java programmēšanas valoda .

Leksikogrāfiskās kārtības noteikšana

Leksikogrāfiskā secība jeb leksikogrāfija matemātikā ir vārdnīcu alfabētiskās secības vispārinājums līdz sakārtoto simbolu secībām vai pilnīgi sakārtota saraksta elementiem. Termins leksikogrāfiskā kārtība ir motivēts ar vārdu 'leksika'. Leksikons ir vārdu kopums, ko izmanto dažās citās valodās, un tam ir ierasta secība. Tādējādi leksikogrāfiskā secība ir veids, kā formalizēt vārdu secību, kur ir norādīta pamatā esošo simbolu secība.

Programmēšanā leksikogrāfiskā kārtība ir tautā pazīstama kā Vārdnīcas kārtība un tiek izmantots virkņu masīva kārtošanai, divu virkņu salīdzināšanai vai masīva elementu kārtošanai. Kļūst diezgan viegli kārtot elementus leksiski. Tas ir tāpēc, ka leksikogrāfiskajai kārtībai ir vairāki varianti un vispārinājumi, kuros:

  • Viens variants ir piemērojams dažāda garuma sekvencēm, jo ​​pirms konkrēto elementu izskatīšanas tiek salīdzināti secību garumi.
  • Otrais variants tiek izmantots noteiktas galīgās kopas apakškopu secībā. Tas tiek darīts, galīgajai kopai piešķirot kopējo secību. Pēc tam tas pārvērš apakškopas pieaugošās secībās, kurām tiek piemērota leksikogrāfiskā secība.
  • Vispārinājums attiecas uz daļēji sakārtotu kopu Dekarta reizinājuma secību, un šāda secība ir kopējā secība, ja un tikai tad, ja katrs Dekarta reizinājuma faktors ir sakārtots pilnībā.

Leksikogrāfiskās kārtības formālā jēdziena izpratne

  • Lai izprastu leksikogrāfiskās kārtības formālo jēdzienu:
  • Tas sākas ar ierobežotu kopu A, kas ir pazīstama kā alfabēts un ir pilnībā sakārtota. Turklāt tas nozīmē, ka a un b (jebkuri divi simboli, kas ir atšķirīgi un nav vienādi) A, vai nu a
  • Šeit A vārdi ir ierobežota simbolu secība no A un ietver vārdus, kuru garums ir 1 un kas satur vienu simbolu, vārdus, kuru garums ir 2, ar diviem simboliem, un vārdiem, kuru garums ir trīs, tas ir 3 un tā tālāk. Attiecībā uz to, tas ietver arī tukšo secību ? kam vispār nav simbolu. Tādējādi galīgās kopas A leksikogrāfisko kārtību var raksturot šādi:
  • Pieņemsim, ka divām dažādām vienāda garuma pasaulēm a = a1a2…akun b=b1b2…bkir dots. Šeit divu vārdu secība ir atkarīga no simbolu alfabētiskās secības pirmajā vietā i, kur divi vārdi atšķiras, skaitot no vārdu sākuma, t.i., izpildot nosacījumu a i i alfabēta A secībā.
  • Ja divi vārdi ir mainījušies garumā, parastā leksikogrāfiskā secība vārdu saīsina ar tukšumiem beigās, līdz abi vārdi kļūst vienādi, un tad vārdus salīdzina.

Leksikogrāfijas ieviešana Java valodā

Kā minēts iepriekš, leksikogrāfisko secību var izmantot vai nu divu virkņu salīdzināšanai, vai elementu kārtošanai. Šeit mēs apspriedīsim abas metodes un ieviesīsim katru.

Elementu šķirošana leksikogrāfiskā secībā

Vārdu sakārtošana secībā ir pazīstama kā leksikogrāfiskā kārtība vai arī pazīstams kā Vārdnīcas kārtība . Tas nozīmē, ka, piemērojot leksikogrāfisko secību, vārdi tiek sakārtoti alfabētiskā secībā atbilstoši to sastāvdaļu alfabētam. Lai kārtotu virkņu masīvu leksikogrāfiskā secībā, mums ir šādas divas metodes:

1. metode: jebkuras šķirošanas metodes izmantošana

Tālāk ir sniegts koda piemērs, kas ļaus mums saprast, kā mēs varam veikt elementu kārtošanu leksikogrāfiskā secībā:

 public class Main { public static void main(String[] args) { String[] name = { &apos;John&apos;,&apos;Remo&apos;,&apos;Mixy&apos;,&apos;Julie&apos;,&apos;Ronny&apos;}; int n = 5; System.out.println(&apos;Before Sorting&apos;); for(int i = 0; i <n; i++) { system.out.println(name[i]); } for(int i="0;" < n-1; ++i) for (int j="i" + 1; 0) string temp="name[i];" name[i]="name[j];" name[j]="temp;" system.out.println('
after performing lexicographical order: '); n; pre> <p> <strong>Code Explanation:</strong> </p> <p>In the above code, we have created a class Main within which the main () method is created.</p> <ul> <li>A string has been initialized, holding some values to it, and each word will get printed as per for loop.</li> <li>Then, we have implemented the main logic within another for loop with the help of which we can form the lexicographical order of the words given.</li> <li>Finally, via for loop, the arranged words are printed on the screen.</li> </ul> <p> <strong>On executing the above example code, we got the following output:</strong> </p> <img src="//techcodeview.com/img/java-tutorial/62/lexicographical-order-java.webp" alt="Lexicographical Order Java"> <p>From the output, we can analyze that the given sequence of the words was not in alphabetical order but after applying the lexicographical order code, we can see that every word is sequenced now in alphabetical order.</p> <p> <strong>Method 2: Applying sort () function</strong> </p> <p>The sort () method is available in the Arrays class within the util package.</p> <p>Below is the example code given that will let us understand that how we can perform sorting on elements in Lexicographical order:</p> <pre> import java.io.*; import java.util.Arrays; class Main { public static void printArray(String str[]) { for (String string : str) System.out.print(string + &apos; &apos;); System.out.println(); } public static void main(String[] args) { String arr[] = {&apos;John&apos;,&apos;Harry&apos;,&apos;Emlie&apos;,&apos;Ronny&apos;,&apos;Julie&apos;,&apos;Mary&apos; }; Arrays.sort(arr,String.CASE_INSENSITIVE_ORDER); printArray(arr); } } </pre> <p> <strong>On executing the above output, we got the below-shown output:</strong> </p> <img src="//techcodeview.com/img/java-tutorial/62/lexicographical-order-java-2.webp" alt="Lexicographical Order Java"> <h3>Comparing two strings using Lexicographical order in Java</h3> <p>For comparing two strings using Lexicographical order, we have the following two methods:</p> <p> <strong>Using compareTo () method</strong> </p> <p>Let&apos;s begin one by one:</p> <p> <strong>Using compareTo () method</strong> </p> <p>Below is an example implementation by which we can compare to strings lexicographically:</p> <pre> import java.lang.*; public class StringExample { public static void main(String[] args) { String str1 = &apos;String&apos;, str2 = &apos;Comparison&apos;; int get_val = str1.compareTo(str2); if (get_val <0) { system.out.println('str1 is greater than str2'); } else if (get_val="=" 0) equal to less < pre> <p> <strong>Code Explanation:</strong> </p> <ul> <li>We have created a class StringExample where we have implemented the main () method.</li> <li>We have initialized two strings, i.e., str1 and str2.</li> <li>Next, using the compareTo () method, we have compared the strings str1 and str2.</li> <li>After it, if the get_val value is found less than 0, it means str1 is greater than str2.</li> <li>Else if the get_val value is equal to 0, it means both str1 and str2 strings are equal.</li> <li>Else, both the strings str1 is less than str2.</li> </ul> <p> <strong>Output:</strong> </p> <img src="//techcodeview.com/img/java-tutorial/62/lexicographical-order-java-3.webp" alt="Lexicographical Order Java"> <p> <strong>By creating a user-defined function</strong> </p> <p>Below we have created a user-defined function using which we can compare two strings lexicographically. The code is as follows:</p> <pre> public class StringExample { public static void main(String[] args) { String firstString = &apos;Red&apos;; String secondString = &apos;Red&apos;; String thirdString = &apos;Green&apos;; String fourthString = &apos;Yellow&apos;; String fifthString = &apos;REdGreen&apos;; System.out.println(&apos;Comparing two strings lexicographically by user defined function&apos;); System.out.print(&apos;
Compairing firstString (&apos;+firstString+&apos;) to the secondString (&apos;+secondString+&apos;) returns: &apos;); System.out.println(compareString(firstString, secondString)); System.out.print(&apos;
Compairing secondString (&apos;+secondString+&apos;) to the thirdString (&apos;+thirdString+&apos;) returns: &apos;); System.out.println(compareString(secondString, thirdString)); System.out.print(&apos;
Compairing thirdString (&apos;+thirdString+&apos;) to the fourthString (&apos;+fourthString+&apos;) returns: &apos;); System.out.println(compareString(thirdString, fourthString)); System.out.print(&apos;
Compairing fourthString (&apos;+fourthString+&apos;) to the firstString (&apos;+firstString+&apos;) returns: &apos;); System.out.println(compareString(fourthString, firstString)); System.out.print(&apos;
Compairing firstString (&apos;+firstString+&apos;) to the fifthString (&apos;+fifthString+&apos;) returns: &apos;); System.out.println(compareString(firstString, fifthString)); } public static int compareString(String str, String argString) { int lim= Math.min(str.length(), argString.length()); int k=0; while(k<lim) { if(str.charat(k)!="argString.charAt(k))" return (int) str.charat(k)- argstring.charat(k); } k++; str.length() - argstring.length(); < pre> <p> <strong>Output:</strong> </p> <img src="//techcodeview.com/img/java-tutorial/62/lexicographical-order-java-4.webp" alt="Lexicographical Order Java"> <p> <strong>Code Explanation:</strong> </p> <ul> <li>We have created a Java class where we have initialized five strings.</li> <li>Next, we have compared the first string with the second string, the second to the third-string, and so on..</li> <li>For making the comparison, we have created a user-defined function compareString () whereby comparing the length and each character of the strings, and we got the results.</li> </ul> <p>Therefore, in this way, we can make use of the lexicographical order in Java for performing such tasks.</p> <hr></lim)></pre></0)></pre></n;>

Izpildot iepriekš minēto izvadi, mēs saņēmām tālāk parādīto izvadi:

Leksikogrāfiskais pasūtījums Java

Divu virkņu salīdzināšana, izmantojot Java leksikogrāfisko secību

Lai salīdzinātu divas virknes, izmantojot leksikogrāfisko secību, mums ir šādas divas metodes:

Izmantojot salīdzinātTo () metodi

Sāksim pa vienam:

Izmantojot salīdzinātTo () metodi

Tālāk ir sniegts īstenošanas piemērs, ar kuru mēs varam salīdzināt virknes leksikogrāfiski:

 import java.lang.*; public class StringExample { public static void main(String[] args) { String str1 = &apos;String&apos;, str2 = &apos;Comparison&apos;; int get_val = str1.compareTo(str2); if (get_val <0) { system.out.println(\'str1 is greater than str2\'); } else if (get_val="=" 0) equal to less < pre> <p> <strong>Code Explanation:</strong> </p> <ul> <li>We have created a class StringExample where we have implemented the main () method.</li> <li>We have initialized two strings, i.e., str1 and str2.</li> <li>Next, using the compareTo () method, we have compared the strings str1 and str2.</li> <li>After it, if the get_val value is found less than 0, it means str1 is greater than str2.</li> <li>Else if the get_val value is equal to 0, it means both str1 and str2 strings are equal.</li> <li>Else, both the strings str1 is less than str2.</li> </ul> <p> <strong>Output:</strong> </p> <img src="//techcodeview.com/img/java-tutorial/62/lexicographical-order-java-3.webp" alt="Lexicographical Order Java"> <p> <strong>By creating a user-defined function</strong> </p> <p>Below we have created a user-defined function using which we can compare two strings lexicographically. The code is as follows:</p> <pre> public class StringExample { public static void main(String[] args) { String firstString = &apos;Red&apos;; String secondString = &apos;Red&apos;; String thirdString = &apos;Green&apos;; String fourthString = &apos;Yellow&apos;; String fifthString = &apos;REdGreen&apos;; System.out.println(&apos;Comparing two strings lexicographically by user defined function&apos;); System.out.print(&apos;
Compairing firstString (&apos;+firstString+&apos;) to the secondString (&apos;+secondString+&apos;) returns: &apos;); System.out.println(compareString(firstString, secondString)); System.out.print(&apos;
Compairing secondString (&apos;+secondString+&apos;) to the thirdString (&apos;+thirdString+&apos;) returns: &apos;); System.out.println(compareString(secondString, thirdString)); System.out.print(&apos;
Compairing thirdString (&apos;+thirdString+&apos;) to the fourthString (&apos;+fourthString+&apos;) returns: &apos;); System.out.println(compareString(thirdString, fourthString)); System.out.print(&apos;
Compairing fourthString (&apos;+fourthString+&apos;) to the firstString (&apos;+firstString+&apos;) returns: &apos;); System.out.println(compareString(fourthString, firstString)); System.out.print(&apos;
Compairing firstString (&apos;+firstString+&apos;) to the fifthString (&apos;+fifthString+&apos;) returns: &apos;); System.out.println(compareString(firstString, fifthString)); } public static int compareString(String str, String argString) { int lim= Math.min(str.length(), argString.length()); int k=0; while(k<lim) { if(str.charat(k)!="argString.charAt(k))" return (int) str.charat(k)- argstring.charat(k); } k++; str.length() - argstring.length(); < pre> <p> <strong>Output:</strong> </p> <img src="//techcodeview.com/img/java-tutorial/62/lexicographical-order-java-4.webp" alt="Lexicographical Order Java"> <p> <strong>Code Explanation:</strong> </p> <ul> <li>We have created a Java class where we have initialized five strings.</li> <li>Next, we have compared the first string with the second string, the second to the third-string, and so on..</li> <li>For making the comparison, we have created a user-defined function compareString () whereby comparing the length and each character of the strings, and we got the results.</li> </ul> <p>Therefore, in this way, we can make use of the lexicographical order in Java for performing such tasks.</p> <hr></lim)></pre></0)>