-
Notifications
You must be signed in to change notification settings - Fork 302
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge branch 'master' into insertion_sort_java
- Loading branch information
Showing
11 changed files
with
768 additions
and
9 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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() |
Oops, something went wrong.
d304b4c
There was a problem hiding this comment.
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.
Until GitMate provides an online UI to show a better overview, you can run coala locally for more details.