-
Notifications
You must be signed in to change notification settings - Fork 1
Third quicksort #3
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
oleksiipet
wants to merge
7
commits into
hyperskill:master
Choose a base branch
from
oleksiipet:third-quicksort
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+102
−2
Open
Changes from all commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
f6f66f2
Done simple linear search
opetenko 7918199
Done simple linear search
opetenko dc208ea
Done simple jump search
opetenko 4950a9a
Done simple jump search and bubble sort
opetenko 03bcbd0
Done simple jump search and bubble sort
opetenko e731386
Done simple binary search and quick sort
opetenko 299a489
Done simple binary search and quick sort
opetenko File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -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; | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,7 +1,87 @@ | ||
| package phonebook; | ||
|
|
||
| import java.io.IOException; | ||
| import java.nio.file.Files; | ||
| 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.stream.Collectors; | ||
| import java.util.stream.Stream; | ||
|
|
||
| public class Main { | ||
| public static void main(String[] args) { | ||
| System.out.print("Hello world!"); | ||
|
|
||
| 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<Contact> contacts = parseContacts(); | ||
| quickSort(contacts); | ||
|
|
||
| List<String> requests = Files.readAllLines(Paths.get(FIND_REQUESTS_DIRECTORY)); | ||
|
|
||
| long start = System.currentTimeMillis(); | ||
| int cnt = 1; | ||
| for (String request : requests) { | ||
| Contact contact = search(contacts, request); | ||
| if ((Objects.nonNull(contact))) { | ||
| 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); | ||
| } | ||
|
|
||
| private static Contact search(List<Contact> contacts, String name) { | ||
| int lo = 0, hi = contacts.size() - 1; | ||
| while (lo <= hi) { | ||
| int mid = lo + (hi - lo) / 2; | ||
| Contact contact = contacts.get(mid); | ||
| int comparing = Objects.compare(contact.getName(), name, Comparator.naturalOrder()); | ||
| if (comparing == 0) { | ||
| return contact; | ||
| } else if (comparing < 0) { | ||
| lo = mid + 1; | ||
| } else { | ||
| hi = mid - 1; | ||
| } | ||
| } | ||
| return null; | ||
| } | ||
|
|
||
| private static void quickSort(List<Contact> contacts) { | ||
| quickSort(contacts, 0, contacts.size() - 1); | ||
| } | ||
|
|
||
| private static void quickSort(List<Contact> contacts, int lo, int hi) { | ||
| if (lo >= hi) { | ||
| return; | ||
| } | ||
| int p = pivot(contacts, lo, hi); | ||
| quickSort(contacts, lo, p - 1); | ||
| quickSort(contacts, p + 1, hi); | ||
| } | ||
|
|
||
| private static int pivot(List<Contact> contacts, int lo, int hi) { | ||
| Contact pivot = contacts.get(lo); | ||
| int j = lo + 1; | ||
| for (int i = lo + 1; i <= hi; i++) { | ||
| int comparing = Objects | ||
| .compare(pivot, contacts.get(i), Comparator.comparing(Contact::getName)); | ||
| if (comparing >= 0) { | ||
| Collections.swap(contacts, i, j); | ||
| j++; | ||
| } | ||
| } | ||
| Collections.swap(contacts, lo, j - 1); | ||
| return j - 1; | ||
| } | ||
|
|
||
| private static List<Contact> parseContacts() throws IOException { | ||
| Stream<String> directoryLines = Files.lines(Paths.get(CONTACT_DATA_DIRECTORY)); | ||
| return directoryLines.map(line -> new Contact(line.substring(0, line.indexOf(" ")), | ||
| line.substring(line.indexOf(" ") + 1))) | ||
| .collect(Collectors.toCollection(ArrayList::new)); | ||
| } | ||
| } | ||
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Now all the code looks better! You may also generalize your methods for sorting with and then move the method in a separated class with sorting utils. But it is not required for this project. Just to have an additional practice.