commit 9adb4a81e1c50e34be75278d9cd2d7c7f649712f Author: Dawid Urbaniak Date: Fri Jun 21 13:30:48 2024 +0200 Inital commit diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..f68d109 --- /dev/null +++ b/.gitignore @@ -0,0 +1,29 @@ +### IntelliJ IDEA ### +out/ +!**/src/main/**/out/ +!**/src/test/**/out/ + +### Eclipse ### +.apt_generated +.classpath +.factorypath +.project +.settings +.springBeans +.sts4-cache +bin/ +!**/src/main/**/bin/ +!**/src/test/**/bin/ + +### NetBeans ### +/nbproject/private/ +/nbbuild/ +/dist/ +/nbdist/ +/.nb-gradle/ + +### VS Code ### +.vscode/ + +### Mac OS ### +.DS_Store \ No newline at end of file diff --git a/.idea/.gitignore b/.idea/.gitignore new file mode 100644 index 0000000..13566b8 --- /dev/null +++ b/.idea/.gitignore @@ -0,0 +1,8 @@ +# Default ignored files +/shelf/ +/workspace.xml +# Editor-based HTTP Client requests +/httpRequests/ +# Datasource local storage ignored files +/dataSources/ +/dataSources.local.xml diff --git a/.idea/misc.xml b/.idea/misc.xml new file mode 100644 index 0000000..69ace3f --- /dev/null +++ b/.idea/misc.xml @@ -0,0 +1,6 @@ + + + + + + \ No newline at end of file diff --git a/.idea/modules.xml b/.idea/modules.xml new file mode 100644 index 0000000..b31201c --- /dev/null +++ b/.idea/modules.xml @@ -0,0 +1,8 @@ + + + + + + + + \ No newline at end of file diff --git a/.idea/uiDesigner.xml b/.idea/uiDesigner.xml new file mode 100644 index 0000000..2b63946 --- /dev/null +++ b/.idea/uiDesigner.xml @@ -0,0 +1,124 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + \ No newline at end of file diff --git a/Paradygmaty_Programowania_1.iml b/Paradygmaty_Programowania_1.iml new file mode 100644 index 0000000..c90834f --- /dev/null +++ b/Paradygmaty_Programowania_1.iml @@ -0,0 +1,11 @@ + + + + + + + + + + + \ No newline at end of file diff --git a/src/Bubble_Sort.java b/src/Bubble_Sort.java new file mode 100644 index 0000000..1c684b8 --- /dev/null +++ b/src/Bubble_Sort.java @@ -0,0 +1,16 @@ +import java.util.Comparator; +public class Bubble_Sort { + public static void bubble_sort(T[] array, Comparator comparator) { + int array_length = array.length; + for (int i = 0; i < array_length; i++) { + for (int j = 0; j < array_length - 1 - i; j++) { + if (comparator.compare(array[j], array[j + 1]) > 0) { + T temporary = array[j]; + array[j] = array[j + 1]; + array[j + 1] = temporary; + } + } + } + } +} + diff --git a/src/Bucket_Sort.java b/src/Bucket_Sort.java new file mode 100644 index 0000000..03ba5fb --- /dev/null +++ b/src/Bucket_Sort.java @@ -0,0 +1,30 @@ +import java.util.ArrayList; +import java.util.Comparator; +import java.util.List; + +public class Bucket_Sort { + public static void bucket_sort(T[] array, Comparator comparator) { + int array_length = array.length; + + List[] bucket = new ArrayList[array_length]; + for (int i = 0; i < array_length; i++) { + bucket[i] = new ArrayList<>(); + } + + for (T elem : array) { + int bucket_index = (int) (array_length * (Float) elem); + bucket[bucket_index].add(elem); + } + + for (List container : bucket) { + container.sort(comparator); + } + + int index_to_add = 0; + for (List container : bucket) { + for (T item : container) { + array[index_to_add++] = item; + } + } + } +} diff --git a/src/Counting_Sort.java b/src/Counting_Sort.java new file mode 100644 index 0000000..73aab52 --- /dev/null +++ b/src/Counting_Sort.java @@ -0,0 +1,39 @@ +import java.util.Arrays; +import java.util.Comparator; +public class Counting_Sort { + public static void counting_sort(T[] array, Comparator comparator){ + int array_length = array.length; + T min = array[0]; + T max = array[0]; + + for (int i = 1; i < array.length; i ++) { + if (comparator.compare(array[i], min) < 0) { + min = array[i]; + } + if (comparator.compare(array[i], max) > 0) { + max=array[i]; + } + } + + int []counting_array = new int[array_length]; + Arrays.fill(counting_array, 0); + + for (T value : array) { + int every_index = Arrays.binarySearch(array, min, comparator); + counting_array[every_index]++; + + } + + for (int i = 1; i < array_length; i++) { + counting_array[i] += counting_array[i - 1]; + } + + T[] array_copy = array; + for (int i = array_length - 1; i >= 0; i--) { + int index = Arrays.binarySearch(array, min, comparator); + array_copy[--counting_array[index]] = array[i]; + } + + System.arraycopy(array_copy, 0, array, 0, array_length); + } +} diff --git a/src/Heap_Sort.java b/src/Heap_Sort.java new file mode 100644 index 0000000..bf9c741 --- /dev/null +++ b/src/Heap_Sort.java @@ -0,0 +1,40 @@ +import java.util.Comparator; + +public class Heap_Sort { + public static void heap_sort(T[] array, Comparator comparator) { + int array_length = array.length; + for (int i = array_length / 2 - 1; i >= 0; i--) { + heaping(array, array_length, i, comparator); + } + + for (int i = array_length - 1; i > 0; i--) { + T temporary = array[0]; + array[0] = array[i]; + array[i] = temporary; + heaping(array, i, 0, comparator); + } + } + + private static void heaping(T[] array, int array_length, int i, Comparator comparator) { + int max = i; + + int left = 2 * i + 1; + int right = 2 * i + 2; + + if (left < array_length && comparator.compare(array[left], array[max]) > 0) { + max = left; + } + + if(right < array_length && comparator.compare(array[right], array[max]) > 0) { + max = right; + } + + if (max != i) { + T temporary = array[i]; + array[i] = array[max]; + array[max] = temporary; + + heaping(array, array_length, max, comparator); + } + } +} diff --git a/src/Insert_Sort.java b/src/Insert_Sort.java new file mode 100644 index 0000000..6d86d2a --- /dev/null +++ b/src/Insert_Sort.java @@ -0,0 +1,15 @@ +import java.util.Comparator; +public class Insert_Sort { + public static void insertion_sort(T[] array, Comparator comparator) { + for (int i = 1; i < array.length; i++) { + T actual_element = array[i]; + int previous_index = i - 1; + + while (previous_index >= 0 && comparator.compare(array[previous_index], actual_element) > 0) { + array[previous_index + 1] = array[i-1]; + previous_index++; + } + array[previous_index + 1] = actual_element; + } + } +} diff --git a/src/Main.java b/src/Main.java new file mode 100644 index 0000000..59af6af --- /dev/null +++ b/src/Main.java @@ -0,0 +1,60 @@ +import java.util.Arrays; + +public class Main { + public static void main(String[] args) { + Integer[] some_array = {21,4,33,56,2,1,7,15}; + System.out.println("Original Array: " + Arrays.toString(some_array)); + System.out.println("--------------------------------------------------------------"); + + Integer[] array_bubble = some_array; + Bubble_Sort.bubble_sort(array_bubble, Integer::compareTo); + System.out.println("After Bubble Sort: " + Arrays.toString(array_bubble)); + System.out.println("--------------------------------------------------------------"); + + Integer[] array_selection_sort = some_array; + Selection_Sort.selection_sort(array_selection_sort, Integer::compareTo); + System.out.println("After Selection Sort: " + Arrays.toString(array_selection_sort)); + System.out.println("--------------------------------------------------------------"); + + Integer[] array_counting_sort = some_array; + Counting_Sort.counting_sort(array_counting_sort, Integer::compareTo); + System.out.println("After Counting Sort: " + Arrays.toString(array_counting_sort)); + System.out.println("--------------------------------------------------------------"); + + Integer[] array_quick_sort = some_array; + int array_length = some_array.length; + Quick_Sort.quick_sort(array_quick_sort, 0, array_length -1, Integer::compareTo); + System.out.println("After Quick Sort: " + Arrays.toString(array_quick_sort)); + System.out.println("--------------------------------------------------------------"); + + Integer[] array_merge_sort = some_array; + Merge_Sort.merge_sort(array_merge_sort, Integer::compareTo); + System.out.println("After Merge Sort: " + Arrays.toString(array_merge_sort)); + System.out.println("--------------------------------------------------------------"); + + Integer[] array_insert_sort = some_array; + Insert_Sort.insertion_sort(array_insert_sort, Integer::compareTo); + System.out.println("After Insert Sort: " + Arrays.toString(array_insert_sort)); + System.out.println("--------------------------------------------------------------"); + + Float[] array_bucket_sort = {0.123f, 0.003f, 0.435f, 0.234f, 0.124f, 0.678f, 0.567f }; + Bucket_Sort.bucket_sort(array_bucket_sort, Float::compareTo); + System.out.println("After Bucket Sort: " + Arrays.toString(array_bucket_sort)); + System.out.println("--------------------------------------------------------------"); + + Integer[] array_radix_sort = some_array; + Radix_Sort.radix_sort(array_radix_sort, Integer::compareTo); + System.out.println("After Radix Sort: " + Arrays.toString(array_radix_sort)); + System.out.println("--------------------------------------------------------------"); + + Integer[] array_heap_sort = some_array; + Heap_Sort.heap_sort(array_heap_sort, Integer::compareTo); + System.out.println("After Heap Sort: " + Arrays.toString(array_heap_sort)); + System.out.println("--------------------------------------------------------------"); + + Integer[] array_shell_sort = some_array; + Shell_Sort.shell_Sort(array_shell_sort, Integer::compareTo); + System.out.println("After Shell Sort: " + Arrays.toString(array_shell_sort)); + System.out.println("--------------------------------------------------------------"); + } +} diff --git a/src/Merge_Sort.java b/src/Merge_Sort.java new file mode 100644 index 0000000..fc78eff --- /dev/null +++ b/src/Merge_Sort.java @@ -0,0 +1,39 @@ +import java.util.Arrays; +import java.util.Comparator; +public class Merge_Sort { + public static void merge_sort(T[] array, Comparator comparator) { + T[] temporary_array = Arrays.copyOf(array, array.length); + int start_index = 0; + int end_index = array.length - 1; + sort(array, temporary_array, start_index, end_index, comparator); + } + + private static void sort(T[] array, T[] temporary_array, int start_index, int end_index, Comparator comparator ) { + if (start_index >= end_index) return; + int middle_index = (start_index + end_index) / 2; + + sort(array, temporary_array, start_index, middle_index, comparator); + sort(array, temporary_array, middle_index + 1, end_index, comparator); + merge(array, temporary_array, start_index, middle_index, end_index, comparator); + } + + private static void merge(T[] array, T[] temporary_array, int start_index, int middle_index, int end_index, Comparator comparator ) { + int left_index_end = middle_index; + int right_index_start = middle_index + 1; + int left_index = start_index; + int right_indext = right_index_start; + int actual_index = start_index; + + while (left_index <= left_index_end && right_indext <= end_index) { + if (comparator.compare(array[left_index], array[right_indext]) <= 0) { + temporary_array[actual_index++] = array[left_index++]; + } else { + temporary_array[actual_index++] = array[right_indext++]; + } + } + System.arraycopy(array, left_index, temporary_array, actual_index, left_index_end - left_index + 1); + System.arraycopy(array, right_indext, temporary_array, actual_index, end_index - right_indext + 1); + System.arraycopy(temporary_array, start_index, array, start_index, end_index - start_index + 1); + } + +} diff --git a/src/Quick_Sort.java b/src/Quick_Sort.java new file mode 100644 index 0000000..398739d --- /dev/null +++ b/src/Quick_Sort.java @@ -0,0 +1,27 @@ +import java.util.Comparator; +public class Quick_Sort { + public static void quick_sort(T[] array, int start_index, int end_index, Comparator comparator) { + + if (start_index < end_index) { + T pivot = array[end_index]; + int pivot_index = start_index - 1; + + for (int j = start_index; j <= end_index - 1; j++) { + if (comparator.compare(array[j], pivot) < 0) { + pivot_index++; + T temp = array[pivot_index]; + array[pivot_index] = array[j]; + array[j] = temp; + } + } + + T temp = array[pivot_index + 1]; + array[pivot_index + 1] = array[end_index]; + array[end_index] = temp; + + int index = pivot_index + 1; + quick_sort(array, start_index, index - 1, comparator); + quick_sort(array, index + 1, end_index, comparator); + } + } +} diff --git a/src/Radix_Sort.java b/src/Radix_Sort.java new file mode 100644 index 0000000..92dc67a --- /dev/null +++ b/src/Radix_Sort.java @@ -0,0 +1,34 @@ +import java.util.Arrays; +import java.util.Comparator; +public class Radix_Sort { + public static void radix_sort(Integer[] array, Comparator comparator) { + int array_length = array.length; + int maximum = Arrays.stream(array).max(comparator).orElse(array[0]); + + for(int i = 1; maximum / i > 0; i *= 10) { + count_sort(array, array_length, i, comparator); + } + + + } + private static void count_sort(Integer[] array, int array_length, int i, Comparator comparator ) { + Integer[] final_array = new Integer[array_length]; + int[] coutning = new int[10]; + Arrays.fill(coutning, 0); + + for (int j = 0; j < array_length; j++) { + coutning[(array[j] / i) % 10]++; + } + + for (int k = 1; k < 10; k++) { + coutning[k] += coutning[k - 1]; + } + + for (int l = array_length - 1; l >= 0; l--) { + final_array[coutning[(array[l] / i) % 10] - 1] = array[l]; + coutning[(array[l] / i) % 10]--; + } + + System.arraycopy(final_array, 0, array, 0, array_length); + } +} diff --git a/src/Selection_Sort.java b/src/Selection_Sort.java new file mode 100644 index 0000000..c65fffe --- /dev/null +++ b/src/Selection_Sort.java @@ -0,0 +1,18 @@ +import java.util.Comparator; +public class Selection_Sort { + public static void selection_sort(T[] array, Comparator comparator) { + int array_length = array.length; + + for (int i = 0; i < array_length - 1; i++) { + int min_index = i; + for (int j = i + 1; j < array_length; j++) { + if (comparator.compare(array[j], array[min_index]) < 0) { + min_index = j; + } + } + T temporary = array[min_index]; + array[min_index] = array[i]; + array[i] = temporary; + } + } +} diff --git a/src/Shell_Sort.java b/src/Shell_Sort.java new file mode 100644 index 0000000..092c0a0 --- /dev/null +++ b/src/Shell_Sort.java @@ -0,0 +1,17 @@ +import java.util.Comparator; +public class Shell_Sort { + public static void shell_Sort(T[] array, Comparator comparator) { + int array_length = array.length; + + for (int distance = array_length / 2; distance > 0; distance = distance / 2) { + for (int i = distance; i < array_length; i++) { + T temporary = array[i]; + int j; + for (j = i; j < distance && comparator.compare(array[j - distance], temporary) > 0; j = j - distance) { + array[j] = array[j - distance]; + } + array[j] = temporary; + } + } + } +}