diff --git a/Algorithms/HeapSortAlogirithm.java b/Algorithms/HeapSortAlogirithm.java new file mode 100644 index 00000000..629e6e21 --- /dev/null +++ b/Algorithms/HeapSortAlogirithm.java @@ -0,0 +1,74 @@ +// Java program for implementation of Heap Sort +public class HeapSort +{ + public void sort(int arr[]) + { + int n = arr.length; + + // Build heap (rearrange array) + for (int i = n / 2 - 1; i >= 0; i--) + heapify(arr, n, i); + + // One by one extract an element from heap + for (int i=n-1; i>0; i--) + { + // Move current root to end + int temp = arr[0]; + arr[0] = arr[i]; + arr[i] = temp; + + // call max heapify on the reduced heap + heapify(arr, i, 0); + } + } + + // To heapify a subtree rooted with node i which is + // an index in arr[]. n is size of heap + void heapify(int arr[], int n, int i) + { + int largest = i; // Initialize largest as root + int l = 2*i + 1; // left = 2*i + 1 + int r = 2*i + 2; // right = 2*i + 2 + + // If left child is larger than root + if (l < n && arr[l] > arr[largest]) + largest = l; + + // If right child is larger than largest so far + if (r < n && arr[r] > arr[largest]) + largest = r; + + // If largest is not root + if (largest != i) + { + int swap = arr[i]; + arr[i] = arr[largest]; + arr[largest] = swap; + + // Recursively heapify the affected sub-tree + heapify(arr, n, largest); + } + } + + /* A utility function to print array of size n */ + static void printArray(int arr[]) + { + int n = arr.length; + for (int i=0; i The type of the elements stored in the list + */ +public class DoublyLinkedList { + private int size; + private ListNode head; + private ListNode tail; + public DoublyLinkedList(){ + this.size = 0; + this.head = new ListNode(null); + this.tail = new ListNode(null); + this.head.setNext(this.tail); + this.tail.setPrev(this.head); + } + + /*Implementing some List interface methods to get some idea*/ + + /** + * @return: returning DoublyLinkedList size + */ + public int size() { + return this.size; + } + + /** + * adding data to the end of the list + * + * @param data: data to be added into the end of list + */ + public boolean add(E data){ + ListNode node = new ListNode(data); + // adding the node at the end + node.setNext(this.tail); + node.setPrev(this.tail.getPrev()); + this.tail.getPrev().setNext(node); + this.tail.setPrev(node); + size++; + return true; + } + + /** + * get data from a specified index + * @param index + */ + public E get(int index) { + int counter = 0; + ListNode nodeCounter = this.head; + while(counter <= index){ + nodeCounter = nodeCounter.getNext(); + counter++; + } + return nodeCounter.getData(); + } + + /** + * + * @param index: index position where the node to add + * @param data: Data contained in that node + */ + public boolean add(int index, E data){ + ListNode node = new ListNode(data); + ListNode nodeCounter = this.head; + int counter = 0; + while (counter <= index){ + nodeCounter = nodeCounter.getNext(); + counter++; + } + node.setNext(nodeCounter); + node.setPrev(nodeCounter.getPrev()); + node.getPrev().setNext(node); + nodeCounter.setPrev(node); + return true; + } +} + +/** + * + * @param The type of element ListNode can have + */ +class ListNode{ + private ListNode prev = null; + private ListNode next = null; + private E data; + public ListNode(E theData){ + this.data = theData; + } + + public E getData() { + return data; + } + + public ListNode getNext() { + return next; + } + + public ListNode getPrev() { + return prev; + } + + public void setData(E data) { + this.data = data; + } + + public void setNext(ListNode next) { + this.next = next; + } + + public void setPrev(ListNode prev) { + this.prev = prev; + } +} diff --git a/Data Structures/PancakeSort.java b/Data Structures/PancakeSort.java new file mode 100644 index 00000000..95e60aac --- /dev/null +++ b/Data Structures/PancakeSort.java @@ -0,0 +1,49 @@ +import java.util.Arrays; + +/** + * @author Alessandro Arosio - 05/10/2020 18:35 + */ +public class PancakeSort { + + public static void main(String[] args) { + int[] result = pancakeSort(new int[]{-32, 41, 6, -2, 16, 0, 23}); + Arrays.stream(result).forEach(System.out::println); + } + + static int[] pancakeSort(int[] arr) { + int i = arr.length; + int len = arr.length; + while (i >= 0) { + int pos = findGreatestElement(arr, 0, len); + flip(arr, pos); + flip(arr, len - 1); + len--; + i--; + } + return arr; + } + + public static void flip(int[] arr, int k) { + int i = 0; + int j = k; + while (i < k) { + int tempArray = arr[j]; + arr[j] = arr[i]; + arr[i] = tempArray; + i++; + j--; + } + } + + public static int findGreatestElement(int[] arr, int start, int end) { + int maxValue = Integer.MIN_VALUE; + int maxPosition = 0; + for (int i = start; i < end; i++) { + if (arr[i] >= maxValue) { + maxValue = arr[i]; + maxPosition = i; + } + } + return maxPosition; + } +} diff --git a/Data Structures/RadixSort.java b/Data Structures/RadixSort.java new file mode 100644 index 00000000..85b464a1 --- /dev/null +++ b/Data Structures/RadixSort.java @@ -0,0 +1,53 @@ +public class RadixSort { + + public static void sort(int[] arr) { + int max = 0; + int n = arr.length; + + for (int i = 0; i <= n - 1; i++) + max = Math.max(max, arr[i]); + + double ceiling = Math.ceil(Math.log(max + 1) / Math.log(10)); + + int p = 1; + for (int j = 1; j <= ceiling; j++) { + int[] count = new int[10]; + for (int i = 0; i <= n - 1; i++) + count[(arr[i] / p) % 10] = count[(arr[i] / p) % 10] + 1; + + for (int i = 1; i <= 9; i++) + count[i] = count[i - 1] + count[i]; + + int[] output = new int[n]; + for (int i = n - 1; i >= 0; i--) { + output[count[(arr[i] / p) % 10] - 1] = arr[i]; + count[(arr[i] / p) % 10] = count[(arr[i] / p) % 10] - 1; + } + + for (int i = 0; i <= n - 1; i++) + arr[i] = output[i]; + + p = p * 10; + } + } + + public static void printArr(int[] arr) { + for (int i = 0; i < arr.length; i++) + System.out.print(arr[i] + " "); + System.out.println(); + } + + public static void main(String[] args) { + int[] arr = {8, 7, 9, 12, 2, 3}; + int n = arr.length; + + System.out.print("Elements in array: "); + printArr(arr); + + RadixSort radix = new RadixSort(); + radix.sort(arr); + + System.out.print("Sorted elements in array: "); + printArr(arr); + } +}