1. This site uses cookies. By continuing to use this site, you are agreeing to our use of cookies. Learn More.

Bubble sorting

Discussion in 'Meet and Greet' started by RajeshGalla, Oct 19, 2011.

  1. RajeshGalla

    RajeshGalla New Member

    Joined:
    Oct 19, 2011
    Messages:
    1
    Likes Received:
    0
    Trophy Points:
    0
    I have to take the number of integers and the integers to be sorted, as input through console during execution of program and the integers will be saved into a pointerd.Then i will give the pointer as an arguement for the function that will bubble sort those integers.I am getting problem in function "bubbleSorting(int n,int *p)".Please someone help me
    Code:
    #include<stdio.h>
    void main(void)
    {
    	int n,i;                      //n will be the number of integers that will be entered
    	printf("No of values:");
    	scanf("%d",&n);
    	int *p;
    	p=(int *)malloc (n * sizeof(int));/*dynamic memory allocation for saving the entered values*/
    	for(i=0;i<n;i++)
    	{
    		scanf("%d",&p[i]);
    	}/*saving values in pointer*/
    	bubbleSorting(n,*p);
    }
    bubbleSorting(int n,int *p)
    {
    	int i,j;
    	printf("values before bubble Sorting:\n")
    	for(i=0;i<n;i++)
    	{
    		printf("%d",p[i]);
    	}
    	for(i=0;i<n;i++)
    	{
    		for(j=0;j<n;j++)
    		{
    			if(p[j]>p[j+1])
    			{
    				p[j]=p[j]+p[j+1];
    				p[j+1]=p[j]-p[j+1];
    				p[j]=p[j]-p[j+1];
    			}
    		}
    	}
    	printf("values after Bubble sorting:\n");
    	for(i=0;i<n;i++)
    	{
    		printf("%d\n",p[j]);
    	}
    }
     

Share This Page