Quicksort 1 - Partition : HackerRank Problem Solution

Quicksort 1 - Partition : HackerRank Problem Solution

C Implementation:


#include <stdio.h>
#include <string.h>
#include <math.h>
#include <stdlib.h>
#include <assert.h>
void swap(int *x,int *y)
    {
    int temp;
    temp=*x;
    *x=*y;
    *y=temp;
}

void partition(int ar_size, int *  ar) {
    int pivot=*(ar+0);
    int pindex=ar_size-1;
    int i;
    for(i=ar_size-1;i>0;i--)
        {
        if(*(ar+i)>pivot)
            {
            swap((ar+i),(ar+pindex));
            pindex--;
        }
    }
    swap((ar+pindex),(ar+0));
    for(i=0;i<ar_size;i++)
        printf("%d ",*(ar+i));
}
int main(void) {
   
   int _ar_size;
scanf("%d", &_ar_size);
int _ar[_ar_size], _ar_i;
for(_ar_i = 0; _ar_i < _ar_size; _ar_i++) { 
   scanf("%d", &_ar[_ar_i]); 
}

partition(_ar_size, _ar);
   
   return 0;
}