Skip to content

Commit 6316bbb

Browse files
committed
add source code
1 parent a5652cc commit 6316bbb

14 files changed

+622
-1
lines changed

.gitignore

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,4 +7,5 @@ Gemfile
77
Gemfile.lock
88
node_modules
99
package.json
10-
images/Thumbs.db
10+
images/Thumbs.db
11+
*.exe

source_code/QuickSort.class

852 Bytes
Binary file not shown.

source_code/QuickSort.java

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
/*
2+
* @Author: Beinan
3+
* @Date: 2014-11-18 21:28:24
4+
* @Last Modified by: Beinan
5+
* @Last Modified time: 2015-01-04 16:06:40
6+
*/
7+
8+
public class QuickSort {
9+
public static void main(String[] args) {
10+
int[] a = { 4, 3, 7, 11, 2, 6, 5 };
11+
quick_sort(a, 0, 6);
12+
for(int v : a){
13+
System.out.print(v);
14+
System.out.print(" ");
15+
}
16+
17+
}
18+
19+
public static void quick_sort(int[] a, int begin, int end) {
20+
if(begin >= end)
21+
return;
22+
int left = begin, right = end;
23+
int pivot_value = a[left];
24+
25+
while (left < right)
26+
{
27+
if (a[left+1] < pivot_value) {
28+
a[left] = a[left+1];
29+
left++;
30+
} else {
31+
int temp = a[left + 1];
32+
a[left + 1] = a[right];
33+
a[right] = temp;
34+
right--;
35+
}
36+
}
37+
a[left] = pivot_value;
38+
quick_sort(a, begin, left - 1);
39+
quick_sort(a, left + 1, end);
40+
}
41+
}

source_code/array.cpp

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
#include <iostream>
2+
3+
void reverse(int[], int);
4+
void output(int[], int);
5+
6+
int main(){
7+
8+
int array[] = { 5 , 4 , 10 , 1 , 7 , 9 , 2 };
9+
reverse(array, 7);
10+
output(array, 7);
11+
return 0;
12+
}
13+
14+
void reverse(int array[], int length){
15+
int left = 0;
16+
int right = length - 1;
17+
while(left < right){
18+
int temp = array[left];
19+
array[left] = array[right];
20+
array[right] = temp;
21+
left ++;
22+
right --;
23+
}
24+
}
25+
void output(int array[], int length){
26+
for(int i = 0; i < length; i++){
27+
std::cout << array[i] << std::endl;
28+
}
29+
}

source_code/binary_search_array.cpp

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
/*
2+
* @Author: Beinan
3+
* @Date: 2015-01-04 17:03:46
4+
* @Last Modified by: Beinan
5+
* @Last Modified time: 2015-01-04 19:14:34
6+
*/
7+
8+
#include <iostream>
9+
10+
using namespace std;
11+
12+
int binary_search(int a[], int key, int begin, int end){
13+
if(begin >= end)
14+
return -1; //array is empty, not found
15+
16+
int middle = (begin + end) / 2;
17+
if(a[middle] > key)
18+
return binary_search(a, key, begin, middle);
19+
else if(a[middle] < key)
20+
return binary_search(a, key, middle + 1, end);
21+
else
22+
return middle;
23+
}
24+
25+
int binary_search_iterative(int a[], int key, int begin, int end){
26+
int middle;
27+
while(begin < end){
28+
middle = (begin + end) / 2;
29+
if(a[middle] > key)
30+
end = middle;
31+
else if(a[middle] < key)
32+
begin = middle + 1;
33+
else
34+
return middle;
35+
}
36+
return -1; //array is empty, not found
37+
38+
39+
}
40+
int main(){
41+
int a[] = { 1, 3, 5, 7, 9};
42+
for(int i = 0; i < 15; i++){
43+
cout << "Searching " << i << endl;
44+
cout << " Position is " << binary_search_iterative(a, i, 0, 5) << endl;
45+
}
46+
return 0;
47+
}

source_code/fib.cpp

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
#include <iostream>
2+
3+
int fib(int n){
4+
if(n < 2)
5+
return n;
6+
else
7+
return fib(n - 1) + fib(n - 2);
8+
}
9+
10+
int main(){
11+
for(int i = 0; i < 50; i++){
12+
std::cout << fib(i) << std::endl;
13+
}
14+
}

source_code/gcd.cpp

Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
/*
2+
* @Author: Beinan
3+
* @Date: 2015-01-04 17:51:36
4+
* @Last Modified by: Beinan
5+
* @Last Modified time: 2015-01-04 18:12:57
6+
*/
7+
8+
#include <iostream>
9+
10+
using namespace std;
11+
12+
int gcd_mod(int m, int n){
13+
if(n == 0)
14+
return m;
15+
else
16+
return gcd_mod(n, m % n);
17+
18+
}
19+
20+
int gcd_sub(int m, int n){
21+
if(m == n || n == 0)
22+
return m;
23+
if(m == 0)
24+
return n;
25+
if(m > n)
26+
return gcd_sub(n, m - n);
27+
else
28+
return gcd_sub(m, n - m);
29+
30+
}
31+
32+
//gcd binary
33+
int gcd(int m, int n){
34+
if (m == n)
35+
return m;
36+
37+
if (m == 0)
38+
return n;
39+
40+
if (n == 0)
41+
return m;
42+
43+
// look for factors of 2
44+
if (~m & 1) // m is even
45+
{
46+
if (n & 1) // n is odd
47+
return gcd(m >> 1, n);
48+
else // both m and n are even
49+
return gcd(m >> 1, n >> 1) << 1;
50+
}
51+
52+
if (~n & 1) // m is odd, n is even
53+
return gcd(m, n >> 1);
54+
55+
// reduce larger argument
56+
if (m > n)
57+
return gcd((m - n) >> 1, n);
58+
59+
return gcd((n - m) >> 1, m);
60+
}
61+
int main(){
62+
cout << gcd_sub(99, 66) << endl;
63+
cout << gcd_mod(99, 66) << endl;
64+
cout << gcd(99, 66) << endl;
65+
return 0;
66+
}

source_code/josephus_array.cpp

Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
/*
2+
* @Author: Beinan
3+
* @Date: 2014-12-28 13:18:47
4+
* @Last Modified by: Beinan
5+
* @Last Modified time: 2014-12-28 13:29:55
6+
*/
7+
8+
#include <iostream>
9+
10+
class joseph_circle{
11+
int* next_persons; //array of the indexes of next person
12+
int circle_length;
13+
int tail;
14+
public:
15+
joseph_circle(int circle_length) : circle_length(circle_length){
16+
next_persons = new int[circle_length];
17+
for(int i = 0; i < circle_length; i++){
18+
next_persons[i] = (i + 1) % circle_length; //points to next person in the circle
19+
}
20+
tail = circle_length - 1;
21+
}
22+
23+
void eliminate(int step) {
24+
int p = tail;
25+
while(next_persons[p] != p){ //equivalent to p->next != p
26+
//while more than one element in the circle
27+
for(int i = 0; i < step - 1; i++){
28+
p = next_persons[p]; //go forward
29+
}
30+
int eliminated_node = next_persons[p]; //eliminate the next one
31+
next_persons[p] = next_persons[next_persons[p]];
32+
if(eliminated_node == tail)
33+
tail = p; //update tail
34+
std::cout << "deleting:" << eliminated_node << std::endl;
35+
output();
36+
}
37+
38+
}
39+
void output(){
40+
int p = tail;
41+
while(true){
42+
p = next_persons[p];
43+
std::cout << p << " ";
44+
if(p == tail)
45+
break; //reach the last element of the circle
46+
}
47+
std::cout << std::endl;
48+
}
49+
};
50+
int main(){
51+
52+
joseph_circle circle(6);
53+
54+
circle.eliminate(3); //eliminate using a step of 3
55+
return 0;
56+
}

source_code/josephus_linkedlist.cpp

Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
1+
/*
2+
* @Author: Beinan
3+
* @Date: 2014-12-28 12:38:26
4+
* @Last Modified by: Beinan
5+
* @Last Modified time: 2014-12-28 13:19:32
6+
*/
7+
#include <iostream>
8+
9+
struct node{
10+
int payload;
11+
node* next;
12+
node(int payload) {this->payload = payload;};
13+
};
14+
15+
class joseph_circle{
16+
node* tail;
17+
node* eliminate_ptr;
18+
public:
19+
joseph_circle() { tail = nullptr; }
20+
//add a new node to joseph circle
21+
void add(int value){
22+
if(tail == nullptr){ //if the joseph circle is empty, modify tail.
23+
tail = new node(value);
24+
tail->next = tail;
25+
}else{ //insert new node as the tail of the circle
26+
node* new_node = new node(value);
27+
new_node->next = tail->next;
28+
tail->next = new_node;
29+
tail = new_node;
30+
}
31+
}
32+
33+
void eliminate(int step) {
34+
node* p = tail;
35+
while(p != nullptr && p->next != p){
36+
//while more than one element in the circle
37+
for(int i = 0; i < step - 1; i++){
38+
p = p->next; //go forward
39+
}
40+
node* eliminated_node = p->next; //eliminate the next one
41+
p->next = p->next->next;
42+
if(eliminated_node == tail)
43+
tail = p; //update tail
44+
std::cout << "deleting:" << eliminated_node->payload << std::endl;
45+
delete eliminated_node;
46+
output();
47+
}
48+
49+
}
50+
void output(){
51+
node* p = tail;
52+
while(p != nullptr){
53+
p = p -> next;
54+
std::cout << p->payload << " ";
55+
if(p == tail)
56+
break; //reach the last element of the circle
57+
}
58+
std::cout << std::endl;
59+
}
60+
};
61+
int main(){
62+
63+
joseph_circle circle;
64+
//insert 6 nodes
65+
for( int i = 0; i < 6 ; i++ ){
66+
circle.add(i);
67+
}
68+
circle.eliminate(3); //eliminate using a step of 3
69+
return 0;
70+
}

source_code/linked_list.cpp

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
#include <iostream>
2+
3+
4+
struct node{
5+
int payload;
6+
node* next;
7+
};
8+
9+
node* reverse_recursive(node* head){
10+
if(head==nullptr || head->next==nullptr)
11+
return head;
12+
node* second = head->next;
13+
node* new_head = reverse_recursive(second);
14+
second->next = head;
15+
head->next = nullptr;
16+
return new_head;
17+
}
18+
19+
node* reverse(node* head){
20+
if(head == nullptr || head->next == nullptr)
21+
return head;
22+
node* p = head->next;
23+
node* p_previous = head;
24+
head->next = nullptr;
25+
while(p != nullptr){
26+
node* p_next = p->next;
27+
p->next = p_previous;
28+
p_previous = p;
29+
p = p_next;
30+
}
31+
return p_previous; //new_head
32+
}
33+
int main(){
34+
35+
node* head = nullptr;
36+
//insert 10 nodes
37+
for( int i = 0; i < 10 ; i++ ){
38+
node* new_node = new node;
39+
new_node->payload = i * 10;
40+
new_node->next = head;
41+
head = new_node;
42+
}
43+
head = reverse_recursive(head);
44+
//output each node in the linked-list
45+
node* iterator = head;
46+
while(iterator){
47+
std::cout << iterator->payload << std::endl;
48+
iterator = iterator->next;
49+
}
50+
return 0;
51+
}

0 commit comments

Comments
 (0)