From 38daad8fa98615b8a6e686885a0a4a6452f59543 Mon Sep 17 00:00:00 2001 From: siddhant2002 Date: Sun, 5 Dec 2021 20:16:29 +0530 Subject: [PATCH 1/9] Fixes: #{2391} --- .../com/thealgorithms/strings/Anagrams.java | 150 ++++++++++++++++++ 1 file changed, 150 insertions(+) create mode 100644 src/main/java/com/thealgorithms/strings/Anagrams.java diff --git a/src/main/java/com/thealgorithms/strings/Anagrams.java b/src/main/java/com/thealgorithms/strings/Anagrams.java new file mode 100644 index 000000000000..1db3bf1b7bf2 --- /dev/null +++ b/src/main/java/com/thealgorithms/strings/Anagrams.java @@ -0,0 +1,150 @@ +/** Author : Siddhant Swarup Mallick + * Github : https://github.com/siddhant2002 + */ + +/** PROBLEM DESCRIPTION : + * An anagram is a word or phrase formed by rearranging the letters of a different word or phrase, typically using all the original letters exactly once.[1] For example, the word anagram itself can be rearranged into nag a ram, also the word binary into brainy and the word adobe into abode. Reference from https://en.wikipedia.org/wiki/Anagram + */ + +package com.thealgorithms.strings; +import java.util.*; +public class Anagrams +{ + // 4 approaches are provided for anagram checking. approach 2 and approach 3 are similar but differ in running time. + public static void main(String args[]) { + String first = "deal"; + String second = "lead"; + // All the below methods takes input but doesn't return any output to the main method. + Anagrams nm=new Anagrams(); + nm.approach2(first, second); /* To activate methods for different approaches*/ + nm.approach1(first, second); /* To activate methods for different approaches*/ + nm.approach3(first, second); /* To activate methods for different approaches*/ + nm.approach4(first, second); /* To activate methods for different approaches*/ + + /** + * OUTPUT : + * first string ="deal" second string ="lead" + * Output: Anagram + * Input and output is constant for all four approaches + * 1st approach Time Complexity : O(n logn) + * Auxiliary Space Complexity : O(1) + * 2nd approach Time Complexity : O(n) + * Auxiliary Space Complexity : O(1) + * 3rd approach Time Complexity : O(n) + * Auxiliary Space Complexity : O(1) + * 4th approach Time Complexity : O(n) + * Auxiliary Space Complexity : O(n) + */ + } + + void approach1(String s, String t) + { + if (s.length() != t.length()) + { + System.out.println(false); + } + else + { + char c[] = s.toCharArray(); + char d[] = t.toCharArray(); + Arrays.sort(c); + Arrays.sort(d); /* In this approach the strings are stored in the character arrays and both the arrays are sorted. After that both the arrays are compared for checking anangram */ + if (Arrays.equals(c, d)) + { + System.out.println("Anagram"); + } else + { + System.out.println("Not Anagram"); + } + } + } + + void approach2(String a, String b) + { + if(a.length()!=b.length()) + { + System.out.println("Not Anagram"); + } + else + { + int m[]=new int[26]; + int n[]=new int[26]; + for(char c: a.toCharArray()) + { + m[c-'a']++; + } + // In this approach the frequency of both the strings are stored and after that the frequencies are iterated from 0 to 26(from 'a' to 'z' ). If the frequencies match then anagram message is displayed + // Running time and space complexity of this algo is less as compared to others + for(char c:b.toCharArray()) + { + n[c-'a']++; + } + for(int i=0;i<26;i++) + { + if(m[i]!=n[i]) + { + System.out.println("Not Anagram"); + } + } + System.out.println("Anagram"); + } + } + + void approach3(String s, String t) + { + if(s.length()!=t.length()) + { + System.out.println("Not Anagram"); + } + // this is similar to approach number 2 but here the string is not converted to character array + else + { + int a[]=new int[26]; + int b[]=new int[26]; + int k=s.length(); + for(int i=0;i nm=new HashMap<>(); + HashMap kk=new HashMap<>(); + for(char c: s.toCharArray()) + { + nm.put(c, nm.getOrDefault(c,0)+1); + } + for(char c: t.toCharArray()) + { + + kk.put(c, kk.getOrDefault(c,0)+1); + } + // It checks for equal frequencies + for(char c:nm.keySet()) + { + if(!nm.get(c).equals(kk.get(c))) + { + System.out.println("Not Anagram"); + } + } + System.out.println("Anagram"); + } + } +} \ No newline at end of file From a4172291f5745bd11c02580f9468225edf07bf2c Mon Sep 17 00:00:00 2001 From: siddhant2002 Date: Tue, 7 Dec 2021 18:21:15 +0530 Subject: [PATCH 2/9] Fixes #(2391) --- .../com/thealgorithms/strings/Anagrams.java | 44 +++++++++---------- .../strings/String_Comparision.java | 0 2 files changed, 22 insertions(+), 22 deletions(-) create mode 100644 src/main/java/com/thealgorithms/strings/String_Comparision.java diff --git a/src/main/java/com/thealgorithms/strings/Anagrams.java b/src/main/java/com/thealgorithms/strings/Anagrams.java index 1db3bf1b7bf2..f76db06c4d58 100644 --- a/src/main/java/com/thealgorithms/strings/Anagrams.java +++ b/src/main/java/com/thealgorithms/strings/Anagrams.java @@ -16,10 +16,10 @@ public static void main(String args[]) { String second = "lead"; // All the below methods takes input but doesn't return any output to the main method. Anagrams nm=new Anagrams(); - nm.approach2(first, second); /* To activate methods for different approaches*/ - nm.approach1(first, second); /* To activate methods for different approaches*/ - nm.approach3(first, second); /* To activate methods for different approaches*/ - nm.approach4(first, second); /* To activate methods for different approaches*/ + System.out.println(nm.approach2(first, second)); /* To activate methods for different approaches*/ + System.out.println(nm.approach1(first, second)); /* To activate methods for different approaches*/ + System.out.println(nm.approach3(first, second)); /* To activate methods for different approaches*/ + System.out.println(nm.approach4(first, second)); /* To activate methods for different approaches*/ /** * OUTPUT : @@ -37,11 +37,11 @@ public static void main(String args[]) { */ } - void approach1(String s, String t) + boolean approach1(String s, String t) { if (s.length() != t.length()) { - System.out.println(false); + return false; } else { @@ -51,19 +51,19 @@ void approach1(String s, String t) Arrays.sort(d); /* In this approach the strings are stored in the character arrays and both the arrays are sorted. After that both the arrays are compared for checking anangram */ if (Arrays.equals(c, d)) { - System.out.println("Anagram"); + return true; } else { - System.out.println("Not Anagram"); + return false; } } } - void approach2(String a, String b) + boolean approach2(String a, String b) { if(a.length()!=b.length()) { - System.out.println("Not Anagram"); + return false; } else { @@ -73,7 +73,7 @@ void approach2(String a, String b) { m[c-'a']++; } - // In this approach the frequency of both the strings are stored and after that the frequencies are iterated from 0 to 26(from 'a' to 'z' ). If the frequencies match then anagram message is displayed + // In this approach the frequency of both the strings are stored and after that the frequencies are iterated from 0 to 26(from 'a' to 'z' ). If the frequencies match then anagram message is displayed in the form of boolean format // Running time and space complexity of this algo is less as compared to others for(char c:b.toCharArray()) { @@ -83,18 +83,18 @@ void approach2(String a, String b) { if(m[i]!=n[i]) { - System.out.println("Not Anagram"); + return false; } } - System.out.println("Anagram"); + return true; } } - void approach3(String s, String t) + boolean approach3(String s, String t) { if(s.length()!=t.length()) { - System.out.println("Not Anagram"); + return false; } // this is similar to approach number 2 but here the string is not converted to character array else @@ -110,19 +110,19 @@ void approach3(String s, String t) for(int i=0;i<26;i++) { if(a[i]!=b[i]) - System.out.println("Not Anagram"); + return false; } - System.out.println("Anagram"); + return true; } } - void approach4(String s, String t) + boolean approach4(String s, String t) { if(s.length()!=t.length()) { - System.out.println("Not Anagram"); + return false; } - // This approach is done using hashmap where frequencies are stored and checked iteratively and if all the frequencies of first string match with the second string then anagram message is displayed + // This approach is done using hashmap where frequencies are stored and checked iteratively and if all the frequencies of first string match with the second string then anagram message is displayed in boolean format else { HashMap nm=new HashMap<>(); @@ -141,10 +141,10 @@ void approach4(String s, String t) { if(!nm.get(c).equals(kk.get(c))) { - System.out.println("Not Anagram"); + return false; } } - System.out.println("Anagram"); + return true; } } } \ No newline at end of file diff --git a/src/main/java/com/thealgorithms/strings/String_Comparision.java b/src/main/java/com/thealgorithms/strings/String_Comparision.java new file mode 100644 index 000000000000..e69de29bb2d1 From e3c28dc4a8b56687faf4d82952bb5e881b2a689b Mon Sep 17 00:00:00 2001 From: Andrii Siriak Date: Thu, 9 Dec 2021 20:45:40 +0200 Subject: [PATCH 3/9] Delete String_Comparision.java --- src/main/java/com/thealgorithms/strings/String_Comparision.java | 0 1 file changed, 0 insertions(+), 0 deletions(-) delete mode 100644 src/main/java/com/thealgorithms/strings/String_Comparision.java diff --git a/src/main/java/com/thealgorithms/strings/String_Comparision.java b/src/main/java/com/thealgorithms/strings/String_Comparision.java deleted file mode 100644 index e69de29bb2d1..000000000000 From f27eff41930233b95dc0a636c61c42ea9e6c9cba Mon Sep 17 00:00:00 2001 From: siddhant2002 Date: Tue, 14 Dec 2021 09:24:30 +0530 Subject: [PATCH 4/9] Fixes: #{2820} --- .../thealgorithms/strings/String_Compare.java | 47 ++++++++++++++ .../strings/String_Compare1.java | 62 +++++++++++++++++++ .../strings/String_Compare2.java | 43 +++++++++++++ 3 files changed, 152 insertions(+) create mode 100644 src/main/java/com/thealgorithms/strings/String_Compare.java create mode 100644 src/main/java/com/thealgorithms/strings/String_Compare1.java create mode 100644 src/main/java/com/thealgorithms/strings/String_Compare2.java diff --git a/src/main/java/com/thealgorithms/strings/String_Compare.java b/src/main/java/com/thealgorithms/strings/String_Compare.java new file mode 100644 index 000000000000..0b0b4bfb9fa8 --- /dev/null +++ b/src/main/java/com/thealgorithms/strings/String_Compare.java @@ -0,0 +1,47 @@ +/** Author : Siddhant Swarup Mallick + * Github : https://github.com/siddhant2002 + */ + + /** Program description - To sort the names alphabetically using comparators and collection class + */ + package com.thealgorithms.strings; +import java.util.*; +public class String_Compare { + public static void main(String args[]) + { + // list is created of string type where names are initialized + List nm = new ArrayList<>(); + nm.add("Shah"); + nm.add("Chhotwant"); + nm.add("Ayush"); + nm.add("Mannu"); + nm.add("Sachin"); + nm.add("Siddhi"); + nm.add("Urus"); + nm.add("Mony"); + String_Compare3 kk=new String_Compare3(); + nm=kk.task(nm); + System.out.println(nm); + // task method is executed and the returned result is stored in the list and after that the list is displayed + + /** + * OUTPUT : + * Input - ["Shah","Chhotwant","Ayush","Mannu","Sachin","Siddhi","Urus","Mony"] + * Output: [Ayush, Chhotwant, Mannu, Mony, Sachin, Shah, Siddhi, Urus] + * 1st approach Time Complexity : O(n logn) + * Auxiliary Space Complexity : O(n) + * */ + } + List task(List nm) + { + Collections.sort(nm , new Comparator(){ + public int compare(String s1 , String s2) + { + return s1.compareTo(s2); + } + }); + return nm; + + // comparator method is used to sort the names in ascending order/alphabetically. To display in reverse order then line number 32 should be changed to s2.compareTo(s1) + } +} \ No newline at end of file diff --git a/src/main/java/com/thealgorithms/strings/String_Compare1.java b/src/main/java/com/thealgorithms/strings/String_Compare1.java new file mode 100644 index 000000000000..8da5d26d9785 --- /dev/null +++ b/src/main/java/com/thealgorithms/strings/String_Compare1.java @@ -0,0 +1,62 @@ +/** Author : Siddhant Swarup Mallick + * Github : https://github.com/siddhant2002 + */ + + /** Program description - To check strings as per object values and string content + */ + + +package com.thealgorithms.strings; +public class String_Compare1 { + public static void main(String args[]) + { + // 4 methods are called which are assigned to different sort of task and the return type is in boolean + String_Compare1 nm=new String_Compare1(); + String s1="Alpha"; + String s2="Alpha"; + String s3=new String(s2); + System.out.println(nm.task(s1,s2)); + System.out.println(nm.task1(s1,s3)); + System.out.println(nm.task2(s1,s2)); + System.out.println(nm.task3(s1,s3)); + + /** + * OUTPUT : + * The input is common for all the methods + * first string ="Alpha" second string ="Alpha" third string ="Alpha" + * Output: true/false + * 1st Output - true + * 1st approach Time Complexity : O(1) + * Auxiliary Space Complexity : O(1) + * 2nd output - false + * 2nd approach Time Complexity : O(1) + * Auxiliary Space Complexity : O(1) + * 3rd output - true + * 3rd approach Time Complexity : O(1) + * Auxiliary Space Complexity : O(1) + * 4th output - true + * 4th approach Time Complexity : O(1) + * Auxiliary Space Complexity : O(1) + */ + } + boolean task(String s1, String s2) + { + return s1==s2; + // Here the answer is true because the object of strings s1 and s2 are same so the object value is checked instead of their content + } + boolean task1(String s1, String s2) + { + return s1==s2; + // Here the answer is false because the object of strings s1 and s2 are different so the object value is checked instead of their content + } + boolean task2(String s1, String s2) + { + return s1.equals(s2); + // Here the answer is true because the content of the strings are checked irrespective of their objects location + } + boolean task3(String s1, String s2) + { + return s1.equals(s2); + // Here the answer is true because the content of the strings are checked irrespective of their objects location + } +} diff --git a/src/main/java/com/thealgorithms/strings/String_Compare2.java b/src/main/java/com/thealgorithms/strings/String_Compare2.java new file mode 100644 index 000000000000..c678d67cf2bb --- /dev/null +++ b/src/main/java/com/thealgorithms/strings/String_Compare2.java @@ -0,0 +1,43 @@ +/** Author : Siddhant Swarup Mallick + * Github : https://github.com/siddhant2002 + */ + + /** Program description - Showing compareTo function in 2 different ways. In first method using compareTo function to generate the length difference and in the second method compareTo function is used to generate the difference between the ascii codes of the characters of the strings + */ + +package com.thealgorithms.strings; +public class String_Compare2 { + public static void main(String args[]) + { + // 2 methods are called to execute the functions and return is in integer + String_Compare2 nm=new String_Compare2(); + String s1="Alpha123"; + String s2="Alpha"; + System.out.println(nm.task(s1,s2)); + s1="Highway"; + s2="Harry"; + System.out.println(nm.task(s1,s2)); + + /** + * OUTPUT : + * 1st input - 1st string="Alpha123" 2nd string="Alpha" + * 1st Output - 3 + * 1st approach Time Complexity : O(1) + * Auxiliary Space Complexity : O(1) + * 2nd input - 1st string="Highway" 2nd string="Harry" + * 2nd output - 8 + * 2nd approach Time Complexity : O(1) + * Auxiliary Space Complexity : O(1) + * */ + } + int task(String s1, String s2) + { + return s1.compareTo(s2); + // Here the output will be displayed as per the length between the two strings. So 3 will be displayed + } + int task1(String s1, String s2) + { + return s1.compareTo(s2); + // Here the output will be displayed as per the ascii code the characters present in both the strings. So 8 will be the output + } +} \ No newline at end of file From 9e77af519aa0f2eb5d1ebc6e0eabee250d7d0f9b Mon Sep 17 00:00:00 2001 From: siddhant2002 Date: Tue, 14 Dec 2021 12:34:10 +0530 Subject: [PATCH 5/9] Fixes #{2820} --- src/main/java/com/thealgorithms/strings/String_Compare.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/main/java/com/thealgorithms/strings/String_Compare.java b/src/main/java/com/thealgorithms/strings/String_Compare.java index 0b0b4bfb9fa8..967a10091fdb 100644 --- a/src/main/java/com/thealgorithms/strings/String_Compare.java +++ b/src/main/java/com/thealgorithms/strings/String_Compare.java @@ -19,7 +19,7 @@ public static void main(String args[]) nm.add("Siddhi"); nm.add("Urus"); nm.add("Mony"); - String_Compare3 kk=new String_Compare3(); + String_Compare kk=new String_Compare(); nm=kk.task(nm); System.out.println(nm); // task method is executed and the returned result is stored in the list and after that the list is displayed From a192de6811d0c54ef057e251e8c51432d89c6584 Mon Sep 17 00:00:00 2001 From: siddhant2002 Date: Mon, 20 Dec 2021 10:39:44 +0530 Subject: [PATCH 6/9] Fixes(#2450) --- .../thealgorithms/sorts/LinkList_Sort.java | 301 ++++++++++++++++++ 1 file changed, 301 insertions(+) create mode 100644 src/main/java/com/thealgorithms/sorts/LinkList_Sort.java diff --git a/src/main/java/com/thealgorithms/sorts/LinkList_Sort.java b/src/main/java/com/thealgorithms/sorts/LinkList_Sort.java new file mode 100644 index 000000000000..5e23b2baf97f --- /dev/null +++ b/src/main/java/com/thealgorithms/sorts/LinkList_Sort.java @@ -0,0 +1,301 @@ +/** Author : Siddhant Swarup Mallick + * Github : https://github.com/siddhant2002 + */ + + /** Program description - To sort the LinkList as per sorting technique */ + +package com.thealgorithms.sorts; + +public class LinkList_Sort { + public static void main(String args[]) + { + int a[] = {89,56,98,123,26,75,12,40,39,68,91}; + // Array is taken as input + int ch=1; + // Choice is choosed as 1(Merge sort) + switch(ch) + { + case 1: + Task nm=new Task(); + Node start=null,prev=null,fresh,ptr; + for(int i=0;i=n[i]) + b[k++]=n[i++]; + else + b[k++]=n[j++]; + } + // Smallest number is stored after checking from both the arrays + while(i<=m) + { + b[k++]=n[i++]; + } + while(j<=e) + { + b[k++]=n[j++]; + } + for(int p=s;p<=e;p++) + { + a[p]=b[p-s]; + } + } + // The method task and task1 is used to sort the linklist using merge sort +} +class Task1 +{ + public Node sort_by_insertionsort(Node head) { + if(head==null||head.next==null) + return head; + int c=count(head); + int a[]=new int[c]; + // Array of size c is created + a[0]=head.val; + int i; + Node ptr; + for(ptr=head.next,i=1;ptr!=null;ptr=ptr.next,i++) + { + int j=i-1; + while(j>=0&&a[j]>ptr.val) + { + // values are stored in the array + a[j+1]=a[j]; + j--; + } + a[j+1]=ptr.val; + } + i=0; + for(ptr=head;ptr!=null;ptr=ptr.next) + { + ptr.val=a[i++]; + // Value is stored in the linklist after being sorted + } + return head; + } + static int count(Node head) + { + Node ptr; + int c=0; + for(ptr=head;ptr!=null;ptr=ptr.next) + { + c++; + } + return c; + // This Method is used to count number of elements/nodes present in the linklist + // It will return a integer type value denoting the number of nodes present + } + // The method task and task1 is used to sort the linklist using insertion sort +} +class Task2 +{ + static int a[]; + public Node sort_by_heapsort(Node head) { + if(head==null||head.next==null) + return head; + int c=count(head); + a=new int[c]; + // Array of size c is created + int i=0; + for(Node ptr=head;ptr!=null;ptr=ptr.next) + { + a[i++]=ptr.val; + // values are stored in the array + } + i=0; + task(a); + for(Node ptr=head;ptr!=null;ptr=ptr.next) + { + ptr.val=a[i++]; + // Value is stored in the linklist after being sorted + } + return head; + } + int count(Node head) + { + int c=0; + Node ptr; + for(ptr=head;ptr!=null;ptr=ptr.next) + { + c++; + } + return c; + // This Method is used to count number of elements/nodes present in the linklist + // It will return a integer type value denoting the number of nodes present + } + void task(int n[]) + { + int k=n.length; + for(int i=k/2-1;i>=0;i--) + { + task1(n,k,i); + } + for(int i=k-1;i>0;i--) + { + int d=n[0]; + n[0]=n[i]; + n[i]=d; + task1(n,i,0); + // recursive calling of task1 method + } + } + void task1(int n[], int k, int i) + { + int p=i; + int l=2*i+1; + int r=2*i+2; + if(ln[p]) + p=l; + if(rn[p]) + p=r; + if(p!=i) + { + int d=n[p]; + n[p]=n[i]; + n[i]=d; + task1(n,k,p); + } + } + // The method task and task1 is used to sort the linklist using heap sort +} From 10e58e39a7bef930c7480b2eedb56fc550034063 Mon Sep 17 00:00:00 2001 From: siddhant2002 Date: Tue, 21 Dec 2021 08:02:04 +0530 Subject: [PATCH 7/9] string Codes removed --- .../thealgorithms/strings/String_Compare.java | 47 -------------- .../strings/String_Compare1.java | 62 ------------------- .../strings/String_Compare2.java | 43 ------------- 3 files changed, 152 deletions(-) delete mode 100644 src/main/java/com/thealgorithms/strings/String_Compare.java delete mode 100644 src/main/java/com/thealgorithms/strings/String_Compare1.java delete mode 100644 src/main/java/com/thealgorithms/strings/String_Compare2.java diff --git a/src/main/java/com/thealgorithms/strings/String_Compare.java b/src/main/java/com/thealgorithms/strings/String_Compare.java deleted file mode 100644 index 967a10091fdb..000000000000 --- a/src/main/java/com/thealgorithms/strings/String_Compare.java +++ /dev/null @@ -1,47 +0,0 @@ -/** Author : Siddhant Swarup Mallick - * Github : https://github.com/siddhant2002 - */ - - /** Program description - To sort the names alphabetically using comparators and collection class - */ - package com.thealgorithms.strings; -import java.util.*; -public class String_Compare { - public static void main(String args[]) - { - // list is created of string type where names are initialized - List nm = new ArrayList<>(); - nm.add("Shah"); - nm.add("Chhotwant"); - nm.add("Ayush"); - nm.add("Mannu"); - nm.add("Sachin"); - nm.add("Siddhi"); - nm.add("Urus"); - nm.add("Mony"); - String_Compare kk=new String_Compare(); - nm=kk.task(nm); - System.out.println(nm); - // task method is executed and the returned result is stored in the list and after that the list is displayed - - /** - * OUTPUT : - * Input - ["Shah","Chhotwant","Ayush","Mannu","Sachin","Siddhi","Urus","Mony"] - * Output: [Ayush, Chhotwant, Mannu, Mony, Sachin, Shah, Siddhi, Urus] - * 1st approach Time Complexity : O(n logn) - * Auxiliary Space Complexity : O(n) - * */ - } - List task(List nm) - { - Collections.sort(nm , new Comparator(){ - public int compare(String s1 , String s2) - { - return s1.compareTo(s2); - } - }); - return nm; - - // comparator method is used to sort the names in ascending order/alphabetically. To display in reverse order then line number 32 should be changed to s2.compareTo(s1) - } -} \ No newline at end of file diff --git a/src/main/java/com/thealgorithms/strings/String_Compare1.java b/src/main/java/com/thealgorithms/strings/String_Compare1.java deleted file mode 100644 index 8da5d26d9785..000000000000 --- a/src/main/java/com/thealgorithms/strings/String_Compare1.java +++ /dev/null @@ -1,62 +0,0 @@ -/** Author : Siddhant Swarup Mallick - * Github : https://github.com/siddhant2002 - */ - - /** Program description - To check strings as per object values and string content - */ - - -package com.thealgorithms.strings; -public class String_Compare1 { - public static void main(String args[]) - { - // 4 methods are called which are assigned to different sort of task and the return type is in boolean - String_Compare1 nm=new String_Compare1(); - String s1="Alpha"; - String s2="Alpha"; - String s3=new String(s2); - System.out.println(nm.task(s1,s2)); - System.out.println(nm.task1(s1,s3)); - System.out.println(nm.task2(s1,s2)); - System.out.println(nm.task3(s1,s3)); - - /** - * OUTPUT : - * The input is common for all the methods - * first string ="Alpha" second string ="Alpha" third string ="Alpha" - * Output: true/false - * 1st Output - true - * 1st approach Time Complexity : O(1) - * Auxiliary Space Complexity : O(1) - * 2nd output - false - * 2nd approach Time Complexity : O(1) - * Auxiliary Space Complexity : O(1) - * 3rd output - true - * 3rd approach Time Complexity : O(1) - * Auxiliary Space Complexity : O(1) - * 4th output - true - * 4th approach Time Complexity : O(1) - * Auxiliary Space Complexity : O(1) - */ - } - boolean task(String s1, String s2) - { - return s1==s2; - // Here the answer is true because the object of strings s1 and s2 are same so the object value is checked instead of their content - } - boolean task1(String s1, String s2) - { - return s1==s2; - // Here the answer is false because the object of strings s1 and s2 are different so the object value is checked instead of their content - } - boolean task2(String s1, String s2) - { - return s1.equals(s2); - // Here the answer is true because the content of the strings are checked irrespective of their objects location - } - boolean task3(String s1, String s2) - { - return s1.equals(s2); - // Here the answer is true because the content of the strings are checked irrespective of their objects location - } -} diff --git a/src/main/java/com/thealgorithms/strings/String_Compare2.java b/src/main/java/com/thealgorithms/strings/String_Compare2.java deleted file mode 100644 index c678d67cf2bb..000000000000 --- a/src/main/java/com/thealgorithms/strings/String_Compare2.java +++ /dev/null @@ -1,43 +0,0 @@ -/** Author : Siddhant Swarup Mallick - * Github : https://github.com/siddhant2002 - */ - - /** Program description - Showing compareTo function in 2 different ways. In first method using compareTo function to generate the length difference and in the second method compareTo function is used to generate the difference between the ascii codes of the characters of the strings - */ - -package com.thealgorithms.strings; -public class String_Compare2 { - public static void main(String args[]) - { - // 2 methods are called to execute the functions and return is in integer - String_Compare2 nm=new String_Compare2(); - String s1="Alpha123"; - String s2="Alpha"; - System.out.println(nm.task(s1,s2)); - s1="Highway"; - s2="Harry"; - System.out.println(nm.task(s1,s2)); - - /** - * OUTPUT : - * 1st input - 1st string="Alpha123" 2nd string="Alpha" - * 1st Output - 3 - * 1st approach Time Complexity : O(1) - * Auxiliary Space Complexity : O(1) - * 2nd input - 1st string="Highway" 2nd string="Harry" - * 2nd output - 8 - * 2nd approach Time Complexity : O(1) - * Auxiliary Space Complexity : O(1) - * */ - } - int task(String s1, String s2) - { - return s1.compareTo(s2); - // Here the output will be displayed as per the length between the two strings. So 3 will be displayed - } - int task1(String s1, String s2) - { - return s1.compareTo(s2); - // Here the output will be displayed as per the ascii code the characters present in both the strings. So 8 will be the output - } -} \ No newline at end of file From d24fdf2852fb50f39a1bd707aaeed9e9b24fb562 Mon Sep 17 00:00:00 2001 From: siddhant2002 Date: Wed, 22 Dec 2021 23:12:10 +0530 Subject: [PATCH 8/9] fixes(#2882) --- .../thealgorithms/sorts/LinkList_Sort.java | 405 +++++++++--------- 1 file changed, 202 insertions(+), 203 deletions(-) diff --git a/src/main/java/com/thealgorithms/sorts/LinkList_Sort.java b/src/main/java/com/thealgorithms/sorts/LinkList_Sort.java index 5e23b2baf97f..fc05120c1fb6 100644 --- a/src/main/java/com/thealgorithms/sorts/LinkList_Sort.java +++ b/src/main/java/com/thealgorithms/sorts/LinkList_Sort.java @@ -2,224 +2,228 @@ * Github : https://github.com/siddhant2002 */ - /** Program description - To sort the LinkList as per sorting technique */ +/** Program description - To sort the LinkList as per sorting technique */ package com.thealgorithms.sorts; public class LinkList_Sort { - public static void main(String args[]) - { - int a[] = {89,56,98,123,26,75,12,40,39,68,91}; + public static void main(String args[]) { + int a[] = { 89, 56, 98, 123, 26, 75, 12, 40, 39, 68, 91 }; // Array is taken as input - int ch=1; - // Choice is choosed as 1(Merge sort) - switch(ch) - { + int b[] = { 12, 26, 39, 40, 56, 68, 75, 89, 91, 98, 123 }; + // Excepted Array + int ch = 1; + // Choice is choosed as 1 (So the linked list will be sorted by Merge sort technique) + switch (ch) { case 1: - Task nm=new Task(); - Node start=null,prev=null,fresh,ptr; - for(int i=0;i=n[i]) - b[k++]=n[i++]; + + void task1(int n[], int s, int m, int e) { + int i = s, k = 0, j = m + 1; + int b[] = new int[e - s + 1]; + while (i <= m && j <= e) { + if (n[j] >= n[i]) + b[k++] = n[i++]; else - b[k++]=n[j++]; + b[k++] = n[j++]; } // Smallest number is stored after checking from both the arrays - while(i<=m) - { - b[k++]=n[i++]; + while (i <= m) { + b[k++] = n[i++]; } - while(j<=e) - { - b[k++]=n[j++]; + while (j <= e) { + b[k++] = n[j++]; } - for(int p=s;p<=e;p++) - { - a[p]=b[p-s]; + for (int p = s; p <= e; p++) { + a[p] = b[p - s]; } } // The method task and task1 is used to sort the linklist using merge sort } -class Task1 -{ + +class Task1 { public Node sort_by_insertionsort(Node head) { - if(head==null||head.next==null) + if (head == null || head.next == null) return head; - int c=count(head); - int a[]=new int[c]; - // Array of size c is created - a[0]=head.val; + int c = count(head); + int a[] = new int[c]; + // Array of size c is created + a[0] = head.val; int i; Node ptr; - for(ptr=head.next,i=1;ptr!=null;ptr=ptr.next,i++) - { - int j=i-1; - while(j>=0&&a[j]>ptr.val) - { + for (ptr = head.next, i = 1; ptr != null; ptr = ptr.next, i++) { + int j = i - 1; + while (j >= 0 && a[j] > ptr.val) { // values are stored in the array - a[j+1]=a[j]; + a[j + 1] = a[j]; j--; } - a[j+1]=ptr.val; + a[j + 1] = ptr.val; } - i=0; - for(ptr=head;ptr!=null;ptr=ptr.next) - { - ptr.val=a[i++]; - // Value is stored in the linklist after being sorted + i = 0; + for (ptr = head; ptr != null; ptr = ptr.next) { + ptr.val = a[i++]; + // Value is stored in the linklist after being sorted } return head; } - static int count(Node head) - { + + static int count(Node head) { Node ptr; - int c=0; - for(ptr=head;ptr!=null;ptr=ptr.next) - { + int c = 0; + for (ptr = head; ptr != null; ptr = ptr.next) { c++; } return c; @@ -228,73 +232,68 @@ static int count(Node head) } // The method task and task1 is used to sort the linklist using insertion sort } -class Task2 -{ + +class Task2 { static int a[]; + public Node sort_by_heapsort(Node head) { - if(head==null||head.next==null) + if (head == null || head.next == null) return head; - int c=count(head); - a=new int[c]; - // Array of size c is created - int i=0; - for(Node ptr=head;ptr!=null;ptr=ptr.next) - { - a[i++]=ptr.val; + int c = count(head); + a = new int[c]; + // Array of size c is created + int i = 0; + for (Node ptr = head; ptr != null; ptr = ptr.next) { + a[i++] = ptr.val; // values are stored in the array } - i=0; + i = 0; task(a); - for(Node ptr=head;ptr!=null;ptr=ptr.next) - { - ptr.val=a[i++]; + for (Node ptr = head; ptr != null; ptr = ptr.next) { + ptr.val = a[i++]; // Value is stored in the linklist after being sorted } return head; } - int count(Node head) - { - int c=0; + + int count(Node head) { + int c = 0; Node ptr; - for(ptr=head;ptr!=null;ptr=ptr.next) - { + for (ptr = head; ptr != null; ptr = ptr.next) { c++; } return c; // This Method is used to count number of elements/nodes present in the linklist // It will return a integer type value denoting the number of nodes present } - void task(int n[]) - { - int k=n.length; - for(int i=k/2-1;i>=0;i--) - { - task1(n,k,i); + + void task(int n[]) { + int k = n.length; + for (int i = k / 2 - 1; i >= 0; i--) { + task1(n, k, i); } - for(int i=k-1;i>0;i--) - { - int d=n[0]; - n[0]=n[i]; - n[i]=d; - task1(n,i,0); + for (int i = k - 1; i > 0; i--) { + int d = n[0]; + n[0] = n[i]; + n[i] = d; + task1(n, i, 0); // recursive calling of task1 method } } - void task1(int n[], int k, int i) - { - int p=i; - int l=2*i+1; - int r=2*i+2; - if(ln[p]) - p=l; - if(rn[p]) - p=r; - if(p!=i) - { - int d=n[p]; - n[p]=n[i]; - n[i]=d; - task1(n,k,p); + + void task1(int n[], int k, int i) { + int p = i; + int l = 2 * i + 1; + int r = 2 * i + 2; + if (l < k && n[l] > n[p]) + p = l; + if (r < k && n[r] > n[p]) + p = r; + if (p != i) { + int d = n[p]; + n[p] = n[i]; + n[i] = d; + task1(n, k, p); } } // The method task and task1 is used to sort the linklist using heap sort From f9fc81e6b89aff84d91e6af6a1aa96ef7a556680 Mon Sep 17 00:00:00 2001 From: siddhant2002 Date: Wed, 19 Jan 2022 09:55:19 +0530 Subject: [PATCH 9/9] Fixes(2882) --- .../thealgorithms/sorts/LinkList_Sort.java | 60 +++++++++++------ .../others/LinkList_Sort_test.java | 64 +++++++++++++++++++ 2 files changed, 105 insertions(+), 19 deletions(-) create mode 100644 src/test/java/com/thealgorithms/others/LinkList_Sort_test.java diff --git a/src/main/java/com/thealgorithms/sorts/LinkList_Sort.java b/src/main/java/com/thealgorithms/sorts/LinkList_Sort.java index fc05120c1fb6..2480091621a9 100644 --- a/src/main/java/com/thealgorithms/sorts/LinkList_Sort.java +++ b/src/main/java/com/thealgorithms/sorts/LinkList_Sort.java @@ -5,15 +5,17 @@ /** Program description - To sort the LinkList as per sorting technique */ package com.thealgorithms.sorts; - +import java.util.*; public class LinkList_Sort { - public static void main(String args[]) { - int a[] = { 89, 56, 98, 123, 26, 75, 12, 40, 39, 68, 91 }; - // Array is taken as input - int b[] = { 12, 26, 39, 40, 56, 68, 75, 89, 91, 98, 123 }; - // Excepted Array - int ch = 1; - // Choice is choosed as 1 (So the linked list will be sorted by Merge sort technique) + public static boolean isSorted(int p[] , int option) { + try (Scanner sc = new Scanner(System.in)) { + } + int a[] = p; + // Array is taken as input from test class + int b[] = p; + // array similar to a + int ch = option; + // Choice is choosed as any number from 1 to 3 (So the linked list will be sorted by Merge sort technique/Insertion sort technique/Heap sort technique) switch (ch) { case 1: Task nm = new Task(); @@ -35,11 +37,18 @@ public static void main(String args[]) { a[i++]=ptr.val; // storing the sorted values in the array } + Arrays.sort(b); + // array b is sorted and it will return true when checked with sorted list LinkList_Sort uu=new LinkList_Sort(); - System.out.println(uu.compare(a,b)); + if(uu.compare(a,b)) + { + return true; + } + else + { + return false; + } // The given array and the expected array is checked if both are same then true is displayed else false is displayed - break; - case 2: Node start1 = null, prev1 = null, fresh1, ptr1; for (int i1 = 0; i1 < a.length; i1++) { @@ -61,10 +70,16 @@ public static void main(String args[]) { // storing the sorted values in the array } LinkList_Sort uu1=new LinkList_Sort(); - System.out.println(uu1.compare(a,b)); + // array b is not sorted and it will return false when checked with sorted list + if(uu1.compare(a,b)) + { + return true; + } + else + { + return false; + } // The given array and the expected array is checked if both are same then true is displayed else false is displayed - break; - case 3: Task2 mm = new Task2(); Node start2 = null, prev2 = null, fresh2, ptr2; @@ -85,16 +100,24 @@ public static void main(String args[]) { a[i3++]=ptr2.val; // storing the sorted values in the array } + Arrays.sort(b); + // array b is sorted and it will return true when checked with sorted list LinkList_Sort uu2=new LinkList_Sort(); - System.out.println(uu2.compare(a,b)); + if(uu2.compare(a,b)) + { + return true; + } + else + { + return false; + } // The given array and the expected array is checked if both are same then true is displayed else false is displayed - break; - default: // default is used incase user puts a unauthorized value System.out.println("Wrong choice"); } // Switch case is used to call the classes as per the user requirement + return false; } boolean compare(int a[] , int b[]) { @@ -192,7 +215,6 @@ void task1(int n[], int s, int m, int e) { } // The method task and task1 is used to sort the linklist using merge sort } - class Task1 { public Node sort_by_insertionsort(Node head) { if (head == null || head.next == null) @@ -297,4 +319,4 @@ void task1(int n[], int k, int i) { } } // The method task and task1 is used to sort the linklist using heap sort -} +} \ No newline at end of file diff --git a/src/test/java/com/thealgorithms/others/LinkList_Sort_test.java b/src/test/java/com/thealgorithms/others/LinkList_Sort_test.java new file mode 100644 index 000000000000..758d25328ad7 --- /dev/null +++ b/src/test/java/com/thealgorithms/others/LinkList_Sort_test.java @@ -0,0 +1,64 @@ +package com.thealgorithms.others; +import org.junit.jupiter.api.Test; + +import com.thealgorithms.sorts.LinkList_Sort; + +import static org.junit.jupiter.api.Assertions.*; +public class LinkList_Sort_test { + @Test + void testForOneElement() + { + int a[]={56}; + assertTrue(LinkList_Sort.isSorted(a,2)); + } + + @Test + void testForTwoElements() + { + int a[]={6,4}; + assertTrue(LinkList_Sort.isSorted(a,1)); + } + + @Test + void testForThreeElements() + { + int a[]={875,253,12}; + assertTrue(LinkList_Sort.isSorted(a,3)); + } + + @Test + void testForFourElements() + { + int a[]={86,32,87,13}; + assertFalse(LinkList_Sort.isSorted(a,2)); + } + + @Test + void testForFiveElements() + { + int a[]={6,5,3,0,9}; + assertTrue(LinkList_Sort.isSorted(a,1)); + } + + + @Test + void testForSixElements() + { + int a[]={9,65,432,32,47,327}; + assertTrue(LinkList_Sort.isSorted(a,3)); + } + + @Test + void testForSevenElements() + { + int a[]={6,4,2,1,3,6,7}; + assertTrue(LinkList_Sort.isSorted(a,1)); + } + + @Test + void testForEightElements() + { + int a[]={123,234,145,764,322,367,768,34}; + assertFalse(LinkList_Sort.isSorted(a,2)); + } +}