Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Multiple Algorithms added, Readme Updated, Restructured Few Directories #93

Merged
merged 47 commits into from
Oct 22, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
47 commits
Select commit Hold shift + click to select a range
7f043ef
Create Merge_Sort.py
Toulik-Das Oct 7, 2020
29b83bf
Rename euclidean algorithm.c to euclidean_algorithm.c
Toulik-Das Oct 7, 2020
2798285
Rename maximum_pairwise_sum.py to MaxPairProduct.py
Toulik-Das Oct 7, 2020
c3feb77
Rename maximum_parwise.ipynb to MaxPairProduct.ipynb
Toulik-Das Oct 7, 2020
d4ffb32
Rename minimum_elements_with_difference_one_or_zero.ipynb to Minimum_…
Toulik-Das Oct 7, 2020
7a569f0
Rename DamianArado.c to Seive of Eratosthenes.c
Toulik-Das Oct 7, 2020
8ce1fed
Rename Seive_of_erat...cpp to Seive of Eratosthenes.cpp
Toulik-Das Oct 7, 2020
99e2a46
Rename Seive of Eratosthenes.c to Seive of Eratosthenes2.c
Toulik-Das Oct 7, 2020
1417701
Rename SieveOfEratosthenes.java to Seive of Eratosthenes.java
Toulik-Das Oct 7, 2020
96f015e
Delete .gitignore
Toulik-Das Oct 7, 2020
3a85e92
Rename knapsack_problem_in_oops.py to knapsack_problem(Using Object O…
Toulik-Das Oct 7, 2020
3fa2202
Rename BinarySearchMain.java to Binary Search.java
Toulik-Das Oct 7, 2020
c974845
Rename PerfectSquare-BSearch.c to PerfectSquare-Binary Search.c
Toulik-Das Oct 7, 2020
eacd3a7
Rename perfect_square.ipynb to Perfect_square using Binary Search.ipynb
Toulik-Das Oct 7, 2020
0cce028
Create bubbleSort.py
Toulik-Das Oct 7, 2020
9c1b143
Create Radix Sort.cpp
Toulik-Das Oct 7, 2020
88567a7
Update Radix Sort.cpp
Toulik-Das Oct 7, 2020
ded98e1
Create Radix Sort.java
Toulik-Das Oct 7, 2020
ab68209
Rename QuickSortMain.java to QuickSort.java
Toulik-Das Oct 7, 2020
595f7f1
Rename quick_sort_recursion.ipynb to quick_sort_using_recursion.ipynb
Toulik-Das Oct 7, 2020
3cd4400
Update README.md
Toulik-Das Oct 7, 2020
a760300
Update README.md
Toulik-Das Oct 7, 2020
b520253
Update README.md
Toulik-Das Oct 7, 2020
51aba9b
Update README.md
Toulik-Das Oct 7, 2020
814e722
Update README.md
Toulik-Das Oct 7, 2020
4c4eedb
Update README.md
Toulik-Das Oct 7, 2020
b2f46c8
Update README.md
Toulik-Das Oct 7, 2020
d46de1d
Update README.md
Toulik-Das Oct 7, 2020
4008bb9
Update README.md
Toulik-Das Oct 7, 2020
07ace81
Update README.md
Toulik-Das Oct 7, 2020
858531e
Update README.md
Toulik-Das Oct 7, 2020
fff0e92
Add files via upload
Toulik-Das Oct 7, 2020
2b871bf
Update README.md
Toulik-Das Oct 7, 2020
be74465
Add files via upload
Toulik-Das Oct 7, 2020
445a04f
Delete 55204045.png
Toulik-Das Oct 7, 2020
2a23333
Update README.md
Toulik-Das Oct 7, 2020
42d77ef
Update README.md
Toulik-Das Oct 7, 2020
b3b9f15
Create README.md
Toulik-Das Oct 7, 2020
3fafa2e
Update bubbleSort.py
Toulik-Das Oct 7, 2020
db599dd
Update Merge_Sort.py
Toulik-Das Oct 7, 2020
04f2e5d
Create README.md
Toulik-Das Oct 7, 2020
40a9e57
Update Radix Sort.java
Toulik-Das Oct 7, 2020
c755878
Create README.md
Toulik-Das Oct 7, 2020
2ef908e
Update README.md
Toulik-Das Oct 7, 2020
6f8983c
Update README.md
Toulik-Das Oct 7, 2020
2bccd61
Update README.md
Toulik-Das Oct 7, 2020
2fa7ba9
Update README.md
Toulik-Das Oct 7, 2020
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Binary file added .github/workflows/65757834.jpg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Original file line number Diff line number Diff line change
Expand Up @@ -54,4 +54,4 @@
]
}
]
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -46,4 +46,4 @@ int main(void)



}
}
4 changes: 0 additions & 4 deletions Algorithms/Greedy_Algorithms/.gitignore

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -40,4 +40,4 @@ def get_max(weights,values):
weights=[10,40,20,30]
values=[60,40,100,120]
max_value=final_value.get_max(weights,values)
print(max_value)
print(max_value)
Original file line number Diff line number Diff line change
Expand Up @@ -72,4 +72,4 @@
]
}
]
}
}
33 changes: 33 additions & 0 deletions Algorithms/Sorting_Algorithms/Bubble_Sort/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
# Bubble Sort
Here's the basic idea of the Bubble Sort algorithm:

1. Start at the beginning of a list (array) of items.
2. Compare the item you're looking at to the next item in the list.
3. If this item is greater than the next one, swap them.
4. Move on to the next item.
5. Repeat steps 1-4 until you go through the whole list without doing any swaps.

#### Bubble Sort Animated
<figure>
<a title="By Swfung8 [CC BY-SA 3.0 (http://creativecommons.org/licenses/by-sa/3.0) or GFDL (http://www.gnu.org/copyleft/fdl.html)], via Wikimedia Commons" href="https://commons.wikimedia.org/wiki/File%3ABubble-sort-example-300px.gif"><img width="400" alt="Bubble-sort-example-300px" src="https://upload.wikimedia.org/wikipedia/commons/c/c8/Bubble-sort-example-300px.gif"/></a><br>
<figcaption>Image: By Swfung8 [CC BY-SA 3.0](http://creativecommons.org/licenses/by-sa/3.0) or [GFDL](http://www.gnu.org/copyleft/fdl.html), via Wikimedia Commons</figcaption>
</figure>

## Problem Statement:
Given an array of integers, sort the array in ascending order using the Bubble Sort algorithm above.

## Input Format

- The first line contains an integer,n , the size of the array a.
- The second line contains n space-separated integers a[i] .

## Output Format
- Print the Sorted array

### Sample Input 1

3</br>
3 2 1

### Sample Output
1 2 3
25 changes: 25 additions & 0 deletions Algorithms/Sorting_Algorithms/Bubble_Sort/bubbleSort.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
def bubbleSort(arr):
n = len(arr)

# Traverse through all array elements
for i in range(n-1):
# range(n) also work but outer loop will repeat one time more than needed.

# Last i elements are already in place
for j in range(0, n-i-1):

# traverse the array from 0 to n-i-1
# Swap if the element found is greater
# than the next element
if arr[j] > arr[j+1] :
arr[j], arr[j+1] = arr[j+1], arr[j]

#Input
arr = []
arr = [int(item) for item in input().split()]

bubbleSort(arr)

print ("Sorted array is:")
for i in range(len(arr)):
print ("%d" %arr[i])
70 changes: 70 additions & 0 deletions Algorithms/Sorting_Algorithms/Merge Sort/Merge_Sort.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
def merge(arr, l, m, r):
n1 = m - l + 1
n2 = r- m

# create temp arrays
L = [0] * (n1)
R = [0] * (n2)

# Copy data to temp arrays L[] and R[]
for i in range(0 , n1):
L[i] = arr[l + i]

for j in range(0 , n2):
R[j] = arr[m + 1 + j]

# Merge the temp arrays back into arr[l..r]
i = 0 # Initial index of first subarray
j = 0 # Initial index of second subarray
k = l # Initial index of merged subarray

while i < n1 and j < n2 :
if L[i] <= R[j]:
arr[k] = L[i]
i += 1
else:
arr[k] = R[j]
j += 1
k += 1

# Copy the remaining elements of L[], if there
# are any
while i < n1:
arr[k] = L[i]
i += 1
k += 1

# Copy the remaining elements of R[], if there
# are any
while j < n2:
arr[k] = R[j]
j += 1
k += 1

# l is for left index and r is right index of the
# sub-array of arr to be sorted
def mergeSort(arr,l,r):
if l < r:

# Same as (l+r)//2, but avoids overflow for
# large l and h
m = (l+(r-1))//2

# Sort first and second halves
mergeSort(arr, l, m)
mergeSort(arr, m+1, r)
merge(arr, l, m, r)


#Input
arr = []
arr = [int(item) for item in input().split()]
n = len(arr)
print ("Given array is")
for i in range(n):
print ("%d" %arr[i]),

mergeSort(arr,0,n-1)
print ("\n\nSorted array is")
for i in range(n):
print ("%d" %arr[i]),
40 changes: 40 additions & 0 deletions Algorithms/Sorting_Algorithms/Merge Sort/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
# Merge Sort

## Why Merge Sort?
Merge sort is one of the most powerful sorting algorithms that you will encounter. In fact it is the native `.sort` algorithm used in many JavaScript environments (such as [Mozilla's](https://bugzilla.mozilla.org/show_bug.cgi?id=224128)). It compares a list of elements using a "Divide and Conquer" strategy.



## How does it work?
Merge Sort works on the basic principal of dividing a list into sub-lists until your sub-lists are of length one or zero. Once your sub-lists are at that size, you merge with a neighboring sub-list. *Note, lists of size one are techinically already sorted.*


## Visualizations
![merge-sort-visualization](https://camo.githubusercontent.com/c9d3bf4590b7284596375ffa0cd98ee62699a757/68747470733a2f2f776562646f63732e63732e75616c62657274612e63612f253745686f6c74652f5432362f4c65637475726536466967362e676966)



## How would I build it?

- A `mergeSort` function may take an array, cut it in half [recursively](https://en.wikipedia.org/wiki/Recursion_(computer_science)) until it has divided the whole array into single items. At this point the recursive calls finally starts returning to the function that invoked it. At this point a separate helper function, `merge`, could be called on a pair of sorted arrays merging them together (see visualization above).
- The `merge` function takes two *sorted* arrays as parameters (an array with one element is technically sorted), looks at the the first elements of the two lists, and assembles a resulting list based on the two lists "zipped" together by pushing the lowest to highest valued elements.

## Problem Statement
Implement Merge algorithm to sort an integer array.

## Input Format

- First line:the size of the array
- Second line:the array to be sorted(numbers seperated by spaces)

## Output Format

- Return shorted array, each number seperated by a coma.

### Sample Input

6</br>
1 9 3 5 4 7

### Sample Output
[1,3,4,5,7,9]
41 changes: 41 additions & 0 deletions Algorithms/Sorting_Algorithms/Radix Sort/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
# Radix Sort

Radix sort is an integer sorting algorithm that sorts data with integer keys by groupin Radix sort uses counting sort as a subroutine to sort an array of numbers.
Because integers can be used to represent strings (by hashing the strings to integers), radix sort works on data types other than just integers.The lower bound of all the comparison based sorting algorithms( Merge sort, Heap sort, Quick sort, ...etc) is Ω(nLogn), i.e.., cannot be better than nLogn.

## The Radix Sort Algorithm
1) Do following for each digit i where i varies from least significant digit to the most significant digit.

- Sort input array using counting sort (or any stable sort) according to the i’th digit.

Let us understand it with the help of an example.

```javascript
Original, unsorted list:

170, 45, 75, 90, 802, 24, 2, 66
Sorting by least significant digit (1s place) gives: [*Notice that we keep 802 before 2, because 802 occurred before 2 in the original list, and similarly for pairs 170 & 90 and 45 & 75.]

170, 90, 802, 2, 24, 45, 75, 66
Sorting by next digit (10s place) gives: [*Notice that 802 again comes before 2 as 802 comes before 2 in the previous list.]

802, 2, 24, 45, 66, 170, 75, 90
Sorting by most significant digit (100s place) gives:

2, 24, 45, 66, 75, 90, 170, 802
```
## The complexity of Radix Sort Technique
- Time Complexity: O(nk)
- Space Complexity: O(n+k)
## Input Output Format
- Input − An array of data, and the total number in the array, digit count of max number

- Output − Sorted array.

## Example

### Input:
The unsorted list: 802 630 20 745 52 300 612 932 78 187
### Output:
Data before Sorting: 802 630 20 745 52 300 612 932 78 187
Data after Sorting: 20 52 78 187 300 612 630 745 802 932
76 changes: 76 additions & 0 deletions Algorithms/Sorting_Algorithms/Radix Sort/Radix Sort.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
// C++ implementation of Radix Sort
#include <iostream>
using namespace std;

// A utility function to get maximum value in arr[]
int getMax(int arr[], int n)
{
int mx = arr[0];
for (int i = 1; i < n; i++)
if (arr[i] > mx)
mx = arr[i];
return mx;
}

// A function to do counting sort of arr[] according to
// the digit represented by exp.
void countSort(int arr[], int n, int exp)
{
int output[n]; // output array
int i, count[10] = { 0 };

// Store count of occurrences in count[]
for (i = 0; i < n; i++)
count[(arr[i] / exp) % 10]++;

// Change count[i] so that count[i] now contains actual
// position of this digit in output[]
for (i = 1; i < 10; i++)
count[i] += count[i - 1];

// Build the output array
for (i = n - 1; i >= 0; i--) {
output[count[(arr[i] / exp) % 10] - 1] = arr[i];
count[(arr[i] / exp) % 10]--;
}

// Copy the output array to arr[], so that arr[] now
// contains sorted numbers according to current digit
for (i = 0; i < n; i++)
arr[i] = output[i];
}

// The main function to that sorts arr[] of size n using
// Radix Sort
void radixsort(int arr[], int n)
{
// Find the maximum number to know number of digits
int m = getMax(arr, n);

// Do counting sort for every digit. Note that instead
// of passing digit number, exp is passed. exp is 10^i
// where i is current digit number
for (int exp = 1; m / exp > 0; exp *= 10)
countSort(arr, n, exp);
}

// A utility function to print an array
void print(int arr[], int n)
{
for (int i = 0; i < n; i++)
cout << arr[i] << " ";
}

int main()
{
int arr[1000];
for(int i=0;i<1000;i++){
cin>>arr[i];
}
int n = sizeof(arr) / sizeof(arr[0]);

// Function Call
radixsort(arr, n);
print(arr, n);
return 0;
}
Loading