I am trying to create a bucketsort algorithm in C++, but it is not working at all. Every run, it adds many new numbers, often very large, such as in the billions, into the array. Does anyone know why this is? Here is the code - (Note that I am passing in an array of size 100, with random numbers from 0 to ~37000, and that the insertion sort function is fully functional and tested multiple times)
It would be greatly appreciated if someone could point out what's wrong.
void bucketSort(int* n, int k)
{
int c = int(floor(k/10)), s = *n, l = *n;
for(int i = 0; i < k; i++) {
if(s > *(n + i)) s = *(n + i);
else if(l < *(n + i)) l = *(n + i);
}
int bucket[c][k + 1];
for(int i = 0; i < c; i++) {
bucket[i][k] = 0;
}
for(int i = 0; i < k; i++) {
for(int j = 0; j < c; j++) {
if(*(n + i) >= (l - s)*j/c) {
continue;
} else {
bucket[j][bucket[j][k]++] = *(n + i);
break;
}
}
}
for(int i = 0; i < c; i++) {
insertionSort(&bucket[i][0], k);
}
}
This line does not compile. int bucket[c][k + 1];
I think the problem is with you bucket indices. This part here
for(int j = 0; j < c; j++) {
if(*(n + i) >= (l - s)*j/c) {
continue;
} else {
bucket[j][bucket[j][k]++] = *(n + i);
break;
}
}
does not do the equivalent of:
insert n[i] into bucket[ bucketIndexFor( n[i]) ]
First it gets the index off by one. Because of that it also misses the break for the numbers for the last bucket. There is also a small error introduced because the index calculation uses the range [0,l-s] instead of [s,l], which are only the same if s equals 0.
When I write bucketIndex as:
int bucketIndex( int n, int c, int s, int l )
{
for(int j = 1; j <= c; j++) {
if(n > s + (l-s)*j/c) {
continue;
} else {
return j-1;
}
}
return c-1;
}
and rewrite the main part of your algorithm as:
std::vector< std::vector<int> > bucket( c );
for(int i = 0; i < k; i++) {
bucket[ bucketIndex( n[i], c, s, l ) ].push_back( n[i] );
}
I get the items properly inserted into their buckets.
Related
#include <bits/stdc++.h>
using namespace std;
int solve(int A, vector<int> &B) {
vector<int> pre(A);
vector<int> suff(A);
vector<int> p;
vector<int> s;
int sum = 0;
int ans = 0;
for (int i = 0; i < A; i++) {
sum = sum + B[i];
}
if (sum % 3 != 0)
return 0;
sum = sum / 3;
for (int i = 0; i < A; i++) {
if (i == 0) {
pre[i] = B[i];
if (pre[i] == sum) {
p.push_back(i);
}
continue;
}
pre[i] = B[i] + pre[i - 1];
if (pre[i] == sum) {
p.push_back(i);
}
}
for (int i = A - 1; i >= 0; i--) {
if (i == A - 1) {
suff[A - 1] = B[A - 1];
if (suff[i] == sum) {
s.push_back(i);
}
continue;
}
suff[i] = B[i] + pre[i + 1];
if (suff[i] == sum) {
s.push_back(i);
}
}
for (int i = 0; i < p.size(); i++) {
for (int j = s.size(); j >= 0; j++) {
if (s[j] > p[i] + 1) {
ans++;
}
}
}
return ans;
}
int main() {
int A = 5;
vector<int> B = {1, 2, 3, 0, 3};
cout << solve(5, B);
return 0;
}
The code keeps getting dumped at pre[i]=B[i]. The code is very simple for counting the number of ways to split all the elements of the array into 3 contiguous parts so that the sum of elements in each part is the same.
I just made a prefix and suffix sum array.
I've been trying to figure out how to get this code to count all inversions, but just can't seem to put my finger on it. To my understanding this code should count all of the inversions, but it fails many test cases, nudges in the right directions, or even the whole fix would be appreciated.
Code:
int countInversion(vector<int>& v, int& inv) {
if(v.size() > 1) {
vector<int> left(v.begin(), v.begin() + v.size()/2);
vector<int> right(v.begin() + v.size()/2, v.end());
countInversion(left, inv);
countInversion(right, inv);
int l = 0; int r = 0;
for(int i = 0; i < v.size(); i++) {
if(r >= right.size() || (l < left.size() && left[l] < right[r])) {
v[i] = left[l++];
} else {
if(right[r] < left[l]) inv += left.size() - l;
v[i] = right[r++];
}
}
} return inv;
}
The code that did work (I found it on the internet) has very low
readability can someone help me understand it? and more importantly, point to me what my code exactly missed on and if it's possible to rectify my code to count properly? Thank you!
Code 2:
long long ans = 0;
void mergei(int a[],int i,int j) {
int ni = ((i+j)/2) + 1, nj = j + 1;
int s = i;
int* arr = new int [j - i + 1];
j = ni;
int k = 0;
while(i < ni && j < nj) {
if(a[i] <= a[j]) {
arr[k] = a[i++];
} else {
arr[k] = a[j++];
ans += (ni - i);
} k++;
}
for(; i < ni; i++, k++) arr[k] = a[i];
for(; j < nj; j++, k++) arr[k] = a[j];
for(k = 0; s < nj; s++, k++) a[s] = arr[k];
delete [] arr;
}
void m_sort(int a[],int i,int j) {
if(i < j) {
m_sort(a, i, (i+j)/2);
m_sort(a, ((i+j)/2) + 1, j);
mergei(a, i, j);
}
}
I finally found a way to solve it using my code, I was sure some changes to the if else block or how I added the inversions would fix it, it just had to, that's the whole point of mergesort, that's why I was adamant about solving it this way, instead of just using another code. The issue was; I was doing left[l] < right[r] which doesn't matter for merging if it's left or right, but does for inversions, just had to change it to left[l] <= right[r].
long countInversions(vector<int>& arr, long& inv) {
if(arr.size() > 1) {
vector<int> left(arr.begin(), arr.begin() + arr.size()/2);
vector<int> right(arr.begin() + arr.size()/2, arr.end());
countInversions(left, inv);
countInversions(right, inv);
int l = 0; int r = 0;
for(int i = 0; i < arr.size(); i++) {
if(r >= right.size() || (l < left.size() && left[l] <= right[r])) {
arr[i] = left[l++];
// inv += r;
} else {
arr[i] = right[r++];
inv += left.size() - l;
}
}
} return inv;
}
A slightly better and more readable code would be:
long countInversions(vector<int>& v) {
if(v.size() <= 1) return 0;
vector<int> left(v.begin(), v.begin() + v.size()/2);
vector<int> right(v.begin() + v.size()/2, v.end());
long inv = countInversions(left) + countInversions(right);
int l = 0, r = 0;
for(int i = 0; i < v.size(); i++) {
if(r >= right.size() || (l < left.size() && left[l] <= right[r])) {
v[i] = left[l++];
// inv += r;
} else {
v[i] = right[r++];
inv += left.size() - l;
}
}
return inv;
}
I need to be able to track the number of exchanges and comparisons in this selection sort algorithm. The algorithm sorts the array just fine. I need to modify it in order to have tracked the number of exchanges.
void insertion_sort(int * theArray, int size)
{
int tmp;
for (int i = 1; i < size; i++)
{
for (int j = i; j > 0 && theArray[j - 1] > theArray[j]; j--)
{
tmp = theArray[j];
theArray[j] = theArray[j - 1];
theArray[j - 1] = tmp;
}
}
}
Here are the two helper functions
bool inOrder(int i, int j) {
numComparisons++;
return i <= j;
}
void swapElement(int & i, int & j) {
int t = i;
i = j;
j = t;
numSwaps++;
}
Something like that ?
void insertion_sort(int * theArray, int size)
{
int tmp;
int count = 0;
for (int i = 1; i < size; i++)
{
for (int j = i; j > 0 && theArray[j - 1] > theArray[j]; j--)
{
tmp = theArray[j];
theArray[j] = theArray[j - 1];
theArray[j - 1] = tmp;
count++; // increment on each loop/exchanges
}
}
}
This is for an assignment in an algorithm class. I understand and agree that using a vector would simplify things, but that isn't an option.
The code for the Mergesort / merge algorithm can't be modified either.
I need to run the merge sort as follows:
starting from 100 all the way to 1000, increments of 100. For each increment I run it 5 times, for each of these times I run it 1000 times.
That being said - everything works fine until my loop reaches 700 and crashes with the error: "Unhandled exception at 0x75612F71 in msdebug.exe: Microsoft C++ exception: std::bad_alloc at memory location 0x010672F4."
Here is my code:
int const size = 6;
int const size2 = 1001;
int const times = 6;
int const interval = 11;
void merge(int arr[], int p, int q, int r)
{
int n1 = q - p + 1;
int n2 = r - q;
int * L = new int[n1 + 1];
int * R = new int[n2 + 1]; // line giving the error after 700
for (int i = 1; i <= n1; i++)
{
L[i] = arr[p + i - 1];
}
for (int j = 1; j <= n2; j++)
{
R[j] = arr[q + j];
}
L[n1 + 1] = 32768;
R[n2 + 1] = 32768;
int i, j;
i = j = 1;
for (int k = p; k <= r; k++)
{
if (L[i] <= R[j])
{
arr[k] = L[i];
i++;
}
else
{
arr[k] = R[j];
j++;
}
}
}
void mergeSort(int arr[], int p, int r)
{
int q;
if (p < r)
{
q = ((p + r) / 2);
mergeSort(arr, p, q);
mergeSort(arr, (q + 1), r);
merge(arr, p, q, r);
}
}
void copyArray(int original[][size2], int copy[], int row, int finish)
{
int i = 1;
while (i <= finish)
{
copy[i] = original[row][i];
i++;
}
}
void copyOneD(int orig[], int cop[])
{
for (int i = 1; i < size2; i++)
{
cop[i] = orig[i];
}
}
int main()
{
struct timeval;
clock_t start, end;
srand(time(NULL));
int arr[size][size2];
int arr2[size2];
int arrCopy[size2];
double tMergeSort[times][interval];
double avgTmergeSort[11];
/*for (int i = 1; i < (size2); i++)
{
arr2[i] = rand();
}*/
for (int i = 1; i < size; i++)
{
for (int j = 1; j < size2; j++)
{
arr[i][j] = rand();
}
}
for (int x = 100; x <= 1000; x = x + 100) //This loop crashes >=700
{
for (int r = 1; r <= 5; r++)
{
copyArray(arr, arr2, r, 1001);
for (int k = 0; k < 1000; k++)
{
copyOneD(arr2, arrCopy);
mergeSort(arrCopy, 1, x);
}
}
}
return 0;
}
You can ignore the code and the arrays. Those functions work fine.
Everything works fine until I set 'x <= 700' or higher and then it crashes.
I had a theory that maybe the computer runs out of memory for the pointers in the merge algorithm but when I tried to use delete it also crashed.
Any help is appreciated and suggestions as well.
Thanks
The problem is to print all subsets that sum up to a value. I wrote code to check if there is a possible subset. Can some one gimme an idea to print the numbers that form the sum. Below is my code. Assume the array contains only +ve nos for simplicity.
void subsetsum(int A[], int target) {
int N = sizeof(A)/sizeof(int), sum = 0;
for(int i = 0; i < N; i++) sum += A[i];
vector<bool> V(sum + 1, 0);
V[0] = 1;
for(int i = 0; i < N; i++)
for(int j = sum; j >= 0; j--) {
if(j + A[i] <= sum && V[j]) V[A[i] + j] = 1;
}
if(V[target]) cout << "Sumbset sum exists" << endl;
else cout << "Sumbset sum doesnt exist" << endl;
}
First you need to generate all the subsets
If [a,b,c,d] is given array, think about generating subsets taking each element from array one at a time.
subsets(X) including y = foreach x in X append y to x
Taking a, we get subsets(a) = { [], [a] }
Take b, we get subsets(a,b) = subsets(a) + (subsets(a) including b)
= { [], [a] } + { [b], [a,b] } = { [], [a], [b], [a,b] }
Take c, subsets(a,b,c) = subsets(a,b) + (subsets(a,b) including c)
= {[], [a],[b],[a,b]} + {[c], [a,c], [b,c], [a,b,c]}
Once you get all subsets, print those whose sums equals target. You can modify the above algo further if you don't need any subsets.
Here's an answer in javascript:
function subsetsum(A, target) {
//int N = sizeof(A)/sizeof(int), sum = 0;
var N = A.length, sum = 0;
//for(int i = 0; i < N; i++) sum += A[i];
for(var i = 0; i < N; i++) sum += A[i];
// vector<bool> V(sum + 1, 0);
var V = [];
V[0] = [];
for(var i = 0; i < N; i++) {
for(var j = sum; j >= 0; j--) {
if(j + A[i] <= sum && V[j]) {
//Join the subset of the memoized result to this result.
V[A[i] + j] = [A[i]].concat(V[j]);
}
}
}
console.log(V);
//evaluates to true if V[target] exists
return !!V[target];
}
function to find power set of a vector<int>
vector<vector<int>> power_set(const vector<int>& nums) {
if (nums.empty()) { return { {} }; }
auto set = power_set(vector<int>(begin(nums) +1, end(nums)));
auto tmp = set;
for (auto& p : tmp) {
p.push_back(nums[0]);
}
set.insert(end(set), begin(tmp), end(tmp));
return set;
}
function that return all sets in the power set that sum to target
vector<vector<int>> test_sum(const vector<vector<int>>& ps, int target) {
vector<vector<int>> v;
for (auto& p : ps) {
int sum = accumulate(begin(p), end(p), 0);
if (sum == target) {
v.push_back(p);
}
}
return v;
}
I modified your code to print the numbers.
void subsetsum(int A[], int target) {
int N = sizeof(A) / sizeof(int), sum = 0;
for (int i = 0; i < N; i++) sum += A[i];
vector<bool> V(sum + 1, 0);
V[0] = 1;
for (int i = 0; i < N; i++)
for (int j = sum; j >= 0; j--) {
if (j + A[i] <= sum && V[j]) V[A[i] + j] = 1;
}
if (V[target]) cout << "Sumbset sum exists" << endl;
else cout << "Sumbset sum doesnt exist" << endl;
if (V[target])
{
for (int i = N - 1; i >= 0; i--)
{
if (V[target - A[i]] == 1) printf("%d, ", A[i]), target -= A[i];
}
printf("\n");
}
}
or Here's my version with vector
bool subsetsum_dp(vector<int>& v, int sum)
{
int n = v.size();
const int MAX_ELEMENT = 100;
const int MAX_ELEMENT_VALUE = 1000;
static int dp[MAX_ELEMENT*MAX_ELEMENT_VALUE + 1]; memset(dp, 0, sizeof(dp));
dp[0] = 1;
for (int i = 0; i < n; i++)
{
for (int j = MAX_ELEMENT*MAX_ELEMENT_VALUE; j >= 0; j--)
{
if (j - v[i] < 0) continue;
if (dp[j - v[i]]) dp[j] = 1;
}
}
if (dp[sum])
{
for (int i = n - 1; i >= 0; i--)
{
if (dp[sum - v[i]] == 1) printf("%d, ", v[i]), sum -= v[i];
}
printf("\n");
}
return dp[sum] ? true : false;
}