-
Notifications
You must be signed in to change notification settings - Fork 503
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #684 from ramsal/main
Upload files!
- Loading branch information
Showing
4 changed files
with
316 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,134 @@ | ||
import java.util.ArrayList; | ||
import java.util.Arrays; | ||
import java.util.List; | ||
|
||
public class Heap<E extends Comparable<E>> { | ||
|
||
private List<E> elements = new ArrayList<>(); | ||
|
||
public static <E extends Comparable<E>> List<E> sort(Iterable<E> elements) { | ||
Heap<E> heap = of(elements); | ||
|
||
List<E> result = new ArrayList<>(); | ||
|
||
while (!heap.isEmpty()) { | ||
result.add(heap.pop()); | ||
} | ||
|
||
return result; | ||
} | ||
|
||
public static <E extends Comparable<E>> Heap<E> of(E... elements) { | ||
return of(Arrays.asList(elements)); | ||
} | ||
|
||
public static <E extends Comparable<E>> Heap<E> of(Iterable<E> elements) { | ||
Heap<E> result = new Heap<>(); | ||
for (E element : elements) { | ||
result.add(element); | ||
} | ||
return result; | ||
} | ||
|
||
public void add(E e) { | ||
elements.add(e); | ||
int elementIndex = elements.size() - 1; | ||
while (!isRoot(elementIndex) && !isCorrectChild(elementIndex)) { | ||
int parentIndex = parentIndex(elementIndex); | ||
swap(elementIndex, parentIndex); | ||
elementIndex = parentIndex; | ||
} | ||
} | ||
|
||
public E pop() { | ||
if (isEmpty()) { | ||
throw new IllegalStateException("You cannot pop from an empty heap"); | ||
} | ||
|
||
E result = elementAt(0); | ||
|
||
int lasElementIndex = elements.size() - 1; | ||
swap(0, lasElementIndex); | ||
elements.remove(lasElementIndex); | ||
|
||
int elementIndex = 0; | ||
while (!isLeaf(elementIndex) && !isCorrectParent(elementIndex)) { | ||
int smallerChildIndex = smallerChildIndex(elementIndex); | ||
swap(elementIndex, smallerChildIndex); | ||
elementIndex = smallerChildIndex; | ||
} | ||
|
||
return result; | ||
} | ||
|
||
public boolean isEmpty() { | ||
return elements.isEmpty(); | ||
} | ||
|
||
private boolean isRoot(int index) { | ||
return index == 0; | ||
} | ||
|
||
private int smallerChildIndex(int index) { | ||
int leftChildIndex = leftChildIndex(index); | ||
int rightChildIndex = rightChildIndex(index); | ||
|
||
if (!isValidIndex(rightChildIndex)) { | ||
return leftChildIndex; | ||
} | ||
|
||
if (elementAt(leftChildIndex).compareTo(elementAt(rightChildIndex)) < 0) { | ||
return leftChildIndex; | ||
} | ||
|
||
return rightChildIndex; | ||
} | ||
|
||
private boolean isLeaf(int index) { | ||
return !isValidIndex(leftChildIndex(index)); | ||
} | ||
|
||
private boolean isCorrectParent(int index) { | ||
return isCorrect(index, leftChildIndex(index)) && isCorrect(index, rightChildIndex(index)); | ||
} | ||
|
||
private boolean isCorrectChild(int index) { | ||
return isCorrect(parentIndex(index), index); | ||
} | ||
|
||
private boolean isCorrect(int parentIndex, int childIndex) { | ||
if (!isValidIndex(parentIndex) || !isValidIndex(childIndex)) { | ||
return true; | ||
} | ||
|
||
return elementAt(parentIndex).compareTo(elementAt(childIndex)) < 0; | ||
} | ||
|
||
private boolean isValidIndex(int index) { | ||
return index < elements.size(); | ||
} | ||
|
||
private void swap(int index1, int index2) { | ||
E element1 = elementAt(index1); | ||
E element2 = elementAt(index2); | ||
elements.set(index1, element2); | ||
elements.set(index2, element1); | ||
} | ||
|
||
private E elementAt(int index) { | ||
return elements.get(index); | ||
} | ||
|
||
private int parentIndex(int index) { | ||
return (index - 1) / 2; | ||
} | ||
|
||
private int leftChildIndex(int index) { | ||
return 2 * index + 1; | ||
} | ||
|
||
private int rightChildIndex(int index) { | ||
return 2 * index + 2; | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,92 @@ | ||
class MergeSort | ||
{ | ||
|
||
void merge(int arr[], int l, int m, int r) | ||
{ | ||
// Find sizes of two subarrays to be merged | ||
int n1 = m - l + 1; | ||
int n2 = r - m; | ||
|
||
/* Create temp arrays */ | ||
int L[] = new int[n1]; | ||
int R[] = new int[n2]; | ||
|
||
/*Copy data to temp arrays*/ | ||
for (int i = 0; i < n1; ++i) | ||
L[i] = arr[l + i]; | ||
for (int j = 0; j < n2; ++j) | ||
R[j] = arr[m + 1 + j]; | ||
|
||
/* Merge the temp arrays */ | ||
|
||
// Initial indexes of first and second subarrays | ||
int i = 0, j = 0; | ||
|
||
// Initial index of merged subarray array | ||
int k = l; | ||
while (i < n1 && j < n2) { | ||
if (L[i] <= R[j]) { | ||
arr[k] = L[i]; | ||
i++; | ||
} | ||
else { | ||
arr[k] = R[j]; | ||
j++; | ||
} | ||
k++; | ||
} | ||
|
||
/* Copy remaining elements of L[] if any */ | ||
while (i < n1) { | ||
arr[k] = L[i]; | ||
i++; | ||
k++; | ||
} | ||
|
||
/* Copy remaining elements of R[] if any */ | ||
while (j < n2) { | ||
arr[k] = R[j]; | ||
j++; | ||
k++; | ||
} | ||
} | ||
|
||
// merge() | ||
void sort(int arr[], int l, int r) | ||
{ | ||
if (l < r) { | ||
// Find the middle point | ||
int m =l+ (r-l)/2; | ||
|
||
// Sort first and second halves | ||
sort(arr, l, m); | ||
sort(arr, m + 1, r); | ||
|
||
// Merge the sorted halves | ||
merge(arr, l, m, r); | ||
} | ||
} | ||
|
||
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 code | ||
public static void main(String args[]) | ||
{ | ||
int arr[] = { 12, 11, 13, 5, 6, 7 }; | ||
|
||
System.out.println("Original Array"); | ||
printArray(arr); | ||
|
||
MergeSort ob = new MergeSort(); | ||
ob.sort(arr, 0, arr.length - 1); | ||
|
||
System.out.println("\nSorted array"); | ||
printArray(arr); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,68 @@ | ||
import java.io.*; | ||
|
||
class QuickSort{ | ||
|
||
// A utility function to swap two elements | ||
static void swap(int[] arr, int i, int j) | ||
{ | ||
int temp = arr[i]; | ||
arr[i] = arr[j]; | ||
arr[j] = temp; | ||
} | ||
|
||
static int partition(int[] arr, int low, int high) | ||
{ | ||
|
||
int pivot = arr[high]; | ||
|
||
int i = (low - 1); | ||
|
||
for(int j = low; j <= high - 1; j++) | ||
{ | ||
|
||
if (arr[j] < pivot) | ||
{ | ||
|
||
i++; | ||
swap(arr, i, j); | ||
} | ||
} | ||
swap(arr, i + 1, high); | ||
return (i + 1); | ||
} | ||
|
||
static void quickSort(int[] arr, int low, int high) | ||
{ | ||
if (low < high) | ||
{ | ||
|
||
int pi = partition(arr, low, high); | ||
|
||
quickSort(arr, low, pi - 1); | ||
quickSort(arr, pi + 1, high); | ||
} | ||
} | ||
|
||
// Function to print an array | ||
static void printArray(int[] arr, int size) | ||
{ | ||
for(int i = 0; i < size; i++) | ||
System.out.print(arr[i] + " "); | ||
|
||
System.out.println(); | ||
} | ||
|
||
// Driver Code | ||
public static void main(String[] args) | ||
{ | ||
int[] arr = { 10, 7, 8, 9, 1, 5 }; | ||
int n = arr.length; | ||
|
||
System.out.println("Original array: "); | ||
printArray(arr, n); | ||
|
||
quickSort(arr, 0, n - 1); | ||
System.out.println("Sorted array: "); | ||
printArray(arr, n); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
|
||
public class ShellSort { | ||
|
||
public static void sort(int arrayToSort[]) { | ||
|
||
System.out.println("Hola!"); | ||
int n = arrayToSort.length; | ||
|
||
for (int gap = n / 2; gap > 0; gap /= 2) { | ||
for (int i = gap; i < n; i++) { | ||
int key = arrayToSort[i]; | ||
int j = i; | ||
|
||
while (j >= gap && arrayToSort[j - gap] > key) { | ||
arrayToSort[j] = arrayToSort[j - gap]; | ||
j -= gap; | ||
} | ||
arrayToSort[j] = key; | ||
} | ||
} | ||
} | ||
} |