Upload files to "/"
This commit is contained in:
commit
7f8b47803d
238
Sorting.java
Normal file
238
Sorting.java
Normal file
@ -0,0 +1,238 @@
|
|||||||
|
import java.io.BufferedReader;
|
||||||
|
import java.io.FileReader;
|
||||||
|
import java.util.*;
|
||||||
|
|
||||||
|
class MergeSort {
|
||||||
|
|
||||||
|
|
||||||
|
static void merge(int values[], int l, int m, int r) {
|
||||||
|
|
||||||
|
int n1 = m - l + 1;
|
||||||
|
int n2 = r - m;
|
||||||
|
|
||||||
|
int L[] = new int[n1];
|
||||||
|
int R[] = new int[n2];
|
||||||
|
|
||||||
|
for (int i = 0; i < n1; ++i)
|
||||||
|
L[i] = values[l + i];
|
||||||
|
for (int j = 0; j < n2; ++j)
|
||||||
|
R[j] = values[m + 1 + j];
|
||||||
|
|
||||||
|
int i = 0, j = 0;
|
||||||
|
|
||||||
|
int k = l;
|
||||||
|
while (i < n1 && j < n2) {
|
||||||
|
if (L[i] <= R[j]) {
|
||||||
|
values[k] = L[i];
|
||||||
|
i++;
|
||||||
|
} else {
|
||||||
|
values[k] = R[j];
|
||||||
|
j++;
|
||||||
|
}
|
||||||
|
k++;
|
||||||
|
}
|
||||||
|
|
||||||
|
while (i < n1) {
|
||||||
|
values[k] = L[i];
|
||||||
|
i++;
|
||||||
|
k++;
|
||||||
|
}
|
||||||
|
|
||||||
|
while (j < n2) {
|
||||||
|
values[k] = R[j];
|
||||||
|
j++;
|
||||||
|
k++;
|
||||||
|
}
|
||||||
|
|
||||||
|
System.out.println(Arrays.toString(values).replace("[", "").replace("]", "").replace(" ", ""));
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
static void mergeSort(int[] values, int l, int r) {
|
||||||
|
|
||||||
|
int n = values.length;
|
||||||
|
|
||||||
|
if (l < r) {
|
||||||
|
|
||||||
|
// Find the middle point
|
||||||
|
int m = l + (r - l) / 2;
|
||||||
|
|
||||||
|
// Sort first and second halves
|
||||||
|
mergeSort(values, l, m);
|
||||||
|
mergeSort(values, m + 1, r);
|
||||||
|
|
||||||
|
// Merge the sorted halves
|
||||||
|
merge(values, l, m, r);
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
class QuickSort {
|
||||||
|
|
||||||
|
static int partition(int[] values, int low, int high) {
|
||||||
|
|
||||||
|
int pivot = values[high];
|
||||||
|
int i = low - 1;
|
||||||
|
|
||||||
|
for (int j = low; j <= high - 1; j++) {
|
||||||
|
if (values[j] < pivot) {
|
||||||
|
i++;
|
||||||
|
swap(values, i, j);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
swap(values, i + 1, high);
|
||||||
|
return i + 1;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
static void swap(int[] values, int i, int j) {
|
||||||
|
int temp = values[i];
|
||||||
|
values[i] = values[j];
|
||||||
|
values[j] = temp;
|
||||||
|
}
|
||||||
|
|
||||||
|
static void quick_sort(int[] values, int low, int high) {
|
||||||
|
|
||||||
|
System.out.println(Arrays.toString(values).replace("[", "").replace("]", "").replace(" ", ""));
|
||||||
|
|
||||||
|
if (low < high) {
|
||||||
|
|
||||||
|
int pi = partition(values, low, high);
|
||||||
|
|
||||||
|
quick_sort(values, low, pi - 1);
|
||||||
|
quick_sort(values, pi + 1, high);
|
||||||
|
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
public class Sorting {
|
||||||
|
|
||||||
|
static void insertion_sort(int[] values) {
|
||||||
|
|
||||||
|
System.out.println(Arrays.toString(values).replace("[", "").replace("]", "").replace(" ", ""));
|
||||||
|
|
||||||
|
int n = values.length;
|
||||||
|
|
||||||
|
for (int i = 1; i < n; ++i) {
|
||||||
|
int key = values[i];
|
||||||
|
int j = i - 1;
|
||||||
|
|
||||||
|
while (j >= 0 && values[j] > key) {
|
||||||
|
values[j + 1] = values[j];
|
||||||
|
j = j - 1;
|
||||||
|
}
|
||||||
|
|
||||||
|
values[j + 1] = key;
|
||||||
|
|
||||||
|
System.out.println(Arrays.toString(values).replace("[", "").replace("]", "").replace(" ", ""));
|
||||||
|
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
System.out.println(Arrays.toString(values).replace("[", "").replace("]", "").replace(" ", "")); // Print out to CSV file ?
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
static void selection_sort(int[] values) {
|
||||||
|
|
||||||
|
|
||||||
|
int n = values.length;
|
||||||
|
|
||||||
|
System.out.println(Arrays.toString(values).replace("[", "").replace("]", "").replace(" ", ""));
|
||||||
|
|
||||||
|
|
||||||
|
for (int i = 0; i < n - 1; i++) {
|
||||||
|
|
||||||
|
int min = i;
|
||||||
|
|
||||||
|
for (int j = i + 1; j < n; j++) {
|
||||||
|
|
||||||
|
if (values[j] < values[min]) {
|
||||||
|
min = j;
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
if (min != i) {
|
||||||
|
int temp = values[i];
|
||||||
|
values[i] = values[min];
|
||||||
|
values[min] = temp;
|
||||||
|
|
||||||
|
System.out.println(Arrays.toString(values).replace("[", "").replace("]", "").replace(" ", "")); // Print out to CSV file ?
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
System.out.println(Arrays.toString(values).replace("[", "").replace("]", "").replace(" ", ""));
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
public static void main(String[] args)
|
||||||
|
{
|
||||||
|
|
||||||
|
String csv_file = args[0];
|
||||||
|
String option = args[1];
|
||||||
|
|
||||||
|
try (BufferedReader reader = new BufferedReader(new FileReader(csv_file))) {
|
||||||
|
|
||||||
|
|
||||||
|
String line = reader.readLine();
|
||||||
|
String[] values = line.split(",");
|
||||||
|
|
||||||
|
int[] intArray = new int[values.length];
|
||||||
|
|
||||||
|
for (int i = 0; i < values.length; i++) {
|
||||||
|
intArray[i] = Integer.parseInt(values[i].trim());;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
switch (option) {
|
||||||
|
case "1" -> insertion_sort(intArray);
|
||||||
|
case "2" -> selection_sort(intArray);
|
||||||
|
case "3" -> {
|
||||||
|
QuickSort.quick_sort(intArray, 0, intArray.length - 1);
|
||||||
|
System.out.println(Arrays.toString(intArray).replace("[", "").replace("]", "").replace(" ", "")); // Print out to CSV file ?
|
||||||
|
|
||||||
|
}
|
||||||
|
case "4" -> {
|
||||||
|
System.out.println(Arrays.toString(intArray).replace("[", "").replace("]", "").replace(" ", ""));
|
||||||
|
MergeSort.mergeSort(intArray, 0, intArray.length - 1);
|
||||||
|
System.out.println(Arrays.toString(intArray).replace("[", "").replace("]", "").replace(" ", ""));
|
||||||
|
|
||||||
|
}
|
||||||
|
case null, default -> System.out.println("Invalid argument for option: must be between 1 and 4");
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
catch (Exception e) {
|
||||||
|
e.printStackTrace();
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
}
|
||||||
|
}
|
Loading…
Reference in New Issue
Block a user