#1
  1. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Dec 2012
    Posts
    2
    Rep Power
    0

    Thumbs up Problem in array partition program


    The following program to partition the array stops unexpectedly when executed. Can anyone help me out?
    Code:
    #include<stdio.h>
    #include<conio.h>
    int partition(int [],int,int);
    main()
    {
        int n,i;
        printf("enter the number of elements in the array\n");
        scanf("%d",&n);
        int a[n];
        for(i=0;i<n;i++)
        {
            printf("enter the number\n");
            scanf("%d",a+i);
        }
        printf("the index of the pivot is %d and the partitioned array is\n",partition(a,0,n-1));
        for(i=0;i<n;i++) printf("%d\t",*(a+i));
        getch();
    }
    int partition(int a[],int lower,int upper)
    {
        int p=a[lower];
        int i=lower,j=upper+1,temp;
        do
           { do{i++;}while(a[i]>=p);
             do{j--;}while(a[j]<=p);
             if(i<j){ temp=a[i];
                      a[i]=a[j];
                      a[j]=temp;
                    }
           }while(i>=j);
    
          a[lower]=a[j];
          a[j]=p;
          return j;
    }
  2. #2
  3. Contributed User
    Devshed Specialist (4000 - 4499 posts)

    Join Date
    Jun 2005
    Posts
    4,407
    Rep Power
    1871
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper

IMN logo majestic logo threadwatch logo seochat tools logo