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

Hacktober Issue #3 #10

Merged
merged 2 commits into from Oct 16, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
Expand Up @@ -16,12 +16,15 @@
*/
public class BucketSort {

// Variable Declaration; Made a default bucket size.
private static final int DEFAULT_BUCKET_SIZE = 5;

// function that calls the bucketSort function with the default bucket size.
public static void sort(int[] arr) {
bucketSort(arr, DEFAULT_BUCKET_SIZE);
}

// function to implement the bucketSort
public static void bucketSort(int arr[], int size) {
if (arr.length == 0) return;

Expand Down Expand Up @@ -56,6 +59,7 @@ public static void bucketSort(int arr[], int size) {
}
}

// This is a main that test(s) the bucket sort.
public static void main(String[] args) {
int[] arr1 = {10, 3, 7, 5, 1, 15, 20};
BucketSort.sort(arr1);
Expand Down
File renamed without changes.
@@ -1,19 +1,25 @@
#include<iostream>
#include<algorithm>
using namespace std;

// this displays the array before and after the sort.
void display(int *array, int size) {
for(int i = 1; i<=size; i++)
cout << array[i] << " ";
cout << endl;
}

//this obtains the max element from the array.
int getMax(int array[], int size) {
int max = array[1];
for(int i = 2; i<=size; i++) {
if(array[i] > max)
max = array[i];
}
return max; //the max element from the array
return max;
}

// This implements the Counting Sort.
void countSort(int *array, int size) {
int output[size+1];
int max = getMax(array, size);
Expand All @@ -32,6 +38,8 @@ void countSort(int *array, int size) {
array[i] = output[i]; //store output array to main array
}
}

// These are test cases for the counting sort.
int main() {
int n;
cout << "Enter the number of elements: ";
Expand Down
File renamed without changes.
@@ -1,3 +1,4 @@
// this function implements the counting sort.
function countingSort(arr, min, max)
{
var i, z = 0, count = [];
Expand All @@ -19,7 +20,7 @@ function countingSort(arr, min, max)
}



// tests the counting sort.
let arr = [3, 44, 38, 5, 47, 15, 36, 26, 27, 2, 46, 4, 19, 50, 48]
console.log('Unsorted: ', arr);
console.log('Sorted: ', countingSort(arr, 0, 5));
File renamed without changes.
3 changes: 3 additions & 0 deletions Sorting/heapsort.cpp → Sorting/HeapSort/heapsort.cpp
Expand Up @@ -26,6 +26,8 @@ void MaxHeapify(int a[], int i, int n)
a[j/2] = temp;
return;
}

// function to implement the heapsort.
void HeapSort(int a[], int n)
{
int i, temp;
Expand All @@ -39,6 +41,7 @@ void HeapSort(int a[], int n)
MaxHeapify(a, 1, i - 1);
}
}

void Build_MaxHeap(int a[], int n)
{
int i;
Expand Down
File renamed without changes.
Expand Up @@ -2,12 +2,14 @@

public class InsertionSort {

// this tests the insertion sort.
public static void main(String[] args) {
int[] arr = {1, 4, 2, 3, 6, 5};
InsertionSort.insertionSort(arr);
System.out.println(Arrays.toString(arr));
}

// this function implements the insertion sort algorithm.
public static void insertionSort(int[] arr) {
for(int i = 1; i < arr.length; i++) {
int key = i;
Expand All @@ -17,6 +19,7 @@ public static void insertionSort(int[] arr) {
}
}

// this function swaps elements of certain indexes in the array.
public static void swap(int[] arr, int index1, int index2) {
int temp = arr[index1];
arr[index1] = arr[index2];
Expand Down
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
4 changes: 2 additions & 2 deletions Sorting/QuickSort.js → Sorting/QuickSort/QuickSort.js
@@ -1,3 +1,4 @@
// implements the quick sort algorithm through code.
const quickSort = (oldArray) => {
if (oldArray.length <= 1) {
return oldArray;
Expand All @@ -19,8 +20,7 @@ const quickSort = (oldArray) => {
}
};



// tests the quick sort algorithm.
let arr = [3, 44, 38, 5, 47, 15, 36, 26, 27, 2, 46, 4, 19, 50, 48]
console.log('Unsorted: ', arr);
console.log('Sorted: ', quickSort(arr));
File renamed without changes.
@@ -1,24 +1,28 @@
import java.util.Arrays;

public class SelectionSort {
// tests the Selection Sort Algorithm.
public static void main(String[] args) {
int[] arr = {1, 2, 5, 3, 4, 6};
SelectionSort.sort(arr);
System.out.println(Arrays.toString(arr));
}

// Swaps elements of certain indexes.
public static void swap(int[] arr, int index1, int index2) {
int temp = arr[index1];
arr[index1] = arr[index2];
arr[index2] = temp;
}

// Implements the Selection Sort.
public static void sort(int[] arr) {
for(int i = 0; i < arr.length; i++) {
swap(arr, i, findSmallest(arr, i));
}
}

// finds the smallest element
public static int findSmallest(int[] arr, int start) {
int toReturn = start;

Expand Down
File renamed without changes.