Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

added the pancakesort and radix sort #516

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
74 changes: 74 additions & 0 deletions Algorithms/HeapSortAlogirithm.java
Original file line number Diff line number Diff line change
@@ -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<n; ++i)
System.out.print(arr[i]+" ");
System.out.println();
}

// Driver program
public static void main(String args[])
{
int arr[] = {12, 11, 13, 5, 6, 7};
int n = arr.length;

HeapSort ob = new HeapSort();
ob.sort(arr);

System.out.println("Sorted array is");
printArray(arr);
}
}
113 changes: 113 additions & 0 deletions Data Structures/LinkedList/DoublyLinkedList.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,113 @@
/**
* A class that implements doubly linked list.
*
* @param <E> The type of the elements stored in the list
*/
public class DoublyLinkedList<E> {
private int size;
private ListNode<E> head;
private ListNode<E> tail;
public DoublyLinkedList(){
this.size = 0;
this.head = new ListNode<E>(null);
this.tail = new ListNode<E>(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<E> node = new ListNode<E>(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<E> 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<E> node = new ListNode<E>(data);
ListNode<E> 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 <E> The type of element ListNode can have
*/
class ListNode<E>{
private ListNode<E> prev = null;
private ListNode<E> next = null;
private E data;
public ListNode(E theData){
this.data = theData;
}

public E getData() {
return data;
}

public ListNode<E> getNext() {
return next;
}

public ListNode<E> getPrev() {
return prev;
}

public void setData(E data) {
this.data = data;
}

public void setNext(ListNode<E> next) {
this.next = next;
}

public void setPrev(ListNode<E> prev) {
this.prev = prev;
}
}
49 changes: 49 additions & 0 deletions Data Structures/PancakeSort.java
Original file line number Diff line number Diff line change
@@ -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;
}
}
53 changes: 53 additions & 0 deletions Data Structures/RadixSort.java
Original file line number Diff line number Diff line change
@@ -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);
}
}