From f6f66f26041c6017a012f6a87f190080979eead5 Mon Sep 17 00:00:00 2001 From: oleksii Date: Thu, 20 Dec 2018 22:29:23 +0200 Subject: [PATCH 1/6] Done simple linear search --- src/phonebook/Main.java | 28 ++++++++++++++++++++++++++-- 1 file changed, 26 insertions(+), 2 deletions(-) diff --git a/src/phonebook/Main.java b/src/phonebook/Main.java index 100546c..515e61e 100644 --- a/src/phonebook/Main.java +++ b/src/phonebook/Main.java @@ -1,7 +1,31 @@ package phonebook; +import java.io.IOException; +import java.nio.file.Files; +import java.nio.file.Paths; +import java.util.ArrayList; +import java.util.List; + public class Main { - public static void main(String[] args) { - System.out.print("Hello world!"); + + public static void main(String[] args) throws IOException { + List directoryLines = Files.readAllLines(Paths.get("data/directory.txt")); + List names = new ArrayList<>(); + List numbers = new ArrayList<>(); + directoryLines.forEach(line -> { + numbers.add(line.substring(0, line.indexOf(" "))); + names.add(line.substring(line.indexOf(" ") + 1)); + }); + List requests = Files.readAllLines(Paths.get("data/find.txt")); + long start = System.currentTimeMillis(); + for (String request : requests) { + for (int i = 0; i < names.size(); i++) { + if (request.equals(names.get(i))) { + System.out.printf("%s has number %s\n", request, numbers.get(i)); + break; + } + } } + System.out.printf("To find all entries it taken %d ms\n", System.currentTimeMillis() - start); + } } \ No newline at end of file From 791819965f0a96d18f8a9098212268f22580643a Mon Sep 17 00:00:00 2001 From: oleksii Date: Fri, 21 Dec 2018 23:57:10 +0200 Subject: [PATCH 2/6] Done simple linear search --- src/phonebook/Contact.java | 20 ++++++++++++++ src/phonebook/Main.java | 55 +++++++++++++++++++++++++++++--------- 2 files changed, 62 insertions(+), 13 deletions(-) create mode 100644 src/phonebook/Contact.java diff --git a/src/phonebook/Contact.java b/src/phonebook/Contact.java new file mode 100644 index 0000000..c3ccc28 --- /dev/null +++ b/src/phonebook/Contact.java @@ -0,0 +1,20 @@ +package phonebook; + +public class Contact { + + private String number; + private String name; + + public Contact(String number, String name) { + this.number = number; + this.name = name; + } + + public String getName() { + return name; + } + + public String getNumber() { + return number; + } +} diff --git a/src/phonebook/Main.java b/src/phonebook/Main.java index 515e61e..f5320e6 100644 --- a/src/phonebook/Main.java +++ b/src/phonebook/Main.java @@ -4,28 +4,57 @@ import java.nio.file.Files; import java.nio.file.Paths; import java.util.ArrayList; +import java.util.Comparator; import java.util.List; +import java.util.stream.Collectors; +import java.util.stream.Stream; public class Main { + static final String CONTACT_DATA_DIRECTORY = "data/directory.txt"; + static final String FIND_REQUESTS_DIRECTORY = "data/find.txt"; + public static void main(String[] args) throws IOException { - List directoryLines = Files.readAllLines(Paths.get("data/directory.txt")); - List names = new ArrayList<>(); - List numbers = new ArrayList<>(); - directoryLines.forEach(line -> { - numbers.add(line.substring(0, line.indexOf(" "))); - names.add(line.substring(line.indexOf(" ") + 1)); - }); - List requests = Files.readAllLines(Paths.get("data/find.txt")); + List contacts = parseContacts(); + List requests = Files.readAllLines(Paths.get(FIND_REQUESTS_DIRECTORY)); + long start = System.currentTimeMillis(); for (String request : requests) { - for (int i = 0; i < names.size(); i++) { - if (request.equals(names.get(i))) { - System.out.printf("%s has number %s\n", request, numbers.get(i)); - break; - } + Contact contact; + if ((contact = search(contacts, request)) != null) { + System.out.printf("%s has number %s\n", request, contact.getNumber()); } } System.out.printf("To find all entries it taken %d ms\n", System.currentTimeMillis() - start); } + + private static Contact search(List contacts, String name) { + int sqr = (int) Math.sqrt(contacts.size()); + int lo = 0, hi = Math.min(sqr, contacts.size() - 1); + while (lo < hi) { + Contact contact = contacts.get(hi); + if (contact.getName().compareTo(name) == 0) { + return contact; + } else if (contact.getName().compareTo(name) < 0) { + lo = hi + 1; + hi = Math.min(hi + sqr + 1, contacts.size() - 1); + } else { + for (int i = hi; i >= lo; i--) { + if (contacts.get(i).getName().compareTo(name) == 0) { + return contact; + } + } + return null; + } + } + return null; + } + + private static List parseContacts() throws IOException { + Stream directoryLines = Files.lines(Paths.get(CONTACT_DATA_DIRECTORY)); + return directoryLines.map(line -> new Contact(line.substring(0, line.indexOf(" ")), + line.substring(line.indexOf(" ") + 1))) + .sorted(Comparator.comparing(Contact::getName)) + .collect(Collectors.toCollection(ArrayList::new)); + } } \ No newline at end of file From dc208ea44a4f696fe9ffcefec375743b592b2d73 Mon Sep 17 00:00:00 2001 From: oleksii Date: Sat, 22 Dec 2018 00:00:46 +0200 Subject: [PATCH 3/6] Done simple jump search --- src/phonebook/Main.java | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/src/phonebook/Main.java b/src/phonebook/Main.java index f5320e6..922d6bc 100644 --- a/src/phonebook/Main.java +++ b/src/phonebook/Main.java @@ -19,10 +19,11 @@ public static void main(String[] args) throws IOException { List requests = Files.readAllLines(Paths.get(FIND_REQUESTS_DIRECTORY)); long start = System.currentTimeMillis(); + int cnt = 1; for (String request : requests) { Contact contact; if ((contact = search(contacts, request)) != null) { - System.out.printf("%s has number %s\n", request, contact.getNumber()); + System.out.printf("%d. %s has number %s\n", cnt++, request, contact.getNumber()); } } System.out.printf("To find all entries it taken %d ms\n", System.currentTimeMillis() - start); From 4950a9ab9dbeccf4629bada9133c4b44d7308241 Mon Sep 17 00:00:00 2001 From: oleksii Date: Sat, 22 Dec 2018 00:09:30 +0200 Subject: [PATCH 4/6] Done simple jump search and bubble sort --- src/phonebook/Main.java | 15 +++++++++++++-- 1 file changed, 13 insertions(+), 2 deletions(-) diff --git a/src/phonebook/Main.java b/src/phonebook/Main.java index 922d6bc..18b2029 100644 --- a/src/phonebook/Main.java +++ b/src/phonebook/Main.java @@ -4,7 +4,7 @@ import java.nio.file.Files; import java.nio.file.Paths; import java.util.ArrayList; -import java.util.Comparator; +import java.util.Collections; import java.util.List; import java.util.stream.Collectors; import java.util.stream.Stream; @@ -16,6 +16,8 @@ public class Main { public static void main(String[] args) throws IOException { List contacts = parseContacts(); + bubbleSort(contacts); + List requests = Files.readAllLines(Paths.get(FIND_REQUESTS_DIRECTORY)); long start = System.currentTimeMillis(); @@ -51,11 +53,20 @@ private static Contact search(List contacts, String name) { return null; } + private static void bubbleSort(List contacts) { + for (int i = 0; i < contacts.size(); i++) { + for (int j = i + 1; j < contacts.size(); j++) { + if (contacts.get(j).getName().compareTo(contacts.get(j - 1).getName()) < 0) { + Collections.swap(contacts, j, j - 1); + } + } + } + } + private static List parseContacts() throws IOException { Stream directoryLines = Files.lines(Paths.get(CONTACT_DATA_DIRECTORY)); return directoryLines.map(line -> new Contact(line.substring(0, line.indexOf(" ")), line.substring(line.indexOf(" ") + 1))) - .sorted(Comparator.comparing(Contact::getName)) .collect(Collectors.toCollection(ArrayList::new)); } } \ No newline at end of file From 03bcbd03a245c6cc8f56846be3006fb542afbacf Mon Sep 17 00:00:00 2001 From: oleksii Date: Sat, 22 Dec 2018 00:22:11 +0200 Subject: [PATCH 5/6] Done simple jump search and bubble sort --- src/phonebook/Main.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/phonebook/Main.java b/src/phonebook/Main.java index 18b2029..098f714 100644 --- a/src/phonebook/Main.java +++ b/src/phonebook/Main.java @@ -55,7 +55,7 @@ private static Contact search(List contacts, String name) { private static void bubbleSort(List contacts) { for (int i = 0; i < contacts.size(); i++) { - for (int j = i + 1; j < contacts.size(); j++) { + for (int j = contacts.size() - 1; j > i; j--) { if (contacts.get(j).getName().compareTo(contacts.get(j - 1).getName()) < 0) { Collections.swap(contacts, j, j - 1); } From 8822fdbe0e8cd3b94f06b9b5cc85c8115b451ea8 Mon Sep 17 00:00:00 2001 From: oleksii Date: Fri, 28 Dec 2018 00:34:40 +0200 Subject: [PATCH 6/6] Done simple jump search and bubble sort review comments --- src/phonebook/Contact.java | 4 ++-- src/phonebook/Main.java | 35 +++++++++++++++++++++-------------- 2 files changed, 23 insertions(+), 16 deletions(-) diff --git a/src/phonebook/Contact.java b/src/phonebook/Contact.java index c3ccc28..6eec25e 100644 --- a/src/phonebook/Contact.java +++ b/src/phonebook/Contact.java @@ -2,8 +2,8 @@ public class Contact { - private String number; - private String name; + private final String number; + private final String name; public Contact(String number, String name) { this.number = number; diff --git a/src/phonebook/Main.java b/src/phonebook/Main.java index 098f714..fe1494a 100644 --- a/src/phonebook/Main.java +++ b/src/phonebook/Main.java @@ -5,14 +5,17 @@ import java.nio.file.Paths; import java.util.ArrayList; import java.util.Collections; +import java.util.Comparator; import java.util.List; +import java.util.Objects; +import java.util.Optional; import java.util.stream.Collectors; import java.util.stream.Stream; public class Main { - static final String CONTACT_DATA_DIRECTORY = "data/directory.txt"; - static final String FIND_REQUESTS_DIRECTORY = "data/find.txt"; + private static final String CONTACT_DATA_DIRECTORY = "data/directory.txt"; + private static final String FIND_REQUESTS_DIRECTORY = "data/find.txt"; public static void main(String[] args) throws IOException { List contacts = parseContacts(); @@ -23,40 +26,44 @@ public static void main(String[] args) throws IOException { long start = System.currentTimeMillis(); int cnt = 1; for (String request : requests) { - Contact contact; - if ((contact = search(contacts, request)) != null) { - System.out.printf("%d. %s has number %s\n", cnt++, request, contact.getNumber()); + Optional contact = search(contacts, request); + if (contact.isPresent()) { + System.out.printf("%d. %s has number %s\n", cnt++, request, contact.get().getNumber()); } } System.out.printf("To find all entries it taken %d ms\n", System.currentTimeMillis() - start); } - private static Contact search(List contacts, String name) { + private static Optional search(List contacts, String name) { int sqr = (int) Math.sqrt(contacts.size()); int lo = 0, hi = Math.min(sqr, contacts.size() - 1); while (lo < hi) { Contact contact = contacts.get(hi); - if (contact.getName().compareTo(name) == 0) { - return contact; - } else if (contact.getName().compareTo(name) < 0) { + int comparation = Objects.compare(contact.getName(), name, Comparator.naturalOrder()); + if (comparation == 0) { + return Optional.of(contact); + } else if (comparation < 0) { lo = hi + 1; hi = Math.min(hi + sqr + 1, contacts.size() - 1); } else { for (int i = hi; i >= lo; i--) { - if (contacts.get(i).getName().compareTo(name) == 0) { - return contact; + Contact current = contacts.get(i); + if (Objects.equals(current.getName(), name)) { + return Optional.of(contact); } } - return null; + return Optional.empty(); } } - return null; + return Optional.empty(); } private static void bubbleSort(List contacts) { for (int i = 0; i < contacts.size(); i++) { for (int j = contacts.size() - 1; j > i; j--) { - if (contacts.get(j).getName().compareTo(contacts.get(j - 1).getName()) < 0) { + int comparing = Objects.compare(contacts.get(j), contacts.get(j - 1), + Comparator.comparing(Contact::getName)); + if (comparing < 0) { Collections.swap(contacts, j, j - 1); } }