Izraz leksikografski vrstni red je matematični izraz, znan pod imeni: leksikalni vrstni red, leksikografski (al) izdelek, abecedni vrstni red ali slovarski vrstni red.
Ta razdelek bo zajemal leksikografski vrstni red teme, njeno definicijo in druge podrobne informacije. Nato se bomo naučili uporabljati koncept leksikografskega reda v Programski jezik Java .
Določitev leksikografskega reda
Leksikografski red ali leksikografski v matematiki je posplošitev abecednega zaporedja slovarjev na zaporedja urejenih simbolov ali elementov popolnoma urejenega seznama. Izraz leksikografski red je motiviran z besedo leksikon. Leksikon je nabor besed, ki se uporablja v nekaterih drugih jezikih in ima konvencionalno vrstni red. Tako je leksikografski red način za formalizacijo besednega reda, kjer je podan vrstni red osnovnih simbolov.
V programiranju je leksikografski red popularno znan kot Slovarski vrstni red in se uporablja za razvrščanje matrike nizov, primerjavo dveh nizov ali razvrščanje elementov matrike. Leksikalno razvrščanje elementov postane zelo enostavno. To je zato, ker ima leksikografski red več različic in posplošitev, v katerih:
- Ena varianta je uporabna za zaporedja različnih dolžin, saj se pred obravnavo posameznih elementov primerjajo dolžine zaporedij.
- Druga različica se uporablja v vrstnih podmnožicah dane končne množice. To naredi tako, da končni množici dodeli skupni red. Nato pretvarja podmnožice v naraščajoča zaporedja, za katera se uporablja leksikografski vrstni red.
- Posplošitev se nanaša na kartezično produktno zaporedje delno urejenih množic in takšno zaporedje je totalni red, če in samo če je vsak faktor kartezičnega produkta popolnoma urejen.
Razumevanje formalnega pojma leksikografskega reda
- Da bi razumeli formalni pojem leksikografskega reda:
- Začne se s končno množico A, ki je znana kot abeceda in je popolnoma zaporedna. To nadalje pomeni, da za a in b (katera koli dva simbola, ki sta različna in nista enaka) v A, bodisi a
- Tu so besede A končno zaporedje simbolov iz A in vključujejo besede dolžine 1, ki imajo en simbol, besede dolžine 2 z dvema simboloma, za besede dolžine tri pa 3 itd. S spoštovanjem, vključuje tudi prazno zaporedje ? nimajo nobenih simbolov. Tako lahko leksikografski red za končno množico A opišemo kot:
- Recimo, da je za dva različna svetova enake dolžine a=a1a2…akin b=b1b2…bkje podan. Tu je vrstni red dveh besed odvisen od abecednega vrstnega reda simbolov na prvem mestu i, kjer se dve besedi spreminjata, če štejemo od začetka besed, tj. izpolnjujeta pogoj a jaz i v vrstnem redu abecede A.
- Če se dve besedi razlikujeta po dolžini, običajni leksikografski vrstni red dopolni besedo s krajšo dolžino s presledki na koncu, dokler obe besedi ne postaneta enako dolgi, nato pa se besede primerjajo.
Implementacija leksikografskega v Javi
Kot je razloženo zgoraj, se lahko leksikografski vrstni red uporablja za primerjavo dveh nizov ali razvrščanje elementov. Tukaj bomo razpravljali o obeh metodah in jih bomo izvajali.
Razvrščanje elementov v leksikografskem vrstnem redu
Urejanje besed po vrstnem redu je znano kot leksikografski red ali znan tudi kot Slovarski vrstni red . To pomeni, da so pri uporabi leksikografskega reda besede razvrščene po abecedi glede na njihove sestavne abecede. Za razvrščanje matrike nizov v leksikografskem vrstnem redu imamo na voljo naslednji dve metodi:
1. način: uporaba katere koli metode razvrščanja
Spodaj je podana vzorčna koda, ki nam bo omogočila razumeti, kako lahko izvedemo razvrščanje elementov v leksikografskem vrstnem redu:
public class Main { public static void main(String[] args) { String[] name = { 'John','Remo','Mixy','Julie','Ronny'}; int n = 5; System.out.println('Before Sorting'); 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 + ' '); System.out.println(); } public static void main(String[] args) { String arr[] = {'John','Harry','Emlie','Ronny','Julie','Mary' }; 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'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 = 'String', str2 = 'Comparison'; 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 = 'Red'; String secondString = 'Red'; String thirdString = 'Green'; String fourthString = 'Yellow'; String fifthString = 'REdGreen'; System.out.println('Comparing two strings lexicographically by user defined function'); System.out.print(' Compairing firstString ('+firstString+') to the secondString ('+secondString+') returns: '); System.out.println(compareString(firstString, secondString)); System.out.print(' Compairing secondString ('+secondString+') to the thirdString ('+thirdString+') returns: '); System.out.println(compareString(secondString, thirdString)); System.out.print(' Compairing thirdString ('+thirdString+') to the fourthString ('+fourthString+') returns: '); System.out.println(compareString(thirdString, fourthString)); System.out.print(' Compairing fourthString ('+fourthString+') to the firstString ('+firstString+') returns: '); System.out.println(compareString(fourthString, firstString)); System.out.print(' Compairing firstString ('+firstString+') to the fifthString ('+fifthString+') returns: '); 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;>
Pri izvedbi zgornjega izhoda smo dobili spodaj prikazan izhod:
Primerjava dveh nizov z uporabo leksikografskega reda v Javi
Za primerjavo dveh nizov z uporabo leksikografskega reda imamo na voljo naslednji dve metodi:
Uporaba metode compareTo ().
Začnimo enega za drugim:
Uporaba metode compareTo ().
Spodaj je primer izvedbe, s katerim lahko leksikografsko primerjamo nize:
import java.lang.*; public class StringExample { public static void main(String[] args) { String str1 = 'String', str2 = 'Comparison'; 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 = 'Red'; String secondString = 'Red'; String thirdString = 'Green'; String fourthString = 'Yellow'; String fifthString = 'REdGreen'; System.out.println('Comparing two strings lexicographically by user defined function'); System.out.print(' Compairing firstString ('+firstString+') to the secondString ('+secondString+') returns: '); System.out.println(compareString(firstString, secondString)); System.out.print(' Compairing secondString ('+secondString+') to the thirdString ('+thirdString+') returns: '); System.out.println(compareString(secondString, thirdString)); System.out.print(' Compairing thirdString ('+thirdString+') to the fourthString ('+fourthString+') returns: '); System.out.println(compareString(thirdString, fourthString)); System.out.print(' Compairing fourthString ('+fourthString+') to the firstString ('+firstString+') returns: '); System.out.println(compareString(fourthString, firstString)); System.out.print(' Compairing firstString ('+firstString+') to the fifthString ('+fifthString+') returns: '); 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)>0)>