Skip to content

Commit 76c791b

Browse files
committed
Merge remote-tracking branch 'refs/remotes/origin/leetcode-cpp' into leetcode-cpp
2 parents 079a161 + cd6145c commit 76c791b

File tree

2 files changed

+136
-40
lines changed

2 files changed

+136
-40
lines changed

.github/workflows/codeql.yml

Lines changed: 50 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -1,51 +1,61 @@
1-
name: "CodeQL"
1+
name: "Code Scanning - Action"
22

33
on:
44
push:
5-
branches: [ "master" ]
5+
branches: [master]
66
pull_request:
7-
branches: [ "master" ]
7+
branches: [master]
8+
schedule:
9+
# ┌───────────── minute (0 - 59)
10+
# │ ┌───────────── hour (0 - 23)
11+
# │ │ ┌───────────── day of the month (1 - 31)
12+
# │ │ │ ┌───────────── month (1 - 12 or JAN-DEC)
13+
# │ │ │ │ ┌───────────── day of the week (0 - 6 or SUN-SAT)
14+
# │ │ │ │ │
15+
# │ │ │ │ │
16+
# │ │ │ │ │
17+
# * * * * *
18+
- cron: '30 1 * * 0'
819

920
jobs:
10-
analyze:
11-
name: Analyze
21+
CodeQL-Build:
22+
# CodeQL runs on ubuntu-latest, windows-latest, and macos-latest
1223
runs-on: ubuntu-latest
24+
1325
permissions:
26+
# required for all workflows
27+
security-events: write
28+
29+
# only required for workflows in private repositories
1430
actions: read
1531
contents: read
16-
security-events: write
32+
1733
steps:
18-
- name: Checkout repository
19-
uses: actions/checkout@v3
20-
- name: Initialize CodeQL
21-
uses: github/codeql-action/init@v2
22-
with:
23-
languages: cpp
24-
# If you wish to specify custom queries, you can do so here or in a config file.
25-
# By default, queries listed here will override any specified in a config file.
26-
# Prefix the list here with "+" to use these queries and those in the config file.
27-
28-
# Details on CodeQL's query packs refer to : https://docs.github.com/en/code-security/code-scanning/automatically-scanning-your-code-for-vulnerabilities-and-errors/configuring-code-scanning#using-queries-in-ql-packs
29-
# queries: security-extended,security-and-quality
30-
31-
# Autobuild attempts to build any compiled languages (C/C++, C#, Go, or Java).
32-
# If this step fails, then you should remove it and run the build manually (see below)
33-
- name: Autobuild
34-
uses: github/codeql-action/autobuild@v2
35-
36-
# ℹ️ Command-line programs to run using the OS shell.
37-
# 📚 See https://docs.github.com/en/actions/using-workflows/workflow-syntax-for-github-actions#jobsjob_idstepsrun
38-
39-
# If the Autobuild fails above, remove it and uncomment the following three lines.
40-
# modify them (or add more) to build your code if your project, please refer to the EXAMPLE below for guidance.
41-
42-
# - run: |
43-
# echo "Run, Build Application using script"
44-
# ./location_of_script_within_repo/buildscript.sh
45-
#
46-
# In our case, this would be a CMake build step.
47-
48-
- name: Perform CodeQL Analysis
49-
uses: github/codeql-action/analyze@v2
50-
with:
51-
category: "/language:cpp"
34+
- name: Checkout repository
35+
uses: actions/checkout@v3
36+
37+
# Initializes the CodeQL tools for scanning.
38+
- name: Initialize CodeQL
39+
uses: github/codeql-action/init@v2
40+
# Override language selection by uncommenting this and choosing your languages
41+
with:
42+
languages: cpp
43+
44+
# Autobuild attempts to build any compiled languages (C/C++, C#, Go, or Java).
45+
# If this step fails, then you should remove it and run the build manually (see below).
46+
- name: Autobuild
47+
uses: github/codeql-action/autobuild@v2
48+
49+
# ℹ️ Command-line programs to run using the OS shell.
50+
# 📚 See https://docs.github.com/en/actions/using-workflows/workflow-syntax-for-github-actions#jobsjob_idstepsrun
51+
52+
# ✏️ If the Autobuild fails above, remove it and uncomment the following
53+
# three lines and modify them (or add more) to build your code if your
54+
# project uses a compiled language
55+
56+
#- run: |
57+
# make bootstrap
58+
# make release
59+
60+
- name: Perform CodeQL Analysis
61+
uses: github/codeql-action/analyze@v2

sorting/stooge_sort.cpp

Lines changed: 86 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,86 @@
1+
/**
2+
* @file
3+
* @brief [Stooge sort implementation](https://en.wikipedia.org/wiki/Stooge_sort)
4+
* in C++
5+
* @details
6+
* Stooge sort is a recursive sorting algorithm.
7+
* It divides the array into 3 parts and proceeds to:
8+
* - sort first two thirds of the array
9+
* - sort last two thirds of the array
10+
* - sort first two thirds of the array
11+
* It's time complexity is O(n^(log3/log1.5)), which is about O(n^2.7),
12+
* which makes it to be not the most efficient sorting algorithm
13+
* on the street on average. Space complexity is O(1).
14+
*/
15+
16+
#include <vector> /// for vector
17+
#include <cassert> /// for assert
18+
#include <algorithm> /// for std::is_sorted
19+
#include <iostream> /// for IO operations
20+
21+
/**
22+
* @brief The stoogeSort() function is used for sorting the array.
23+
* @param L - vector of values (int) to be sorted in in place (ascending order)
24+
* @param i - the first index of the array (0)
25+
* @param j - the last index of the array (L.size() - 1)
26+
* @returns void
27+
*/
28+
void stoogeSort(std::vector<int>* L, size_t i, size_t j) {
29+
if (i >= j) {
30+
return;
31+
}
32+
if ((*L)[i] > (*L)[j]) {
33+
std::swap((*L)[i], (*L)[j]);
34+
}
35+
if (j - i > 1) {
36+
size_t third = (j - i + 1) / 3;
37+
stoogeSort(L, i, j - third);
38+
stoogeSort(L, i + third, j);
39+
stoogeSort(L, i, j - third);
40+
}
41+
}
42+
43+
/**
44+
* @brief Function to test sorting algorithm
45+
* @returns void
46+
*/
47+
void test1() {
48+
std::vector<int> L = { 8, 9, 10, 4, 3, 5, 1 };
49+
stoogeSort(&L, 0, L.size() - 1);
50+
assert(std::is_sorted(std::begin(L), std::end(L)));
51+
}
52+
53+
/**
54+
* @brief Function to test sorting algorithm, one element
55+
* @returns void
56+
*/
57+
void test2() {
58+
std::vector<int> L = { -1 };
59+
stoogeSort(&L, 0, L.size() - 1);
60+
assert(std::is_sorted(std::begin(L), std::end(L)));
61+
}
62+
63+
/**
64+
* @brief Function to test sorting algorithm, repeating elements
65+
* @returns void
66+
*/
67+
void test3() {
68+
std::vector<int> L = { 1, 2, 5, 4, 1, 5 };
69+
stoogeSort(&L, 0, L.size() - 1);
70+
assert(std::is_sorted(std::begin(L), std::end(L)));
71+
}
72+
73+
/**
74+
* @brief Main function
75+
* @param argc commandline argument count (ignored)
76+
* @param argv commandline array of arguments (ignored)
77+
* @returns 0 on exit
78+
*/
79+
int main() {
80+
test1();
81+
test2();
82+
test3();
83+
84+
std::cout << "All tests have successfully passed!\n";
85+
return 0;
86+
}

0 commit comments

Comments
 (0)