mirror of
https://hub.njuu.cf/TheAlgorithms/C-Plus-Plus.git
synced 2023-10-11 13:05:55 +08:00
Merge pull request #888 from ayaankhan98/master
fix: LGTM code quality and Added docs
This commit is contained in:
commit
d958eec03b
@ -13,13 +13,16 @@ class MinHeap {
|
|||||||
int heap_size; ///< Current number of elements in min heap
|
int heap_size; ///< Current number of elements in min heap
|
||||||
|
|
||||||
public:
|
public:
|
||||||
/** Constructor
|
/** Constructor: Builds a heap from a given array a[] of given size
|
||||||
* \param[in] capacity initial heap capacity
|
* \param[in] capacity initial heap capacity
|
||||||
*/
|
*/
|
||||||
MinHeap(int capacity);
|
explicit MinHeap(int cap) {
|
||||||
|
heap_size = 0;
|
||||||
|
capacity = cap;
|
||||||
|
harr = new int[cap];
|
||||||
|
}
|
||||||
|
|
||||||
/** to heapify a subtree with the root at given index
|
/** to heapify a subtree with the root at given index */
|
||||||
*/
|
|
||||||
void MinHeapify(int);
|
void MinHeapify(int);
|
||||||
|
|
||||||
int parent(int i) { return (i - 1) / 2; }
|
int parent(int i) { return (i - 1) / 2; }
|
||||||
@ -44,14 +47,9 @@ class MinHeap {
|
|||||||
|
|
||||||
/** Inserts a new key 'k' */
|
/** Inserts a new key 'k' */
|
||||||
void insertKey(int k);
|
void insertKey(int k);
|
||||||
};
|
|
||||||
|
|
||||||
/** Constructor: Builds a heap from a given array a[] of given size */
|
~MinHeap() { delete[] harr; }
|
||||||
MinHeap::MinHeap(int cap) {
|
};
|
||||||
heap_size = 0;
|
|
||||||
capacity = cap;
|
|
||||||
harr = new int[cap];
|
|
||||||
}
|
|
||||||
|
|
||||||
// Inserts a new key 'k'
|
// Inserts a new key 'k'
|
||||||
void MinHeap::insertKey(int k) {
|
void MinHeap::insertKey(int k) {
|
||||||
|
@ -1,3 +1,24 @@
|
|||||||
|
/**
|
||||||
|
*
|
||||||
|
* \file
|
||||||
|
* \brief [Disjoint Sets Data Structure
|
||||||
|
* (Disjoint Sets)](https://en.wikipedia.org/wiki/Disjoint-set_data_structure)
|
||||||
|
*
|
||||||
|
* \author [leoyang429](https://github.com/leoyang429)
|
||||||
|
*
|
||||||
|
* \details
|
||||||
|
* A disjoint set data structure (also called union find or merge find set)
|
||||||
|
* is a data structure that tracks a set of elements partitioned into a number
|
||||||
|
* of disjoint (non-overlapping) subsets.
|
||||||
|
* Some situations where disjoint sets can be used are-
|
||||||
|
* to find connected components of a graph, kruskal's algorithm for finding
|
||||||
|
* Minimum Spanning Tree etc.
|
||||||
|
* There are two operation which we perform on disjoint sets -
|
||||||
|
* 1) Union
|
||||||
|
* 2) Find
|
||||||
|
*
|
||||||
|
*/
|
||||||
|
|
||||||
#include <iostream>
|
#include <iostream>
|
||||||
#include <vector>
|
#include <vector>
|
||||||
|
|
||||||
@ -5,16 +26,30 @@ using std::cout;
|
|||||||
using std::endl;
|
using std::endl;
|
||||||
using std::vector;
|
using std::vector;
|
||||||
|
|
||||||
vector<int> root, rnk;
|
vector<int> root, rank;
|
||||||
|
|
||||||
|
/**
|
||||||
|
*
|
||||||
|
* Function to create a set
|
||||||
|
* @param n number of element
|
||||||
|
*
|
||||||
|
*/
|
||||||
void CreateSet(int n) {
|
void CreateSet(int n) {
|
||||||
root = vector<int>(n + 1);
|
root = vector<int>(n + 1);
|
||||||
rnk = vector<int>(n + 1, 1);
|
rank = vector<int>(n + 1, 1);
|
||||||
for (int i = 1; i <= n; ++i) {
|
for (int i = 1; i <= n; ++i) {
|
||||||
root[i] = i;
|
root[i] = i;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
*
|
||||||
|
* Find operation takes a number x and returns the set to which this number
|
||||||
|
* belongs to.
|
||||||
|
* @param x element of some set
|
||||||
|
* @return set to which x belongs to
|
||||||
|
*
|
||||||
|
*/
|
||||||
int Find(int x) {
|
int Find(int x) {
|
||||||
if (root[x] == x) {
|
if (root[x] == x) {
|
||||||
return x;
|
return x;
|
||||||
@ -22,22 +57,39 @@ int Find(int x) {
|
|||||||
return root[x] = Find(root[x]);
|
return root[x] = Find(root[x]);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
*
|
||||||
|
* A utility function to check if x and y are from same set or not
|
||||||
|
* @param x element of some set
|
||||||
|
* @param y element of some set
|
||||||
|
*
|
||||||
|
*/
|
||||||
bool InSameUnion(int x, int y) { return Find(x) == Find(y); }
|
bool InSameUnion(int x, int y) { return Find(x) == Find(y); }
|
||||||
|
|
||||||
|
/**
|
||||||
|
*
|
||||||
|
* Union operation combines two disjoint sets to make a single set
|
||||||
|
* in this union function we pass two elements and check if they are
|
||||||
|
* from different sets then combine those sets
|
||||||
|
* @param x element of some set
|
||||||
|
* @param y element of some set
|
||||||
|
*
|
||||||
|
*/
|
||||||
void Union(int x, int y) {
|
void Union(int x, int y) {
|
||||||
int a = Find(x), b = Find(y);
|
int a = Find(x), b = Find(y);
|
||||||
if (a != b) {
|
if (a != b) {
|
||||||
if (rnk[a] < rnk[b]) {
|
if (rank[a] < rank[b]) {
|
||||||
root[a] = b;
|
root[a] = b;
|
||||||
} else if (rnk[a] > rnk[b]) {
|
} else if (rank[a] > rank[b]) {
|
||||||
root[b] = a;
|
root[b] = a;
|
||||||
} else {
|
} else {
|
||||||
root[a] = b;
|
root[a] = b;
|
||||||
++rnk[b];
|
++rank[b];
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/** Main function */
|
||||||
int main() {
|
int main() {
|
||||||
// tests CreateSet & Find
|
// tests CreateSet & Find
|
||||||
int n = 100;
|
int n = 100;
|
||||||
|
@ -1,49 +1,101 @@
|
|||||||
// Kind of better version of Bubble sort.
|
/**
|
||||||
// While Bubble sort is comparering adjacent value, Combsort is using gap larger
|
*
|
||||||
// than 1 Best case: O(n) Worst case: O(n ^ 2)
|
* \file
|
||||||
|
* \brief [Comb Sort Algorithm
|
||||||
|
* (Comb Sort)](https://en.wikipedia.org/wiki/Comb_sort)
|
||||||
|
*
|
||||||
|
* \author
|
||||||
|
*
|
||||||
|
* \details
|
||||||
|
* - A better version of bubble sort algorithm
|
||||||
|
* - Bubble sort compares adjacent values whereas comb sort uses gap larger
|
||||||
|
* than 1
|
||||||
|
* - Best case Time complexity O(n)
|
||||||
|
* Worst case Time complexity O(n^2)
|
||||||
|
*
|
||||||
|
*/
|
||||||
|
|
||||||
#include <algorithm>
|
#include <algorithm>
|
||||||
|
#include <cassert>
|
||||||
#include <iostream>
|
#include <iostream>
|
||||||
|
|
||||||
int a[100005];
|
/**
|
||||||
int n;
|
*
|
||||||
|
* Find the next gap by shrinking the current gap by shrink factor of 1.3
|
||||||
|
* @param gap current gap
|
||||||
|
* @return new gap
|
||||||
|
*
|
||||||
|
*/
|
||||||
|
int FindNextGap(int gap) {
|
||||||
|
gap = (gap * 10) / 13;
|
||||||
|
|
||||||
int FindNextGap(int x) {
|
return std::max(1, gap);
|
||||||
x = (x * 10) / 13;
|
|
||||||
|
|
||||||
return std::max(1, x);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
void CombSort(int a[], int l, int r) {
|
/** Function to sort array
|
||||||
// Init gap
|
*
|
||||||
int gap = n;
|
* @param arr array to be sorted
|
||||||
|
* @param l start index of array
|
||||||
|
* @param r end index of array
|
||||||
|
*
|
||||||
|
*/
|
||||||
|
void CombSort(int *arr, int l, int r) {
|
||||||
|
/**
|
||||||
|
*
|
||||||
|
* initial gap will be maximum and the maximum possible value is
|
||||||
|
* the size of the array that is n and which is equal to r in this
|
||||||
|
* case so to avoid passing an extra parameter n that is the size of
|
||||||
|
* the array we are using r to initialize the initial gap.
|
||||||
|
*
|
||||||
|
*/
|
||||||
|
int gap = r;
|
||||||
|
|
||||||
// Initialize swapped as true to make sure that loop runs
|
/// Initialize swapped as true to make sure that loop runs
|
||||||
bool swapped = true;
|
bool swapped = true;
|
||||||
|
|
||||||
// Keep running until gap = 1 or none elements were swapped
|
/// Keep running until gap = 1 or none elements were swapped
|
||||||
while (gap != 1 || swapped) {
|
while (gap != 1 || swapped) {
|
||||||
// Find next gap
|
/// Find next gap
|
||||||
gap = FindNextGap(gap);
|
gap = FindNextGap(gap);
|
||||||
|
|
||||||
swapped = false;
|
swapped = false;
|
||||||
|
|
||||||
// Compare all elements with current gap
|
/// Compare all elements with current gap
|
||||||
for (int i = l; i <= r - gap; ++i) {
|
for (int i = l; i <= r - gap; ++i) {
|
||||||
if (a[i] > a[i + gap]) {
|
if (arr[i] > arr[i + gap]) {
|
||||||
std::swap(a[i], a[i + gap]);
|
std::swap(arr[i], arr[i + gap]);
|
||||||
swapped = true;
|
swapped = true;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
void tests() {
|
||||||
|
/// Test 1
|
||||||
|
int arr1[10] = {34, 56, 6, 23, 76, 34, 76, 343, 4, 76};
|
||||||
|
CombSort(arr1, 0, 10);
|
||||||
|
assert(std::is_sorted(arr1, arr1 + 10));
|
||||||
|
std::cout << "Test 1 passed\n";
|
||||||
|
|
||||||
|
/// Test 2
|
||||||
|
int arr2[8] = {-6, 56, -45, 56, 0, -1, 8, 8};
|
||||||
|
CombSort(arr2, 0, 8);
|
||||||
|
assert(std::is_sorted(arr2, arr2 + 8));
|
||||||
|
std::cout << "Test 2 Passed\n";
|
||||||
|
}
|
||||||
|
|
||||||
|
/** Main function */
|
||||||
int main() {
|
int main() {
|
||||||
|
/// Running predefined tests
|
||||||
|
tests();
|
||||||
|
|
||||||
|
/// For user interaction
|
||||||
|
int n;
|
||||||
std::cin >> n;
|
std::cin >> n;
|
||||||
for (int i = 1; i <= n; ++i) std::cin >> a[i];
|
int *arr = new int[n];
|
||||||
|
for (int i = 0; i < n; ++i) std::cin >> arr[i];
|
||||||
CombSort(a, 1, n);
|
CombSort(arr, 0, n);
|
||||||
|
for (int i = 0; i < n; ++i) std::cout << arr[i] << ' ';
|
||||||
for (int i = 1; i <= n; ++i) std::cout << a[i] << ' ';
|
delete[] arr;
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user