Skip to content

Commit

Permalink
Merge branch 'master' into insertion_sort_java
Browse files Browse the repository at this point in the history
  • Loading branch information
salif-04 authored Oct 15, 2018
2 parents dc4f609 + 8939a6f commit d304b4c
Show file tree
Hide file tree
Showing 11 changed files with 768 additions and 9 deletions.
16 changes: 9 additions & 7 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

[![Gitter](https://badges.gitter.im/NITSkmOS/algo.svg)](https://gitter.im/NITSkmOS/algo?utm_source=badge&utm_medium=badge&utm_campaign=pr-badge)
[![NITSkmOS Algorithms](https://img.shields.io/badge/NITSkmOS-Algorithms-blue.svg)](https://github.com/NITSkmOS/Algorithms)
[![Codacy Badge](https://api.codacy.com/project/badge/Grade/29024673e94b48459809432cdd280e39)](https://app.codacy.com/app/NITSkmOS/Algorithms?utm_source=github.com&utm_medium=referral&utm_content=NITSkmOS/Algorithms&utm_campaign=Badge_Grade_Settings)
[![Build Status](https://travis-ci.com/NITSkmOS/Algorithms.svg?branch=master)](https://travis-ci.com/NITSkmOS/Algorithms)
[![GitHub](https://img.shields.io/github/license/mashape/apistatus.svg)](https://github.com/NITSkmOS/Algorithms/blob/master/LICENSE)


Expand All @@ -13,24 +13,26 @@ This repository contains examples of various algorithms written on different pro
| Algorithm | C | CPP | Java | Python |
|:----------------------------------------------------------------------------------------------- |:-------------------------------------:|:-------------------------------------:|:-------------------------------------:|:-------------------------------------:|
| [Euclidean GCD](https://en.wikipedia.org/wiki/Euclidean_algorithm) | [:octocat:](euclidean_gcd/C) | | [:octocat:](euclidean_gcd/Java) | [:octocat:](euclidean_gcd/Python) |
| [QuickSort](https://en.wikipedia.org/wiki/Quicksort) | | | | [:octocat:](quicksort/Python) |
| [Merge Sort](https://en.wikipedia.org/wiki/Merge_sort) | | | | [:octocat:](merge_sort/Python) |
| [Merge Sort](https://en.wikipedia.org/wiki/Merge_sort) | [:octocat:](merge_sort/C) | | | [:octocat:](merge_sort/Python) |
| [QuickSort](https://en.wikipedia.org/wiki/Quicksort) | [:octocat:](quicksort/C) | | | [:octocat:](quicksort/Python) |
| [Insertion Sort](https://en.wikipedia.org/wiki/Insertion_sort) | [:octocat:](insertion_sort/C) | [:octocat:](insertion_sort/Cpp) | [:octocat:](insertion_sort/Java) | [:octocat:](insertion_sort/Python) |
| [Counting Sort](https://en.wikipedia.org/wiki/Counting_sort) | | | | [:octocat:](counting_sort/Python) |
| [Radix Sort](https://en.wikipedia.org/wiki/Radix_sort) | | | | [:octocat:](radix_sort/Python) |
| [Counting Sort](https://en.wikipedia.org/wiki/Counting_sort) |[:octocat:](counting_sort/C) | [:octocat:](counting_sort/Cpp) | | [:octocat:](counting_sort/Python) |
| [Radix Sort](https://en.wikipedia.org/wiki/Radix_sort) | | [:octocat:](radix_sort/Cpp) | | [:octocat:](radix_sort/Python) |
| [Binary Search](https://en.wikipedia.org/wiki/Binary_search_algorithm) | | [:octocat:](binary_search/Cpp) | | [:octocat:](binary_search/Python) |
| [Bubble Sort](https://en.wikipedia.org/wiki/Bubble_sort) | [:octocat:](bubble_sort/C) | [:octocat:](bubble_sort/Cpp) | | |
| [Shell Sort](https://en.wikipedia.org/wiki/Shellsort) | [:octocat:](shell_sort/C) | | | [:octocat:](shell_sort/Python) |
| [Heap Sort](https://en.wikipedia.org/wiki/Heapsort) | | | | [:octocat:](heap_sort/python) |
| [Maximum Subarray Problem](https://en.wikipedia.org/wiki/Maximum_subarray_problem) | | | | [:octocat:](/maximum_subarray/Python)|
| [Knapsack Problem](https://en.wikipedia.org/wiki/Knapsack_problem) | | | | [:octocat:](knapsack_problem/Python)|


## Implemented Data Structures

| Data Structure | C | CPP | Java | Python |
|:----------------------------------------------------------------------------------------------- |:-------------------------------------:|:-------------------------------------:|:-------------------------------------:|:-------------------------------------:|
| [Queue](https://en.wikipedia.org/wiki/Queue_(abstract_data_type)) | | [:octocat:](queue/Cpp) | | |
| [Stack](https://en.wikipedia.org/wiki/Stack_(abstract_data_type)) | | | [:octocat:](stack/Java) | [:octocat:](stack/Python) |
| [Linear Linked List](https://en.wikipedia.org/wiki/Linked_list) | [:octocat:](linked_list/C) | | | [:octocat:](linked_list/Python) |
| [Stack](https://en.wikipedia.org/wiki/Stack_(abstract_data_type)) | [:octocat:](stack/C ) | | [:octocat:](stack/Java) | [:octocat:](stack/Python) |
| [Linear Linked List](https://en.wikipedia.org/wiki/Linked_list) | [:octocat:](linked_list/C) | [:octocat:](linked_list/Cpp) | | [:octocat:](linked_list/Python) |
| [AVL Tree](https://en.wikipedia.org/wiki/AVL_tree) | [:octocat:](avl_tree/C) | | [:octocat:](avl_tree/Java) | [:octocat:](avl_tree/Python) |


Expand Down
73 changes: 73 additions & 0 deletions counting_sort/C/counting_sort.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
#include <stdio.h>
#include <stdlib.h>

/*
Counting sort implementation for sorting given array.
Note - Works on Non-negative integer array only.
Parameters:
int a[] - Integer array a to be sorted.
int n - size of array a.
int r. - Range of numbers in a.
*/

void counting_sort(int a[], int n, int r) {
if(a == NULL) return;

//array to store count of each occurences
int *cnt = (int *) malloc((r+1)*sizeof(int)); // Ignore CPPLintBear
//Out array to store sorted values
int *out = (int *) malloc((n)*sizeof(int)); // Ignore CPPLintBear


// Intitialise the count array to 0
for (int i = 0; i <= r; ++i)
cnt[i] = 0;

// Count each occurences digit wise
for (int i = 0; i < n; ++i)
cnt[a[i]]++;

// Cumulative count array
for (int i = 1; i <= r; ++i)
cnt[i]+=cnt[i-1];

// Sort
for (int i = n - 1; i >= 0; i--) {
// Note - starting from n-1 to maintain "stable sort" property
out[cnt[a[i]] - 1] = a[i];
cnt[a[i]]--;
}

// Copy results back to orignal array
for (int i = 0; i < n; i++)
a[i] = out[i];
}


int main() {
int a[] = {1, 2, 9, 293, 41, 15, 52, 3, 121, 7, 1223, 3449, 15, 1 };
int n = sizeof(a)/sizeof(a[0]);

// Get the max element for calculating range of number
int max_a = a[0];
for (int i = 1; i < n; ++i)
if(max_a < a[i]) max_a = a[i];


printf("Input Array:\n");
for (int i = 0; i < n; i++) {
printf("%d ", a[i]);
}
printf("\n");

counting_sort(a, n, max_a);

printf("Sorted Array:\n");
for (int i = 0; i < n; i++) {
printf("%d ", a[i]);
}
printf("\n");

return 0;
}
73 changes: 73 additions & 0 deletions counting_sort/Cpp/counting_sort.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
#include <iostream>
#include <algorithm>
#include <cmath>
using namespace std;

/*
Counting sort implementation for sorting given array.
Note - Works on Non-negative integer array only.
Parameters:
int a[] - Integer array a to be sorted.
int n - size of array a.
int r. - Range of numbers in a.
*/

void counting_sort(int a[], int n, int r) {
if(a == NULL) return;

int *cnt = new int[r+1]; //array to store count of each occurences
int *out = new int[n]; //Out array to store sorted values


// Intitialise the count array to 0
for (int i = 0; i <= r; ++i)
cnt[i] = 0;

// Count each occurences digit wise
for (int i = 0; i < n; ++i)
cnt[a[i]]++;

// Cumulative count array
for (int i = 1; i <= r; ++i)
cnt[i]+=cnt[i-1];

// Sort
for (int i = n - 1; i >= 0; i--) {
// Note - starting from n-1 to maintain "stable sort" property
out[cnt[a[i]] - 1] = a[i];
cnt[a[i]]--;
}

// Copy results back to orignal array
for (int i = 0; i < n; i++)
a[i] = out[i];
}


int main() {
ios_base::sync_with_stdio(false); //For faster io
int a[] = {1, 2, 9, 293, 41, 15, 52, 3, 121, 7, 1223, 3449, 15, 1 };
int n = sizeof(a)/sizeof(a[0]);

// Get the iterator to max element for calculating range of number
int * max_a_itr = max_element(a, a+n);
int max_a = *max_a_itr;


cout << "Input Array:\n";
for (int i = 0; i < n; i++) {
cout << a[i] << " ";
}
cout << "\n";

counting_sort(a, n, max_a);

cout << "Sorted Array:\n";
for (int i = 0; i < n; i++) {
cout << a[i] << " ";
}
cout << "\n";

return 0;
}
60 changes: 60 additions & 0 deletions knapsack_problem/Python/knapsack.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
def knapsack(profit, weight, capacity):
"""
Here Knapsack problem has been implemented using Greedy Approach
where the weight and and corresponding profit of some items has been
given. And also the maximum capacity of a sack(bag) is given. we have
to take items so that capacity does not exceed and we get maximum profit.
For more information visit: <https://en.wikipedia.org/wiki/Knapsack_problem>
:param profit: array of profit of the items
:param weight: array of weight of the items
:param capacity: capacity of the sack
:return: maximum profit and the fraction of items
"""

# array of profit/weight ratio
ratio = [v / w for v, w in zip(profit, weight)]

# a list of (0, 1, ..., n-1)
index = list(range(len(profit)))

# index is sorted according to ratio in descending order
index.sort(key=lambda i: ratio[i], reverse=True)

# max_profit is the maximum profit gained
max_profit = 0

# fraction is the fraction in which items should be taken
fraction = [0] * len(profit)

for i in index:
if weight[i] <= capacity:
fraction[i] = 1
max_profit += profit[i]
capacity -= weight[i]
else:
fraction[i] = capacity / weight[i]
max_profit += profit[i] * fraction[i]
break

return max_profit, fraction


def main():
# profit is array of profit of the items
# weight is array of weight of the items
# capacity is capacity of the sack

profit = [50, 60, 80]
weight = [10, 30, 20]
capacity = 50

# max_profit is the maximum profit gained
# fraction is the fraction in which items should be taken
max_profit, fraction = knapsack(profit, weight, capacity)
print('Maximum profit:', max_profit)
print('Items should be taken in fraction of:', fraction)


if __name__ == '__main__':
main()
Loading

1 comment on commit d304b4c

@sangamcse
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Comment on d304b4c.

There are 28 results for the section all.pyjava. They have been shortened and will not be shown inline because they are more than 10.

Message File Line
Line contains following spacing inconsistencies: - Tabs used instead of spaces. insertion_sort/Java/insertion_sort.java 3
Line contains following spacing inconsistencies: - Tabs used instead of spaces. insertion_sort/Java/insertion_sort.java 4
Line contains following spacing inconsistencies: - Tabs used instead of spaces. insertion_sort/Java/insertion_sort.java 5
Line contains following spacing inconsistencies: - Tabs used instead of spaces. insertion_sort/Java/insertion_sort.java 6
Line contains following spacing inconsistencies: - Tabs used instead of spaces. insertion_sort/Java/insertion_sort.java 7
Line contains following spacing inconsistencies: - Tabs used instead of spaces. insertion_sort/Java/insertion_sort.java 8
Line contains following spacing inconsistencies: - Tabs used instead of spaces. insertion_sort/Java/insertion_sort.java 9
Line contains following spacing inconsistencies: - Tabs used instead of spaces. insertion_sort/Java/insertion_sort.java 10
Line contains following spacing inconsistencies: - Tabs used instead of spaces. insertion_sort/Java/insertion_sort.java 11
Line contains following spacing inconsistencies: - Tabs used instead of spaces. insertion_sort/Java/insertion_sort.java 12
Line contains following spacing inconsistencies: - Tabs used instead of spaces. insertion_sort/Java/insertion_sort.java 13
Line contains following spacing inconsistencies: - Tabs used instead of spaces. insertion_sort/Java/insertion_sort.java 14
Line contains following spacing inconsistencies: - Tabs used instead of spaces. insertion_sort/Java/insertion_sort.java 15
Line contains following spacing inconsistencies: - Tabs used instead of spaces. insertion_sort/Java/insertion_sort.java 16
Line contains following spacing inconsistencies: - Tabs used instead of spaces. insertion_sort/Java/insertion_sort.java 17
Line contains following spacing inconsistencies: - Tabs used instead of spaces. insertion_sort/Java/insertion_sort.java 18
Line contains following spacing inconsistencies: - Tabs used instead of spaces. insertion_sort/Java/insertion_sort.java 19
Line contains following spacing inconsistencies: - Tabs used instead of spaces. insertion_sort/Java/insertion_sort.java 20
Line contains following spacing inconsistencies: - Tabs used instead of spaces. insertion_sort/Java/insertion_sort.java 21
Line contains following spacing inconsistencies: - Tabs used instead of spaces. insertion_sort/Java/insertion_sort.java 22
Line contains following spacing inconsistencies: - Tabs used instead of spaces. insertion_sort/Java/insertion_sort.java 23
Line contains following spacing inconsistencies: - Tabs used instead of spaces. insertion_sort/Java/insertion_sort.java 24
Line contains following spacing inconsistencies: - Tabs used instead of spaces. insertion_sort/Java/insertion_sort.java 25
Line contains following spacing inconsistencies: - Tabs used instead of spaces. insertion_sort/Java/insertion_sort.java 26
Line contains following spacing inconsistencies: - Trailing whitespaces. - Tabs used instead of spaces. insertion_sort/Java/insertion_sort.java 28
Line contains following spacing inconsistencies: - Tabs used instead of spaces. insertion_sort/Java/insertion_sort.java 29
Line contains following spacing inconsistencies: - Tabs used instead of spaces. insertion_sort/Java/insertion_sort.java 30
Line contains following spacing inconsistencies: - No newline at EOF. insertion_sort/Java/insertion_sort.java 31

Until GitMate provides an online UI to show a better overview, you can run coala locally for more details.

Please sign in to comment.