Skip to content

Commit 900d4e2

Browse files
author
Kendrick Ledet
authored
Merge pull request kennyledet#515 from mike168m/master
Add maximum sub array sum in C++
2 parents bba456a + caeb61d commit 900d4e2

4 files changed

Lines changed: 168 additions & 0 deletions

File tree

Lines changed: 80 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,80 @@
1+
#pragma once
2+
3+
#include <iostream>
4+
#include <vector>
5+
#include <iterator>
6+
#include <algorithm> // hehe... algoception :)
7+
#include <list>
8+
9+
// an undirected graph
10+
template<class Type>
11+
class Graph
12+
{
13+
public:
14+
int vertices;
15+
int edges;
16+
std::vector<std::vector<Type>> adjList;
17+
18+
explicit Graph(int numVertices)
19+
:vertices(numVertices), edges(0)
20+
{
21+
adjList = std::vector<std::vector<Type>>(vertices);
22+
std::generate(adjList.begin(), adjList.end(), []{
23+
return std::vector<Type>{};
24+
});
25+
}
26+
27+
~Graph() = default;
28+
29+
void add(Type v, Type w) {
30+
adjList[v].push_back(w);
31+
adjList[w].push_back(v);
32+
edges++;
33+
}
34+
35+
std::vector<Type>& adj(Type v) {
36+
return adjList[v];
37+
}
38+
39+
std::string to_string() const {
40+
std::string str;
41+
for (int i = 0; i < vertices; i++) {
42+
str += std::to_string(i) + "\n";
43+
for (auto t : adj(i)) {
44+
str += "\t" + t;
45+
}
46+
}
47+
48+
return str;
49+
}
50+
};
51+
52+
template<class Type>
53+
class DepthFirstSearch {
54+
const Type startVertex;
55+
Graph<Type>& graph;
56+
std::vector<bool> markedVertexes;
57+
public:
58+
DepthFirstSearch(Graph<Type>& _graph, Type _startVertex)
59+
:startVertex(_startVertex), graph(_graph)
60+
{
61+
markedVertexes = std::vector<bool>(_graph.vertices);
62+
}
63+
64+
void dfs() {
65+
dfs_impl(graph, this->startVertex);
66+
}
67+
68+
void dfs_impl(Graph<Type>& _graph, Type start) {
69+
markedVertexes[start] = true;
70+
for (const Type& t : _graph.adj(start)) {
71+
if (!markedVertexes[t]) {
72+
dfs_impl(_graph, t);
73+
}
74+
}
75+
}
76+
};
77+
78+
79+
80+
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
#include <iostream>
2+
#include <string>
3+
#include "graph.h"
4+
5+
6+
int main() {
7+
int key = 0;
8+
Graph<int> g(13);
9+
g.add(0, 5);
10+
g.add(0, 6);
11+
g.add(1, 6);
12+
13+
DepthFirstSearch<int> depthFirstSearch(g, 6);
14+
depthFirstSearch.dfs();
15+
16+
std::cin >> key;
17+
}
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
TITLE Linear/Sequential search implemented in x86 assembly (MASM)
2+
; Search for a value in an array of bytes by comparing each value with a key
3+
; return the position of the key
4+
; O(n) time complexity
5+
; the address of the first element in the array must be in the edx
6+
; and the arrays length in the ecx register
7+
; the index of the key is returned in the eax register
8+
9+
.386 ; minimum CPU required to run the program
10+
.model flat, stdcall ; identifies the memory model for the program,
11+
; in this case its protected mode. It also tells which
12+
; calling convention to use for procedures
13+
.stack 4096
14+
15+
.data
16+
data db 42, 40, 70, 90, 62, 70, 40 ; our array of data
17+
18+
.code
19+
; implementation of sequential search
20+
SequentialSearch PROC
21+
xor eax, eax ; clear eax register just in case
22+
BEGIN_LOOP:
23+
cmp BYTE PTR[edx], bl ; if [edx] == bl
24+
je FOUND ; then goto FOUND
25+
inc eax ; else increment eax
26+
inc edx ; increment edx
27+
loop BEGIN_LOOP ; automatically decrements ecx register
28+
FOUND:
29+
ret
30+
SequentialSearch ENDP
31+
32+
; entry point
33+
main PROC
34+
mov bl, 90 ; search for 92 in array
35+
mov edx, OFFSET data
36+
mov ecx, LENGTHOF data
37+
call SequentialSearch
38+
main ENDP
39+
40+
END main
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
#include <algorithm>
2+
#include <iostream>
3+
#include <vector>
4+
5+
template<typename T>
6+
T kadane_method(const std::vector<T>& v){
7+
T ret = 0;
8+
std::for_each(v.begin(), v.end(), [c_max = 0, max = 0, &ret](T m) mutable {
9+
c_max = c_max + m;
10+
// the ccurrent max is a negative number,
11+
// we can't use that cuz it will not give us
12+
// the maximum sum
13+
if (c_max < 0) c_max = 0;
14+
// we have to check if there is a new max
15+
// if c_max is greater than it means there is a new max so we
16+
// have to use that instead.
17+
if (max < c_max) max = c_max;
18+
19+
//std::cout << "m: " << m << " c_max: "
20+
// << c_max << " max: " << max << '\n';
21+
ret = max;
22+
});
23+
24+
return ret;
25+
}
26+
27+
int main() {
28+
std::cout << kadane_method<int>({-2, -3, 4, -1, -2, 1, 5, -3}) << '\n';
29+
std::cout << kadane_method<int>({-2, 1, -3, 4, -1, 2, 1, -5, 4}) << '\n';
30+
std::cout << kadane_method<int>({2, 3, 7, -5, -1, 4, -10}) << '\n';
31+
}

0 commit comments

Comments
 (0)