A bug in merge sort [closed] - c

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 2 years ago.
Improve this question
Can you guys please help me with this code. I know this is basic but I am now really obsessed with it. I am trying to implement a very simple version of mergesort, but the output is all wrong. Please try to fix this code instead of writing a completely new one.
#include <stdio.h>
void merge_them(int a[], int l, int m, int r)
{
int i=l, j=m+1;
int final[r-l+1];
int p=0;
for(int k=0; k<=r-l+1; k++)
{
if(a[i]<a[j])
{
final[p]=a[i];
i++;
p++;
}
else if(a[i]>=a[j])
{
final[p]=a[j];
j++;
p++;
}
}
j=0;
for( i=l; i<r+1; i++)
{
a[i]=final[j];
j++;
}
}
void merge(int a[], int l, int r)
{
if(l<r)
{
int m = (l+r)/2;
merge(a,l,m);
merge(a,m+1,r);
merge_them(a,l,m,r);
}
}
int main()
{
int a[10] = {10,9,8,7,6,5,4,3,2,1};
merge(a,0,9);
for(int i=0; i<10; i++)
printf("%d ",a[i]);
}
UPDATE: This was not a homework. I was just revisiting some sorting algos based on recursion and tried to implement Merge sort. Anyway, I switched to while loop and used a more precise condition check, which led to correct sorted outputs.

There are actually three bugs:
The condition k<=r-l+1 in the for statement is wrong and it will have it do one more loop. It should be k<r-l+1 (< should be used instead of <=) like the condition i<r+1 used later.
The condition a[i]<a[j] is wrong because it isn't checking if the parts are used up. It should be j > r || (i <= m && a[i]<a[j]).
if(a[i]>=a[j]) should be removed because if you don't take an element from one part, you will obviously take from another part.

Related

How can I improve the following code's performance? [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
This question does not appear to be about programming within the scope defined in the help center.
Closed 3 years ago.
Improve this question
Here is the Question.
Find the largest GCD of the input variable with the values of an array.
Inputs are as follows:-
First-line contains two integers, N and Q.
Second-line contains N integers which form the arr[].
Next, Q lines contain an integer M, the time in seconds she wishes to go back.
#include <stdio.h>
int main(){
int n,q;
scanf("%d %d", &n,&q);
int a[n];
int gcdn[n];
for (int i = 0;i < n; i++) {
scanf("%d",&a[i]); }
while (q>0){
q--;
int x;
scanf("%d",&x);
for(int i=0;i<n;i++) {
gcdn[i] = gcd(a[i],x);
}
printf("%d \n",max(gcdn,n));
}
}
int gcd (int a, int b) {
if (b==0)
return a;
else
return gcd(b, a%b);
}
int max(int *a,int n) {
int max= a[0];
for(int i = 1;i<n;i++) {
if(a[i]>min) {
max= a[i];
}
}
return max;
}
How can I make this code better in terms of competitive programming? And what other languages can make this code better and more efficient?
The most obvious performance improvement is that your array is not sorted: instead of simply adding an element to the array, you'll need to do a search first (which is O(\log(n))), but once you need to get the maximum of an array, you just need the first (or the last) entry.

Sorting an array - C language [closed]

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 5 years ago.
Improve this question
I have tried to write a sorting function sort(int *buffer, int array[], int size) which works in a way similar to the insertion sort - it takes the first element from the array, sets it as the first element of the buffer and then checks whether or not the next value showing up in the array is greater than the last value stored in the buffer. If yes, it keeps swapping the two elements until everything is in its place. This is my minimal working example:
#include <stdio.h>
void sort(int *buffer, int array[], int size) {
for(int i = 0; i < size; i++) {
buffer[i] = array[i];
while(i >= 1 && buffer[i] < buffer[i-1]) {
int tmp = buffer[i-1];
buffer[i-1] = buffer[i];
buffer[i] = tmp;
printf("i = %d i: %d, i -1 : %d \n",i, buffer[i], buffer[i-1]);
i--;
}
}
}
int main(void) {
int array[3] = {4,3,2};
int buffer[3];
sort(buffer, array, 3);
for(int i = 0; i < 3; i++) {
printf("%d", buffer[i]);
}
}
However, the output of this program is 222
To be honest, I don't see how it's even possible that three identical elements got placed in the buffer.
What can have gone wrong?
You are using the same variable for the inner while cycle and for the outer for loop. Use a different variable and copy the value of i to it in each iteration of the for.

Hackerrank challenge timout [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 6 years ago.
Improve this question
hey i am just trying to solve a challenge on hackerrank but in some test cases the code timeouts and i don't know why. This is the challenge.
And here is my code:
#include <math.h>
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <assert.h>
#include <limits.h>
#include <stdbool.h>
int main(){
int n, k, q;
scanf("%d %d %d",&n,&k,&q);
int qs[q];
int a[n];
for(int i = 0; i < n; i++){
scanf("%d", &a[i]);
}
for(int i = 0; i < q; i++){
scanf("%d",&qs[i]);
}
int lastNbr = a[n-1];
for(int i = 0; i < k; i++){
lastNbr = a[n-1];
for(int j = n - 1; j > -1; j--){
a[j] = (j-1 >= 0) ? a[j-1] : lastNbr;
}
}
for(int i = 0; i < q; i++){
printf("%d\n", a[qs[i]]);
}
return 0;
}
All right, lets start with analysing your algorithm's time complexity:
You have the 2 nested for loops which always go for n * k operations, as you rotate your array k times and you need n operations to make the rotation.
This goes as O(n * k), so it's about 10^10 operations on the worst case input, which is quite too much for this task.
Please read this article first about how to calculate an algorithm's complexity, as it provides very useful information and it's explained with practical examples.
Now, you have to rethink your algorithm and get a better time complexity. I won't spoil the solution to you, but I can give you a hint: Think that you don't really need to rotate your array k times by 1 unit, you can instead do it only once by k units.
Hope this helps, good luck in solving the challenge! :)
If you look at your code it is o (n*k) solution. You can solve it in o (n) time. And input size is 10 pow 5 that's why you are getting time out error. % will help you out here.

C Programm output completely changed due to unrelated, empty printf statement [closed]

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 6 years ago.
Improve this question
I have this C program code here, which determines the highest contiguous value for an array:
#include <stdio.h>
int inputs[8];
int calcSum(int i, int j);
int main(void) {
int i, j, maxSum = 0, tempSum = 0;
int length = sizeof(inputs)/sizeof(inputs[0]);
for(i=0;i<length;i++) {
scanf("%d", &inputs[i]);
}
for(i=0;i<length;i++) {
for(j=i;j<length;j++) {
tempSum = calcSum(i,j);
if(tempSum > maxSum) {
maxSum = tempSum;
}
}
}
printf("%d\n", maxSum);
return 0;
}
int calcSum(int i, int j) {
int c, sum;
for(c=i;c<=j;c++) {
sum+=inputs[c];
}
return sum;
}
Even though this code looks correct to me, it outputs a wrong result. Sampling adding any sort of printf("") (can be empty as well) between tempSum = ... and if(tempSum >...) will make the code output the correct answer for all test cases. I even rewrote the entire code from scratch and still get the same issue.
For example, the number series: 5 2 -1 -2 -4 3 5 -6 should output 8, which it does once the printf("") is added, otherwise it outputs 38...and I have no idea why. Can you please explain, where I went wrong?
In this code:
int calcSum(int i, int j) {
int c, sum;
for(c=i;c<=j;c++) {
sum+=inputs[c];
}
return sum;
}
You need to make sure you initialize sum to a starting value:
int calcSum(int i, int j) {
int c, sum;
sum=0;
for(c=i;c<=j;c++) {
sum+=inputs[c];
}
return sum;
}

What is the reason of this Segmentation fault for PRIME1 on Spoj? [closed]

Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 7 years ago.
Improve this question
I'm trying to solve the PRIME1 problem on Spoj. I implemented this code using segmented sieve of Eratosthenes
#include <stdio.h>
long int primes[100000];
int main(void) {
long int t, m, n, i, j, p;
scanf("%ld",&t);
while(t--) {
scanf("%ld",&m);
scanf("%ld",&n);
for(i=2; i*i<=n; i++) {
p=m/i;
p=p*i;
for(j=p; j<=n; j+=i) {
if(j!=i)
primes[j-m] = 1;
}
}
for(i=0; i<(n-m+1); i++) {
if(primes[i] == 0)
printf("%ld ",i+m);
else
continue;
}
printf("\n");
}
return 0;
}
Its works fine when value of n-m is not very high. But if the value of n-m gets very high (i.e. near about 100000), it gives Segmentation fault. Why there is such kind of behaviour?
Also, when I declared the primes array inside main, the code works correctly.
#include <stdio.h>
int main(void) {
long int t, m, n, i, j, p;
scanf("%ld",&t);
while(t--) {
scanf("%ld",&m);
scanf("%ld",&n);
if(m==1) m=2;
long int primes[n-m+1];
for(i=0;i<n-m+1;i++)
primes[i]=0;
for(i=2; i*i<=n; i++) {
p=m/i;
p=p*i;
for(j=p; j<=n; j+=i) {
if(j!=i)
primes[j-m] = 1;
}
}
for(i=0; i<(n-m+1); i++) {
if(primes[i] == 0)
printf("%ld ",i+m);
else
continue;
}
printf("\n");
}
return 0;
}
Why the program gives Segmentation fault when I declare the primes array globally?
In the first you are declaring primes to have a fixed size of 100000 so if you ever try to access an element outside that range you'll be accessing memory you should not, thus a seg fault.
In the second example you declare it to be size n-m+1 which will make sure it's always big enough to do what you want.

Resources