Skip to content

Commit 9bef7a9

Browse files
authored
Merge branch 'master' into master
2 parents a238fa3 + a4db6c7 commit 9bef7a9

File tree

15 files changed

+1016
-384
lines changed

15 files changed

+1016
-384
lines changed

CONTRIBUTING.md

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ If you fixed or added something useful to the project, you can send pull-request
1515
Bugs
1616
----
1717

18-
If you found an error, mistype or any other flawback in the project, please report about it using [issues](https://github.com/Thuva4/Algorithms_Example/issues).
18+
If you found an error, mistype or any other flaw in the project, please report about it using [issues](https://github.com/Thuva4/Algorithms_Example/issues).
1919
The more details you provide, the easier it can be reproduced and the faster can be fixed.
2020
Unfortunately, sometimes the bug can be only reproduced in your project or in your environment, so maintainers cannot reproduce it. In this case we believe you can fix the bug and send us the fix.
2121

@@ -24,13 +24,13 @@ Unfortunately, sometimes the bug can be only reproduced in your project or in yo
2424

2525
1. Fork this repository.
2626

27-
2. Check the table in README.md file weather the algorithm is added. If not add a new row with the name of algorithm and create a new folder with a name of the algorithm.
27+
2. Check the table in the README.md file to see if the algorithm has already been added. If not, add a new row with the name of algorithm and create a new folder with a name of the algorithm.
2828

29-
3. Inside the folder create the folder for language you want to share. and add your code.
29+
3. Inside the folder create the folder for language you want to share and add your code.
3030

3131
5. Commit
3232

33-
6. Update the README.md. check the language you have used in the table. (check emoji is ``:+1:`` )
33+
6. Update the README.md. Check the language you have used in the table (check emoji is ``:+1:`` )
3434

3535
7. Commit, Push
3636

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
import java.util.Scanner;
2+
3+
class GFG
4+
{
5+
public static void counting_sort(int a[], int range)
6+
{
7+
int c[] = new int[range]; // declare array for keeping count (frequency)
8+
for(int i = 1; i< range; i++) // initialise them to 0
9+
c[i] = 0;
10+
11+
for(int i = 0; i<a.length; i++) // count the frequency of each number in range
12+
c[a[i]]++;
13+
14+
int k=0;
15+
for(int i = 0; i< range; i++) // for each number in range
16+
for(int j = 0; j<c[i]; j++) // add the number as many times as its frequency
17+
a[k++] = i;
18+
}
19+
public static void main (String[] args)
20+
{
21+
int n,range;
22+
Scanner sc = new Scanner(System.in);
23+
24+
n = sc.nextInt(); // number of elements to be sorted
25+
range = sc.nextInt(); // range of the elements
26+
27+
int a[] = new int[n];
28+
//read the list to be sorted
29+
for(int i = 0; i<n; i++)
30+
a[i] = sc.nextInt();
31+
32+
counting_sort(a,range); // perform counting sort
33+
34+
// display the elements after sorting
35+
for(int i = 0; i<n; i++)
36+
System.out.print(a[i] + " ");
37+
}
38+
}

Doomsday/Swift/Doomsday.swift

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,4 +24,5 @@ print("\(dow(year: 2001, month: 1, day: 15)): \(dowS(year: 2001, month: 1, day:
2424
print("\(dow(year: 2017, month: 10, day: 10)): \(dowS(year: 2017, month: 10, day: 10))")
2525
print("\(dow(year: 2018, month: 1, day: 1)): \(dowS(year: 2018, month: 1, day: 1))")
2626
print("\(dow(year: 2018, month: 2, day: 16)): \(dowS(year: 2018, month: 2, day: 16))")
27-
print("\(dow(year: 2018, month: 5, day: 17)): \(dowS(year: 2018, month: 5, day: 17))")
27+
print("\(dow(year: 2018, month: 5, day: 17)): \(dowS(year: 2018, month: 5, day: 17))")
28+

Doomsday/python/doomsday.py

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
def day_of_week(year, month, day):
2+
t = [0, 3, 2, 5, 0, 3, 5, 1, 4, 6, 2, 4]
3+
year -= month < 3
4+
return (year + int(year/4) - int(year/100) + int(year/400) + t[month-1] + day) % 7
5+
y = input("Enter Year: ")
6+
m = input("Enter month: ")
7+
d = input("Enter day: ")
8+
n = (day_of_week(y,m,d))
9+
if n == 0:
10+
print "Sunday"
11+
elif n == 1:
12+
print "Monday"
13+
elif n==2:
14+
print "Tuesday"
15+
elif n==3:
16+
print "Wednesday"
17+
elif n == 4:
18+
print "Thrusday"
19+
elif n==5:
20+
print "Friday"
21+
else:
22+
print "Saturday"

Fibonacci/Ruby/Fibonacci.rb

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
#Returns the nth fibonacci number
2+
3+
#Iterative Algorithm
4+
def fibonacci_iterative(n)
5+
num1 = 0
6+
num2 = 1
7+
(2..n+1).each { num1, num2 = num2, num1 + num2 }
8+
9+
return num1
10+
end
11+
12+
#Recursive Algorithm
13+
def fibonacci_recursive(n)
14+
return n if n < 2
15+
return fibonacci_recursive(n-1) + fibonacci_recursive(n-2)
16+
end
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
2+
function FischerYatesShuffle(tbl) {
3+
var N = tbl.length;
4+
for (var i = 1; i < N; i++) {
5+
var j = Math.floor(Math.random()*i);
6+
var tmp = tbl[i];
7+
tbl[i] = tbl[j];
8+
tbl[j] = tmp;
9+
}
10+
}
11+
12+
13+
tbl = [];
14+
for (var i = 0; i < 20; i++) {
15+
tbl[tbl.length] = i + 1;
16+
}
17+
console.log("Initial array:");
18+
console.log(tbl);
19+
FischerYatesShuffle(tbl);
20+
console.log("Shuffled array:");
21+
console.log(tbl);
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
// Hamming distance between two strings of equal length:
2+
// sum the number of positions where the two strings are different
3+
function hammingDistance (s1, s2) {
4+
if(s1.length !== s2.length) throw 'The two strings must have equal length'
5+
let distance = 0
6+
for (let i = 0; i < s1.length; i++) {
7+
if (s1.charAt(i) !== s2.charAt(i)) {
8+
distance++
9+
}
10+
}
11+
return distance
12+
}
13+
14+
// EXAMPLE:
15+
const s1 = 'bend'
16+
const s2 = 'bond'
17+
console.log(hammingDistance(s1,s2))
Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
/*
2+
* @author Abhishek Datta
3+
* @github_id abdatta
4+
* @since 15th October, 2017
5+
*
6+
* The following algroithm takes a list of numbers
7+
* and sorts them using the insertion sort algorithm.
8+
*/
9+
10+
#include <iostream>
11+
using namespace std;
12+
13+
// function to swap two numbers
14+
void swap(int *a, int *b)
15+
{
16+
int t = *a;
17+
*a = *b;
18+
*b = t;
19+
}
20+
21+
// function to perform insertion sort
22+
void insertion_sort(int *a, int n)
23+
{
24+
for (int i = 1; i < n; i++) // iterating over all the elements
25+
{
26+
int j = i; // j denotes the final position of the current element, which is initialised to the current position
27+
28+
while (j > 0 && a[j-1] > a[j]) // shift j until it is in the correct position
29+
{
30+
swap(a[j], a[j-1]); // swap with the shifted element
31+
j--;
32+
}
33+
}
34+
}
35+
36+
// main function to try the algorithm
37+
int main()
38+
{
39+
int n; // number of elements to be read
40+
41+
cin>>n;
42+
int *a = new int[n];
43+
44+
// reading a list of unsorted numbers
45+
for (int i = 0; i < n; ++i)
46+
cin>>a[i];
47+
48+
insertion_sort(a, n); // sorting the list
49+
50+
// printing the sorted list
51+
for (int i = 0; i < n; ++i)
52+
cout<<a[i]<<' ';
53+
cout<<endl;
54+
55+
return 0;
56+
}

InsertionSort/c/InsertionSort.c

Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
// including liberary
2+
#include <stdio.h>
3+
4+
/*
5+
the algo goes here
6+
*/
7+
void Sort_Array(int arr[], int size) // it will accept interger type array and size of the array
8+
{
9+
// local variable defination
10+
int i, key, j;
11+
for (i = 1; i < size; i++)
12+
{
13+
key = arr[i]; // the i th value
14+
j = i-1; // j is for processing from back
15+
16+
/* Move elements of arr[0..i-1], that are
17+
greater than key, to one position ahead
18+
of their current position */
19+
while (j >= 0 && arr[j] > key) // loop will work till j th value of array is greater than i th value of array and j >= 0
20+
{
21+
arr[j+1] = arr[j];
22+
j--;
23+
}
24+
arr[j+1] = key;
25+
}
26+
}
27+
28+
/*
29+
MAIN FUNCTION
30+
*/
31+
int main() {
32+
// declaring the variable
33+
int size; // size is the array length
34+
int i; // i is for iterations
35+
printf("enter the size of array ");
36+
scanf("%d",&size); // getting size
37+
int array[size]; // declaring array of size entered by the user
38+
39+
// getting value from the user
40+
printf("\nenter values in the array\n");
41+
for (i = 0; i < size; i++) {scanf("%d", &array[i]); }
42+
43+
// printing the original array
44+
printf("\noriginal array -> ");
45+
for (i = 0; i < size; i++) { printf("%d ", array[i]); }
46+
47+
// sorting the array
48+
Sort_Array(array, size);
49+
50+
// printing the sorted array
51+
printf("\nsorted array -> ");
52+
for (i = 0; i < size; i++) { printf("%d ", array[i]); }
53+
54+
return 0;
55+
}
56+
Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
/* BUILD : g++ FisherYatesShuffle.cpp -std=c++11*/
2+
3+
#include <iostream>
4+
#include <vector>
5+
6+
using namespace std;
7+
8+
void LIS(vector<int> input)
9+
{
10+
int input_length = input.size();
11+
int longest_length = 1;
12+
int longest_index = 0;
13+
14+
vector<int> dp(input_length, 1);
15+
vector<int> trace(input_length, -1);
16+
vector<int> output;
17+
18+
cout << "Input : " << input[0] << " ";
19+
for (int i = 1; i < input_length; i++)
20+
{
21+
cout << input[i] << " ";
22+
for (int j = 0; j < i; j++)
23+
{
24+
if (input[i] > input[j])
25+
if (dp[j] + 1 > dp[i])
26+
{
27+
dp[i] = dp[j] + 1;
28+
trace[i] = j;
29+
if (dp[i] > longest_length)
30+
{
31+
longest_length = dp[i];
32+
longest_index = i;
33+
}
34+
}
35+
}
36+
}
37+
cout << endl;
38+
39+
cout << "Longest length : " << longest_length << endl;
40+
cout << "Longest sequence : ";
41+
int i = longest_index;
42+
while (trace[i] != -1)
43+
{
44+
output.push_back(input[i]);
45+
i = trace[i];
46+
}
47+
output.push_back(input[i]);
48+
49+
for (int i = output.size() - 1; i >= 0 ; i--)
50+
cout << output[i] << " ";
51+
cout << endl;
52+
cout << endl;
53+
}
54+
55+
int main()
56+
{
57+
vector<int> a {17,12,5,3,5,3,14,19,12,2};
58+
vector<int> b {9,12,2,7,18,9,9,13,6,19};
59+
vector<int> c {14,7,15,5,16,8,3,7,13,20};
60+
vector<int> d {12,4,19,3,6,20,11,3,15,9};
61+
vector<int> e {13,8,11,1,14,7,3,6,10,2};
62+
LIS(a);
63+
LIS(b);
64+
LIS(c);
65+
LIS(d);
66+
LIS(e);
67+
return 0;
68+
}

0 commit comments

Comments
 (0)