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
#include <iostream> | |
#include <vector> | |
template <class T> | |
size_t partition(std::vector<T>& vec, size_t p, size_t q); | |
template <class T> | |
void quicksort_util(std::vector<T>& vec, size_t p, size_t r); | |
template <class T> |
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
#include <vector> | |
#include <iostream> | |
#include <utility> | |
template <class T> | |
auto binarySearch(std::vector<T>& vec, const T& key) | |
{ | |
size_t left = 0; | |
size_t right = vec.size() + 1; | |
size_t middle = (left + right) / 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
/* | |
* A generic implementation of binary search for the Linux kernel | |
* | |
* Copyright (C) 2008-2009 Ksplice, Inc. | |
* Author: Tim Abbott <[email protected]> | |
* | |
* This program is free software; you can redistribute it and/or | |
* modify it under the terms of the GNU General Public License as | |
* published by the Free Software Foundation; version 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
#include <iostream> | |
#include <string> | |
#include <algorithm> | |
void solution(std::string str) { | |
str += " "; | |
auto size = str.size(); | |
size_t start = 0; | |
for(size_t i = 0; i < size; i++) { | |
size_t end = str.find(' ', start); |
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
#include <vector> | |
#include <iostream> | |
using ull = unsigned long long; | |
ull fast_power(ull num, int power) | |
{ | |
if(num < 0 || power < 0) { | |
std::cout << "invalid arguments specified\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
#include <iostream> | |
#include <cmath> | |
#include <vector> | |
using ull = unsigned long long; | |
bool isPrime(int n) | |
{ | |
for(int i = 2; i < n / 2; i++) | |
{ |
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
template <class T> | |
auto countingSort(const std::vector<T>& vec) | |
{ | |
if(vec.size() <= 1) std::move(vec); | |
std::vector<T> counts(vec.size(), 0); | |
const auto size = vec.size(); | |
for(size_t i = 0; i < size - 1; ++i) | |
{ |
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
1) Find the last element of a list. | |
xs = [1, 2, 3, 4] | |
last xs | |
2) Find the last but one element of a list. | |
xs = [1, 2, 3, 4] | |
last (init xs) | |
3) Find the K'th element of a list. The first element in the list is number 1. | |
xs = [1, 2, 4, 3, 5, 6, 8, 7] |
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
#include <iostream> | |
#include <unordered_map> | |
#include <vector> | |
#include <string> | |
#include <algorithm> | |
template <class T> | |
auto&& minusKFromAll(std::vector<T> v, T k) { | |
std::transform(std::begin(v), std::end(v), std::begin(v), [](T i) { return i - 1; }); |
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
#include <vector> | |
#include <iostream> | |
#include <algorithm> | |
#include <iterator> | |
template <class ForwardIterator, class Comp = std::less<>> | |
auto lis(ForwardIterator begin, | |
ForwardIterator end, | |
Comp comp = Comp()) { | |
OlderNewer