Error with sorting code in C - c

I think there was something wrong with the algorithm.
It shows wrong answers.
I changed every line by line, but I didn't get the proper answer.
#include<stdio.h>
void sort(int a[],int n)
{
int gap,i,j,temp;
for(gap=n;gap>0;gap/=2)
{
for(i=gap;i<n;i+=1)
{
temp=a[i];
for(j=i;j>=gap&&a[j]>temp;j=gap)
a[j]=a[j];
a[j]=temp;
}
}
}
int main()
{
int a[20],i,n;
printf("Number of elements:");
scanf("%d",&n);
printf("Enter array elements:\n");
for(i=0;i<n;++i)
scanf("%d",&a[i]);
printf("Array elements before the sort:\n");
for(i=0;i<n;++i)
printf("%d",a[i]);
sort(a,n);
printf("\nAfter sort:\n");
for(i=0;i<n;++i)
printf("%d ",a[i]);
return 0;
}
Please help me with this code.

You're right emely,There were some issues in your algorithm.
In the 6th line the gap should be n/2, not n.
And in the 12th line it should be j-gap.
After correcting those errors,I rewrote the code.
Hope it might help.
Cheers.
#include<stdio.h>
void sort(int a[],int n)
{
int gap,i,j,temp;
for(gap=n/2;gap>0;gap/=2)
{
for(i=gap;i<n;i+=1)
{
temp=a[i];
for(j=i;j>=gap&&a[j-gap]>temp;j-=gap)
a[j]=a[j-gap];
a[j]=temp;
}
}
}
int main()
{
int a[20],i,n;
printf("Number of elements:");
scanf("%d",&n);
printf("Enter array elements:\n");
for(i=0;i<n;++i)
scanf("%d",&a[i]);
printf("Array elements before the sort:\n");
for(i=0;i<n;++i)
printf("%d",a[i]);
sort(a,n);
printf("\nAfter shell sort:\n");
for(i=0;i<n;++i)
printf("%d ",a[i]);
return 0;
}

Related

Function not working on c program selection sort

Why this function is not working here? It's not sorting on output.
Suppose if I enter 1 4 2 the output is always 1 4 2 not 1 2 4.
How can properly implement this selection sort?
Thanks in advance!!
#include <stdio.h>
int selection_sort (int a[],int n, int i, int j,int temp,int min){
for(i=0;i<n;i++)
scanf("%d",&a[i]);
for(i=0;i<n;i++){
min=i;
for(j=i+1;j<n;j++){
if(a[j]<a[min]){
min=j;
}
}
temp=a[i];
a[i]=a[min];
a[min]=temp;
}
}
int main(){
int i, j, n,a[20], temp,min;
printf("How many elements:\n ");
scanf("%d",&n);
printf("Enter array elements:\n");
for(i=0;i<n;i++)
scanf("%d",&a[i]);
printf("Sorted array: ");
for(i=0;i<n;i++)
printf(" %d",a[i]);
return 0;
selection_sort(i,j,n,a,temp,min);
}
There are 3 main issues in your code:
You are getting user input twice. One in main function:
printf("Enter array elements:\n");
for(i=0;i<n;i++)
scanf("%d",&a[i]);
Another in selection_sort function:
for(i=0;i<n;i++)
scanf("%d",&a[i]);
Fix this by removing the one in selection_sort function.
As noted in the comments section, you are returning from main function before calling selection_sort function:
return 0;
Fix this by moving it to the end of main function.
You are printing the expected results from selection_sort function before calling it:
printf("Sorted array: ");
for(i=0;i<n;i++)
printf(" %d",a[i]);
...
selection_sort(i,j,n,a,temp,min);
Fix this by calling selection_sort before printing its results.
Here is the fixed code:
#include <stdio.h>
int selection_sort (int a[], int n, int i, int j, int temp, int min)
{
for(i=0;i<n;i++) {
min=i;
for(j=i+1;j<n;j++) {
if(a[j]<a[min]) {
min=j;
}
}
temp=a[i];
a[i]=a[min];
a[min]=temp;
}
}
int main()
{
int i, j, n,a[20], temp, min;
printf("How many elements: ");
scanf("%d",&n);
printf("Enter array elements: ");
for(i=0;i<n;i++) {
scanf("%d",&a[i]);
}
selection_sort(a, n, i, j, temp, min);
printf("Sorted array:");
for(i=0;i<n;i++) {
printf(" %d", a[i]);
}
printf("\n");
return 0;
}
Here is a test:
$ gcc main.c && ./a.out
How many elements: 3
Enter array elements: 1 4 2
Sorted array: 1 2 4
int main(){
int i, j, n,a[20], temp,min;
printf("How many elements:\n ");
scanf("%d",&n);
printf("Enter array elements:\n");
for(i=0;i<n;i++)
scanf("%d",&a[i]);
selection_sort(a,n,i,temp,min);//Not need to use j,cause j isn't used in your function
for(i=0;i<n;i++)
printf("%d\t",a[i]);//You should call your function before print your data,and also before "return 0"
return 0;
}
AND,"int i,int temp,int min" these don't need to be the parameters.You can just create them in your function

Junk values when using fixed size array whereas if I use dynamically sized array the results are correct

#include<stdio.h>
void readMatrix(int m,int n,int a[][n]);
void displayMatrix(int m,int n,int a[][n]);
void addMatrix(int m,int n,int a[][n],int b[][n]);
int main()
{
int a[5][5],b[5][5],m,n,p,q;
printf("Enter the no. of rows and columns of matrix A\n");
scanf("%d%d",&m,&n);
printf("Enter the no. of rows and columns of matrix B\n");
scanf("%d%d",&p,&q);
if(m!=p||n!=q)
{
printf("Matrix addition not possible\n");
return 0;
}
printf("Enter %d elements to matrix A\n",m*n);
readMatrix(m,n,a);
printf("Enter %d elements to matrix B\n",m*n);
readMatrix(p,q,b);
printf("Matrix A\n");
displayMatrix(m,n,a);
printf("Matrix B\n");
displayMatrix(p,q,b);
addMatrix(m,n,a,b);
}
void readMatrix(int m,int n,int a[m][n])
{
for(int i=0;i<m;i++)
{
for(int j=0;j<n;j++)
{
scanf("%d",&a[i][j]);
}
}
}
void displayMatrix(int m,int n,int a[m][n])
{
for(int i=0;i<m;i++)
{
for(int j=0;j<n;j++)
{
printf("%d ",a[i][j]);
}
printf("\n");
}
}
void addMatrix(int m,int n,int a[m][n],int b[m][n])
{
int c[5][5]; //I guess this is causing problem
//int c[m][n]; //gives correct answer
for(int i=0;i<m;i++)
{
for(int j=0;j<n;j++)
{
c[i][j]=a[i][j]+b[i][j];
}
}
printf("Sum Matrix\n");
/*Gives correct result
for(int i=0;i<m;i++)
{
for(int j=0;j<n;j++)
{
printf("%d ",c[i][j]);
}
printf("\n");
}
*/
displayMatrix(m,n,c);
}
displayMatrix(m,n,c) is correct If i define array as c[m][n], code for it is given above.
It is giving junk value if I use c[5][5].
I'm using gcc compiler. I displayed the sum in addMatrix function itself, which gives correct result. Not able to figure out the mistake. Kindly help. Thank You.

MergeSort giving Segmentation fault

I used this merge sort program to create my own program:
https://gist.github.com/mycodeschool/9678029
my program gives me segmentation fault 11. I can't seem to figure out what's wrong. Please help me.
my program:
#include<stdio.h>
int a[20],n, nl, nr,left[20],right[20];
void merge(int *l, int *r, int *x);
void mergesort(int *x, int v);
void main()
{
int i;
printf("Enter the number of the elements \n");
scanf("%d", &n);
printf("Enter the array elements \n");
for(i=0; i<n; i++)
scanf("%d", &a[i]);
printf("The array before sorting: \n");
for(i=0; i<n; i++)
printf("%d \n", a[i]);
mergesort(a,n);
printf("The array after sorting: \n");
for(i=0; i<n; i++)
printf("%d \n", a[i]);
}
void merge(int *l, int *r, int *x)
{
int i=0, j=0, k=0;
while(i<nl && j<nr)
{
if(l[i]<=r[j])
{
x[k]=l[i];
i++;
}
else
{
x[k]=r[j];
j++;
}
k++;
}
while(i<nl)
{
x[k]=l[i];
i++;
k++;
}
while(j<nr)
{
x[k]=r[j];
j++;
k++;
}
}
void mergesort(int *x, int v)
{
int mid,i;
if(n<2)
return;
mid=n/2;
nl=mid;
nr= n-mid;
for(i=0; i<mid; i++)
left[i]=x[i];
for(i=mid; i<n; i++)
right[i-mid]= x[i];
mergesort(left, mid);
mergesort(right,v-mid);
merge(left, right, a);
}
I would really appreciate if someone helped me. Thanks!
here is an example of a merge sort program, easily found via a google of the web.
Some comparison should show you where your code went wrong.
#include<stdio.h>
void mergesort(int a[],int i,int j);
void merge(int a[],int i1,int j1,int i2,int j2);
int main()
{
int a[30],n,i;
printf("Enter no of elements:");
scanf("%d",&n);
printf("Enter array elements:");
for(i=0;i<n;i++)
scanf("%d",&a[i]);
mergesort(a,0,n-1);
printf("\nSorted array is :");
for(i=0;i<n;i++)
printf("%d ",a[i]);
return 0;
}
void mergesort(int a[],int i,int j)
{
int mid;
if(i<j)
{
mid=(i+j)/2;
mergesort(a,i,mid); //left recursion
mergesort(a,mid+1,j); //right recursion
merge(a,i,mid,mid+1,j); //merging of two sorted sub-arrays
}
}
void merge(int a[],int i1,int j1,int i2,int j2)
{
int temp[50]; //array used for merging
int i,j,k;
i=i1; //beginning of the first list
j=i2; //beginning of the second list
k=0;
while(i<=j1 && j<=j2) //while elements in both lists
{
if(a[i]<a[j])
temp[k++]=a[i++];
else
temp[k++]=a[j++];
}
while(i<=j1) //copy remaining elements of the first list
temp[k++]=a[i++];
while(j<=j2) //copy remaining elements of the second list
temp[k++]=a[j++];
//Transfer elements from temp[] back to a[]
for(i=i1,j=0;i<=j2;i++,j++)
a[i]=temp[j];
}

program to print the position of the smallest number of n numbers using arrays

This code does not give me answer when i enter value 1 in array.
for eg i have taken no. of elements as 5
then i enter them as 2,3,1,6,4
then output gives 2 as smallest number and position number is not always correct
what's the error?
#include<stdio.h>
int main()
{
int n,i,a[10],sum=0;
int small=0,pos=0;
printf("enter no of elements in array:");
scanf("%d",&n);
for(i=0;i<n;i++)
{
printf("a[%d]=",i);
scanf("%d",&a[i]);
}
for(i=1;i<n;i++)
{
small=a[0];
if( a[i] < small)
{
small=a[i];
pos=i;
}
}
printf("smallest no:%d \n",small);
printf("position:%d",pos);
}
"small" variable is overridden with a[0] in each iteration. Just move it outside the loop:
#include<stdio.h>
int main()
{
int n,i,a[10],sum=0;
int small=0,pos=0;
printf("enter no of elements in array:");
scanf("%d",&n);
for(i=0;i<n;i++)
{
printf("a[%d]=",i);
scanf("%d",&a[i]);
}
small=a[0];
for(i=1;i<n;i++)
{
if( a[i] < small)
{
small=a[i];
pos=i;
}
}
printf("smallest no:%d \n",small);
printf("position:%d",pos);
}
You are not that far away - use are initializing small=a[0]; every time whenever loop iterates so just initialize small=a[0]; before loop
Here your corrected code
#include<stdio.h>
int main()
{
int n,i,a[10],sum=0;
int small=0,pos=0;
printf("enter no of elements in array:");
scanf("%d",&n);
for(i=0;i<n;i++)
{
printf("a[%d]=",i);
scanf("%d",&a[i]);
}
small=a[0];
for(i=1;i<n;i++)
{
if( a[i] < small)
{
small=a[i];
pos=i;
}
}
printf("smallest no:%d \n",small);
printf("position:%d",pos);
}
small = a[0] should be before the loop.
You should write small = a[0] before the beginning of a for loop.

Merge sort not showing correct output

I have implemented each line of merge sort algorithm-wise and can't find error.
al and ar are left and right sub arrays. Arrays are passed along with size.
#include<stdio.h>
#include<conio.h>
void mergesort(int a[] ,int);
void merge(int al[],int,int ar[],int,int a[]);
int main()
{
int i,n;
printf("Enter the no of elements to be sorted\n");
scanf("%d",&n);
int a[n];
for(i=0;i<n;i++)
{
scanf("%d",&a[i]);
}
mergesort(a,n);
printf("\nThe elements after sorting are:");
for(i=0;i<n;i++)
{
printf("%d ",a[i]);
}
getch();
return 0;
}
void mergesort(int a[],int size)
{
int i,n=size,mid;
if(n<2)
return;
mid=n/2;
int left[mid],right[n-mid];
for(i=0;i<mid;i++)
left[i]=a[i];
for(i=mid;i<n;i++)
right[i]=a[i];
mergesort(left,mid);
mergesort(right,n-mid);
merge(left,mid,right,n-mid,a);
}
void merge(int al[],int sl,int ar[],int sr,int a[])
{
int i=0,j=0,k=0;
while(i<sl && j<sr)
{
if(al[i]<=ar[j])
{
a[k]=al[i];
i++;
}
else
{
a[k]=ar[j];
j++;
}
k++;
}
while(i<sl)
{
a[k]=al[i];
i++;
k++;
}
while(j<sr)
{
a[k]=ar[j];
j++;
k++;
}
}
input:
no of elements:4
5 6 3 1
output:
5 16 16 16
Look at this part of your code:
int left[mid],right[n-mid];
for(i=0;i<mid;i++)
left[i]=a[i];
for(i=mid;i<n;i++)
right[i]=a[i];
You are accessing the right array with indexes beyond the array bounds. That should be something like this instead:
right[i - mid]=a[i];
I have correct code for merge sort written in C. Please tally with it.
#include<stdio.h>
void mergesort(int a[],int i,int j);
void merge(int a[],int i1,int j1,int i2,int j2);
int main()
{
int a[30],n,i;
printf("Enter no of elements:");
scanf("%d",&n);
printf("Enter array elements:");
for(i=0;i<n;i++)
scanf("%d",&a[i]);
mergesort(a,0,n-1);
printf("\nSorted array is :");
for(i=0;i<n;i++)
printf("%d ",a[i]);
return 0;
}
void mergesort(int a[],int i,int j)
{
int mid;
if(i<j)
{
mid=(i+j)/2;
mergesort(a,i,mid);
mergesort(a,mid+1,j);
merge(a,i,mid,mid+1,j);
}
}
void merge(int a[],int i1,int j1,int i2,int j2)
{
int temp[50];
int i,j,k;
i=i1;
j=i2;
k=0;
while(i<=j1 && j <=j2)
{
if(a[i]<a[j])
temp[k++]=a[i++];
else
temp[k++]=a[j++];
}
while(i<=j1)
temp[k++]=a[i++];
while(j<=j2)
temp[k++]=a[j++];
for(i=i1,j=0;i<=j2;i++,j++)
a[i]=temp[j];
}

Resources