Skip to content

Commit d958eec

Browse files
authored
Merge pull request #888 from ayaankhan98/master
fix: LGTM code quality and Added docs
2 parents 55fe002 + 62aafd1 commit d958eec

File tree

3 files changed

+141
-39
lines changed

3 files changed

+141
-39
lines changed

data_structures/binaryheap.cpp

Lines changed: 9 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -13,13 +13,16 @@ class MinHeap {
1313
int heap_size; ///< Current number of elements in min heap
1414

1515
public:
16-
/** Constructor
16+
/** Constructor: Builds a heap from a given array a[] of given size
1717
* \param[in] capacity initial heap capacity
1818
*/
19-
MinHeap(int capacity);
19+
explicit MinHeap(int cap) {
20+
heap_size = 0;
21+
capacity = cap;
22+
harr = new int[cap];
23+
}
2024

21-
/** to heapify a subtree with the root at given index
22-
*/
25+
/** to heapify a subtree with the root at given index */
2326
void MinHeapify(int);
2427

2528
int parent(int i) { return (i - 1) / 2; }
@@ -44,14 +47,9 @@ class MinHeap {
4447

4548
/** Inserts a new key 'k' */
4649
void insertKey(int k);
47-
};
4850

49-
/** Constructor: Builds a heap from a given array a[] of given size */
50-
MinHeap::MinHeap(int cap) {
51-
heap_size = 0;
52-
capacity = cap;
53-
harr = new int[cap];
54-
}
51+
~MinHeap() { delete[] harr; }
52+
};
5553

5654
// Inserts a new key 'k'
5755
void MinHeap::insertKey(int k) {

data_structures/disjoint_set.cpp

Lines changed: 57 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,43 +1,95 @@
1+
/**
2+
*
3+
* \file
4+
* \brief [Disjoint Sets Data Structure
5+
* (Disjoint Sets)](https://en.wikipedia.org/wiki/Disjoint-set_data_structure)
6+
*
7+
* \author [leoyang429](https://github.com/leoyang429)
8+
*
9+
* \details
10+
* A disjoint set data structure (also called union find or merge find set)
11+
* is a data structure that tracks a set of elements partitioned into a number
12+
* of disjoint (non-overlapping) subsets.
13+
* Some situations where disjoint sets can be used are-
14+
* to find connected components of a graph, kruskal's algorithm for finding
15+
* Minimum Spanning Tree etc.
16+
* There are two operation which we perform on disjoint sets -
17+
* 1) Union
18+
* 2) Find
19+
*
20+
*/
21+
122
#include <iostream>
223
#include <vector>
324

425
using std::cout;
526
using std::endl;
627
using std::vector;
728

8-
vector<int> root, rnk;
29+
vector<int> root, rank;
930

31+
/**
32+
*
33+
* Function to create a set
34+
* @param n number of element
35+
*
36+
*/
1037
void CreateSet(int n) {
1138
root = vector<int>(n + 1);
12-
rnk = vector<int>(n + 1, 1);
39+
rank = vector<int>(n + 1, 1);
1340
for (int i = 1; i <= n; ++i) {
1441
root[i] = i;
1542
}
1643
}
1744

45+
/**
46+
*
47+
* Find operation takes a number x and returns the set to which this number
48+
* belongs to.
49+
* @param x element of some set
50+
* @return set to which x belongs to
51+
*
52+
*/
1853
int Find(int x) {
1954
if (root[x] == x) {
2055
return x;
2156
}
2257
return root[x] = Find(root[x]);
2358
}
2459

60+
/**
61+
*
62+
* A utility function to check if x and y are from same set or not
63+
* @param x element of some set
64+
* @param y element of some set
65+
*
66+
*/
2567
bool InSameUnion(int x, int y) { return Find(x) == Find(y); }
2668

69+
/**
70+
*
71+
* Union operation combines two disjoint sets to make a single set
72+
* in this union function we pass two elements and check if they are
73+
* from different sets then combine those sets
74+
* @param x element of some set
75+
* @param y element of some set
76+
*
77+
*/
2778
void Union(int x, int y) {
2879
int a = Find(x), b = Find(y);
2980
if (a != b) {
30-
if (rnk[a] < rnk[b]) {
81+
if (rank[a] < rank[b]) {
3182
root[a] = b;
32-
} else if (rnk[a] > rnk[b]) {
83+
} else if (rank[a] > rank[b]) {
3384
root[b] = a;
3485
} else {
3586
root[a] = b;
36-
++rnk[b];
87+
++rank[b];
3788
}
3889
}
3990
}
4091

92+
/** Main function */
4193
int main() {
4294
// tests CreateSet & Find
4395
int n = 100;

sorting/comb_sort.cpp

Lines changed: 75 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -1,49 +1,101 @@
1-
// Kind of better version of Bubble sort.
2-
// While Bubble sort is comparering adjacent value, Combsort is using gap larger
3-
// than 1 Best case: O(n) Worst case: O(n ^ 2)
1+
/**
2+
*
3+
* \file
4+
* \brief [Comb Sort Algorithm
5+
* (Comb Sort)](https://en.wikipedia.org/wiki/Comb_sort)
6+
*
7+
* \author
8+
*
9+
* \details
10+
* - A better version of bubble sort algorithm
11+
* - Bubble sort compares adjacent values whereas comb sort uses gap larger
12+
* than 1
13+
* - Best case Time complexity O(n)
14+
* Worst case Time complexity O(n^2)
15+
*
16+
*/
417

518
#include <algorithm>
19+
#include <cassert>
620
#include <iostream>
721

8-
int a[100005];
9-
int n;
22+
/**
23+
*
24+
* Find the next gap by shrinking the current gap by shrink factor of 1.3
25+
* @param gap current gap
26+
* @return new gap
27+
*
28+
*/
29+
int FindNextGap(int gap) {
30+
gap = (gap * 10) / 13;
1031

11-
int FindNextGap(int x) {
12-
x = (x * 10) / 13;
13-
14-
return std::max(1, x);
32+
return std::max(1, gap);
1533
}
1634

17-
void CombSort(int a[], int l, int r) {
18-
// Init gap
19-
int gap = n;
35+
/** Function to sort array
36+
*
37+
* @param arr array to be sorted
38+
* @param l start index of array
39+
* @param r end index of array
40+
*
41+
*/
42+
void CombSort(int *arr, int l, int r) {
43+
/**
44+
*
45+
* initial gap will be maximum and the maximum possible value is
46+
* the size of the array that is n and which is equal to r in this
47+
* case so to avoid passing an extra parameter n that is the size of
48+
* the array we are using r to initialize the initial gap.
49+
*
50+
*/
51+
int gap = r;
2052

21-
// Initialize swapped as true to make sure that loop runs
53+
/// Initialize swapped as true to make sure that loop runs
2254
bool swapped = true;
2355

24-
// Keep running until gap = 1 or none elements were swapped
56+
/// Keep running until gap = 1 or none elements were swapped
2557
while (gap != 1 || swapped) {
26-
// Find next gap
58+
/// Find next gap
2759
gap = FindNextGap(gap);
2860

2961
swapped = false;
3062

31-
// Compare all elements with current gap
63+
/// Compare all elements with current gap
3264
for (int i = l; i <= r - gap; ++i) {
33-
if (a[i] > a[i + gap]) {
34-
std::swap(a[i], a[i + gap]);
65+
if (arr[i] > arr[i + gap]) {
66+
std::swap(arr[i], arr[i + gap]);
3567
swapped = true;
3668
}
3769
}
3870
}
3971
}
4072

41-
int main() {
42-
std::cin >> n;
43-
for (int i = 1; i <= n; ++i) std::cin >> a[i];
73+
void tests() {
74+
/// Test 1
75+
int arr1[10] = {34, 56, 6, 23, 76, 34, 76, 343, 4, 76};
76+
CombSort(arr1, 0, 10);
77+
assert(std::is_sorted(arr1, arr1 + 10));
78+
std::cout << "Test 1 passed\n";
4479

45-
CombSort(a, 1, n);
80+
/// Test 2
81+
int arr2[8] = {-6, 56, -45, 56, 0, -1, 8, 8};
82+
CombSort(arr2, 0, 8);
83+
assert(std::is_sorted(arr2, arr2 + 8));
84+
std::cout << "Test 2 Passed\n";
85+
}
4686

47-
for (int i = 1; i <= n; ++i) std::cout << a[i] << ' ';
87+
/** Main function */
88+
int main() {
89+
/// Running predefined tests
90+
tests();
91+
92+
/// For user interaction
93+
int n;
94+
std::cin >> n;
95+
int *arr = new int[n];
96+
for (int i = 0; i < n; ++i) std::cin >> arr[i];
97+
CombSort(arr, 0, n);
98+
for (int i = 0; i < n; ++i) std::cout << arr[i] << ' ';
99+
delete[] arr;
48100
return 0;
49101
}

0 commit comments

Comments
 (0)