feat: Added more sorts and minor changes
This commit is contained in:
9
README.md
Normal file
9
README.md
Normal file
@ -0,0 +1,9 @@
|
|||||||
|
# MeitSort
|
||||||
|
|
||||||
|
Simple sorting algorithm implementation, where all number comparing is done via email.
|
||||||
|
|
||||||
|
This program sents email and the person has to respond which number is bigger.
|
||||||
|
|
||||||
|
---
|
||||||
|
|
||||||
|
I tried to make this code as readable as possible
|
||||||
@ -53,4 +53,4 @@ public class Main {
|
|||||||
|
|
||||||
System.exit(0);
|
System.exit(0);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
27
src/main/java/cz/jzitnik/algs/InsertionSort.java
Normal file
27
src/main/java/cz/jzitnik/algs/InsertionSort.java
Normal file
@ -0,0 +1,27 @@
|
|||||||
|
package cz.jzitnik.algs;
|
||||||
|
|
||||||
|
import cz.jzitnik.GlobalContext;
|
||||||
|
import cz.jzitnik.utils.SortingAlgorithm;
|
||||||
|
import jakarta.mail.MessagingException;
|
||||||
|
|
||||||
|
import java.io.IOException;
|
||||||
|
|
||||||
|
public class InsertionSort extends SortingAlgorithm {
|
||||||
|
public InsertionSort(GlobalContext globalContext, String from, String recipient) {
|
||||||
|
super(globalContext, from, recipient);
|
||||||
|
}
|
||||||
|
|
||||||
|
public int[] sort(int[] arr) throws MessagingException, InterruptedException, IOException {
|
||||||
|
int n = arr.length;
|
||||||
|
for (int i = 1; i < n; i++) {
|
||||||
|
int key = arr[i];
|
||||||
|
int j = i - 1;
|
||||||
|
while (j >= 0 && compare(arr[j], key) == ResponseAnswer.FIRST) {
|
||||||
|
arr[j + 1] = arr[j];
|
||||||
|
j--;
|
||||||
|
}
|
||||||
|
arr[j + 1] = key;
|
||||||
|
}
|
||||||
|
return arr;
|
||||||
|
}
|
||||||
|
}
|
||||||
51
src/main/java/cz/jzitnik/algs/MergeSort.java
Normal file
51
src/main/java/cz/jzitnik/algs/MergeSort.java
Normal file
@ -0,0 +1,51 @@
|
|||||||
|
package cz.jzitnik.algs;
|
||||||
|
|
||||||
|
import cz.jzitnik.GlobalContext;
|
||||||
|
import cz.jzitnik.utils.SortingAlgorithm;
|
||||||
|
import jakarta.mail.MessagingException;
|
||||||
|
|
||||||
|
import java.io.IOException;
|
||||||
|
|
||||||
|
public class MergeSort extends SortingAlgorithm {
|
||||||
|
public MergeSort(GlobalContext globalContext, String from, String recipient) {
|
||||||
|
super(globalContext, from, recipient);
|
||||||
|
}
|
||||||
|
|
||||||
|
public int[] sort(int[] arr) throws MessagingException, InterruptedException, IOException {
|
||||||
|
mergeSort(arr, 0, arr.length - 1);
|
||||||
|
return arr;
|
||||||
|
}
|
||||||
|
|
||||||
|
private void mergeSort(int[] arr, int left, int right) throws MessagingException, InterruptedException, IOException {
|
||||||
|
if (left < right) {
|
||||||
|
int mid = (left + right) / 2;
|
||||||
|
mergeSort(arr, left, mid);
|
||||||
|
mergeSort(arr, mid + 1, right);
|
||||||
|
merge(arr, left, mid, right);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
private void merge(int[] arr, int left, int mid, int right) throws MessagingException, InterruptedException, IOException {
|
||||||
|
int n1 = mid - left + 1;
|
||||||
|
int n2 = right - mid;
|
||||||
|
|
||||||
|
int[] L = new int[n1];
|
||||||
|
int[] R = new int[n2];
|
||||||
|
|
||||||
|
System.arraycopy(arr, left, L, 0, n1);
|
||||||
|
System.arraycopy(arr, mid + 1, R, 0, n2);
|
||||||
|
|
||||||
|
int i = 0, j = 0, k = left;
|
||||||
|
|
||||||
|
while (i < n1 && j < n2) {
|
||||||
|
if (compare(L[i], R[j]) == ResponseAnswer.FIRST) {
|
||||||
|
arr[k++] = R[j++];
|
||||||
|
} else {
|
||||||
|
arr[k++] = L[i++];
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
while (i < n1) arr[k++] = L[i++];
|
||||||
|
while (j < n2) arr[k++] = R[j++];
|
||||||
|
}
|
||||||
|
}
|
||||||
43
src/main/java/cz/jzitnik/algs/QuickSort.java
Normal file
43
src/main/java/cz/jzitnik/algs/QuickSort.java
Normal file
@ -0,0 +1,43 @@
|
|||||||
|
package cz.jzitnik.algs;
|
||||||
|
|
||||||
|
import cz.jzitnik.GlobalContext;
|
||||||
|
import cz.jzitnik.utils.SortingAlgorithm;
|
||||||
|
import jakarta.mail.MessagingException;
|
||||||
|
|
||||||
|
import java.io.IOException;
|
||||||
|
|
||||||
|
public class QuickSort extends SortingAlgorithm {
|
||||||
|
public QuickSort(GlobalContext globalContext, String from, String recipient) {
|
||||||
|
super(globalContext, from, recipient);
|
||||||
|
}
|
||||||
|
|
||||||
|
public int[] sort(int[] arr) throws MessagingException, InterruptedException, IOException {
|
||||||
|
quickSort(arr, 0, arr.length - 1);
|
||||||
|
return arr;
|
||||||
|
}
|
||||||
|
|
||||||
|
private void quickSort(int[] arr, int low, int high) throws MessagingException, InterruptedException, IOException {
|
||||||
|
if (low < high) {
|
||||||
|
int pi = partition(arr, low, high);
|
||||||
|
quickSort(arr, low, pi - 1);
|
||||||
|
quickSort(arr, pi + 1, high);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
private int partition(int[] arr, int low, int high) throws MessagingException, InterruptedException, IOException {
|
||||||
|
int pivot = arr[high];
|
||||||
|
int i = low - 1;
|
||||||
|
for (int j = low; j < high; j++) {
|
||||||
|
if (compare(arr[j], pivot) != ResponseAnswer.FIRST) {
|
||||||
|
i++;
|
||||||
|
int temp = arr[i];
|
||||||
|
arr[i] = arr[j];
|
||||||
|
arr[j] = temp;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
int temp = arr[i + 1];
|
||||||
|
arr[i + 1] = arr[high];
|
||||||
|
arr[high] = temp;
|
||||||
|
return i + 1;
|
||||||
|
}
|
||||||
|
}
|
||||||
29
src/main/java/cz/jzitnik/algs/SelectionSort.java
Normal file
29
src/main/java/cz/jzitnik/algs/SelectionSort.java
Normal file
@ -0,0 +1,29 @@
|
|||||||
|
package cz.jzitnik.algs;
|
||||||
|
|
||||||
|
import cz.jzitnik.GlobalContext;
|
||||||
|
import cz.jzitnik.utils.SortingAlgorithm;
|
||||||
|
import jakarta.mail.MessagingException;
|
||||||
|
|
||||||
|
import java.io.IOException;
|
||||||
|
|
||||||
|
public class SelectionSort extends SortingAlgorithm {
|
||||||
|
public SelectionSort(GlobalContext globalContext, String from, String recipient) {
|
||||||
|
super(globalContext, from, recipient);
|
||||||
|
}
|
||||||
|
|
||||||
|
public int[] sort(int[] arr) throws MessagingException, InterruptedException, IOException {
|
||||||
|
int n = arr.length;
|
||||||
|
for (int i = 0; i < n - 1; i++) {
|
||||||
|
int minIndex = i;
|
||||||
|
for (int j = i + 1; j < n; j++) {
|
||||||
|
if (compare(arr[minIndex], arr[j]) == ResponseAnswer.FIRST) {
|
||||||
|
minIndex = j;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
int temp = arr[i];
|
||||||
|
arr[i] = arr[minIndex];
|
||||||
|
arr[minIndex] = temp;
|
||||||
|
}
|
||||||
|
return arr;
|
||||||
|
}
|
||||||
|
}
|
||||||
@ -8,7 +8,6 @@ import jakarta.mail.MessagingException;
|
|||||||
import jakarta.mail.Multipart;
|
import jakarta.mail.Multipart;
|
||||||
import jakarta.mail.internet.InternetAddress;
|
import jakarta.mail.internet.InternetAddress;
|
||||||
|
|
||||||
import java.awt.geom.RectangularShape;
|
|
||||||
import java.io.IOException;
|
import java.io.IOException;
|
||||||
|
|
||||||
public abstract class SortingAlgorithm {
|
public abstract class SortingAlgorithm {
|
||||||
@ -35,8 +34,8 @@ public abstract class SortingAlgorithm {
|
|||||||
mailSender.sendMessage(
|
mailSender.sendMessage(
|
||||||
new InternetAddress(from),
|
new InternetAddress(from),
|
||||||
InternetAddress.parse(recipient),
|
InternetAddress.parse(recipient),
|
||||||
"Porovnání dvou čísel",
|
"Comparison of Two Numbers",
|
||||||
"Které číslo je větší? " + x + " nebo " + y + "? Pokud první odpovězte na email s textem PRVNI, pokud druhé tak DRUHE a pokud se rovnají odpovězte ROVNO."
|
"Which number is bigger? " + x + " or " + y + "? If the first, reply to this email with the text FIRST; if the second, reply with SECOND; and if they are equal, reply with EQUAL."
|
||||||
);
|
);
|
||||||
|
|
||||||
globalContext.setWaitingForMessage(true);
|
globalContext.setWaitingForMessage(true);
|
||||||
@ -48,15 +47,15 @@ public abstract class SortingAlgorithm {
|
|||||||
|
|
||||||
String answ = getTextFromMessage(answer).trim();
|
String answ = getTextFromMessage(answer).trim();
|
||||||
|
|
||||||
if (answ.startsWith("PRVNI")) {
|
if (answ.startsWith("FIRST")) {
|
||||||
return ResponseAnswer.FIRST;
|
return ResponseAnswer.FIRST;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (answ.startsWith("DRUHE")) {
|
if (answ.startsWith("SECOND")) {
|
||||||
return ResponseAnswer.SECOND;
|
return ResponseAnswer.SECOND;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (answ.startsWith("ROVNO")) {
|
if (answ.startsWith("EQUAL")) {
|
||||||
return ResponseAnswer.SAME;
|
return ResponseAnswer.SAME;
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -65,14 +64,12 @@ public abstract class SortingAlgorithm {
|
|||||||
|
|
||||||
public abstract int[] sort(int[] arr) throws MessagingException, InterruptedException, IOException;
|
public abstract int[] sort(int[] arr) throws MessagingException, InterruptedException, IOException;
|
||||||
|
|
||||||
|
|
||||||
private String getTextFromMessage(Message message) throws MessagingException, IOException {
|
private String getTextFromMessage(Message message) throws MessagingException, IOException {
|
||||||
Object content = message.getContent();
|
Object content = message.getContent();
|
||||||
if (content instanceof String) {
|
if (content instanceof String) {
|
||||||
// Plain text email
|
// Plain text email
|
||||||
return (String) content;
|
return (String) content;
|
||||||
} else if (content instanceof Multipart) {
|
} else if (content instanceof Multipart multipart) {
|
||||||
Multipart multipart = (Multipart) content;
|
|
||||||
return getTextFromMultipart(multipart);
|
return getTextFromMultipart(multipart);
|
||||||
}
|
}
|
||||||
return "";
|
return "";
|
||||||
|
|||||||
@ -39,8 +39,6 @@ public class MailReader implements Runnable {
|
|||||||
inbox = store.getFolder("INBOX");
|
inbox = store.getFolder("INBOX");
|
||||||
inbox.open(Folder.READ_ONLY);
|
inbox.open(Folder.READ_ONLY);
|
||||||
|
|
||||||
System.out.println("[EmailChecker] Connected and monitoring inbox...");
|
|
||||||
|
|
||||||
while (true) {
|
while (true) {
|
||||||
Message[] messages = inbox.getMessages();
|
Message[] messages = inbox.getMessages();
|
||||||
int messageCount = messages.length;
|
int messageCount = messages.length;
|
||||||
|
|||||||
Reference in New Issue
Block a user