# Generic quick sort

While comparing in generic quick sort , void * is to be converted to char * and comparator and swap functions would be provided by the caller.

/* C implementation QuickSort */

#include<stdio.h>

int cmp(const void *c1, const void *c2)

{

int a = *(const int*)c1;

int b = *(const int*)c2;

if (a > b) return 1;

if (a < b) return -1;

return 0;

}

void swap(void *c1, void *c2)

{

int c = *(int*)c1;

*(int*)c1 = *(int*)c2;

*(int*)c2 = c;

}

/* This function takes last element as pivot, places

the pivot element at its correct position in sorted

array, and places all smaller (smaller than pivot)

to left of pivot and all greater elements to right

of pivot */

int partition (void * arr,int size , int low, int high)

{

int pivot = high; // pivot

int i = (low – 1); // Index of smaller element

for (int j = low; j <= high- 1; j++)

{

// If current element is smaller than or

// equal to pivot

//if (arr[j] <= pivot)

if (cmp((char*)arr + pivot * size, (char*)arr + j * size) > 0)

{

i++; // increment index of smaller element

//swap(&arr[i], &arr[j]);

swap((char*)arr + i * size, (char*)arr + j * size);

}

}

//swap(&arr[i + 1], &arr[high]);

swap((char*)arr + (i+1) * size, (char*)arr + high * size);

return (i + 1);

}

/* The main function that implements QuickSort

arr[] –> Array to be sorted,

low –> Starting index,

high –> Ending index */

void quickSort(void * arr,int size , int low, int high)

{

if (low < high)

{

/* pi is partitioning index, arr[p] is now

at right place */

int pi = partition(arr, size, low, high);

// Separately sort elements before

// partition and after partition

quickSort(arr, size,low, pi – 1);

quickSort(arr, size ,pi + 1, high);

}

}

/* Function to print an array */

void printArray(int arr[], int size)

{

int i;

for (i=0; i < size; i++)

printf(“%d “, arr[i]);

printf(“\n”);

}

// Driver program to test above functions

int main()

{

int arr[] = {10, 7, 8, 9, 1, 5};

int n = sizeof(arr)/sizeof(arr[0]);

quickSort(arr, 4,0, n-1);

printf(“Sorted array: \n”);

printArray(arr, n);

return 0;

}

**Code to know direction in which stack grows:**

void func(int *p) {

int i;

if (!p)

func(&i);

else if (p < &i)

printf(“Stack grows upward\n”);

else

printf(“Stack grows downward\n”);

}

func(NULL);

These Questions is mainly asked in Adobe Illustrator team interview.

# Resolving technical problems:

Solve your technical problems instantly

We provide Remote Technical Support from Monday to Sunday, 7:00PM to 1:00 AM

Mail your problem details at writeulearn@gmail.com along with your mobile numberand we will give you a call for further details. We usually attend your problems within 60 minutes and solve it in maximum 2 days.