-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
c471d50
commit 6d89d48
Showing
1 changed file
with
21 additions
and
55 deletions.
There are no files selected for viewing
This file contains 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,68 +1,34 @@ | ||
package phonebook; | ||
|
||
import java.util.ArrayList; | ||
import java.io.File; | ||
import java.util.Hashtable; | ||
import java.util.List; | ||
|
||
public class Sort { | ||
public class Start { | ||
private List<Person> directoryList; | ||
private List<String> findList; | ||
private Hashtable<String,String> directoryTable; | ||
|
||
Time time = new Time(); | ||
Search search = new Search(); | ||
Sort sort = new Sort(); | ||
|
||
protected List<Person> bubbleSort(List<Person> directoryList, List<String> findList) { | ||
time.startSorting(); | ||
for (int i = 0; i < directoryList.size() - 1; i++) { | ||
for (int j = 0; j < directoryList.size() - i - 1; j++) { | ||
if (time.currentSortingTime() > time.getLinearSearchTime() * 10) { | ||
time.endSorting(); | ||
search.linearSearch(directoryList, findList, false); | ||
public void start() { | ||
directoryList = Files.addDir(new File("C:\\Users\\helen\\Phone Book (Java)\\directory.txt")); | ||
findList = Files.addFind(new File("C:\\Users\\helen\\Phone Book (Java)\\find.txt")); | ||
|
||
time.printLinearSearchAndBubbleSortTime(); | ||
time.printSortingTime(); | ||
System.out.print(" - STOPPED, moved to linear search"); | ||
time.printSearchingTime(); | ||
return directoryList; | ||
} | ||
System.out.println("Start searching (linear search)..."); | ||
search.linearSearch(directoryList, findList, true); | ||
|
||
if (directoryList.get(j).getName().compareTo(directoryList.get(j + 1).getName()) > 0) { | ||
Person tmp = directoryList.get(j); | ||
directoryList.set(j, directoryList.get(j + 1)); | ||
directoryList.set(j + 1, tmp); | ||
} | ||
} | ||
} | ||
time.endSorting(); | ||
return directoryList; | ||
} | ||
|
||
protected List<Person> quickSort(List<Person> directoryList) { | ||
time.startSorting(); | ||
if(directoryList.isEmpty()){ | ||
return directoryList; | ||
} | ||
int pivot = directoryList.size() / 2; | ||
|
||
List<Person> sorted; | ||
List<Person> lesser = new ArrayList<>(); | ||
List<Person> greater = new ArrayList<>(); | ||
|
||
Person piv = directoryList.get(pivot); | ||
String pivotVal = piv.getName(); | ||
System.out.println("Start searching (bubble sort + jump search)..."); | ||
List<Person> sortedBubble = sort.bubbleSort(directoryList, findList); | ||
search.jumpSearch(sortedBubble, findList); | ||
|
||
for (int i = 1; i < directoryList.size(); i++) { | ||
Person p = directoryList.get(i); | ||
if (p.getName().compareTo(pivotVal) < 0) { | ||
lesser.add(p); | ||
} else { | ||
greater.add(p); | ||
} | ||
} | ||
System.out.println("Start searching (quick sort + binary search)..."); | ||
List<Person> sortedQuick = sort.quickSort(directoryList); | ||
search.binarySearch(sortedQuick, findList); | ||
|
||
lesser = quickSort(lesser); | ||
greater = quickSort(greater); | ||
lesser.add(piv); | ||
lesser.addAll(greater); | ||
sorted = lesser; | ||
time.endSorting(); | ||
return sorted; | ||
System.out.println("Start searching (hash table)..."); | ||
directoryTable = Files.toHash(directoryList); | ||
search.hashSearch(directoryTable,findList); | ||
} | ||
} |