Skip to content

Instantly share code, notes, and snippets.

@prashant1k99
Created July 9, 2019 16:18
Show Gist options
  • Save prashant1k99/a859748a019b2922c12dafb83ce67765 to your computer and use it in GitHub Desktop.
Save prashant1k99/a859748a019b2922c12dafb83ce67765 to your computer and use it in GitHub Desktop.
# include <conio.h>
# include <stdio.h>
void main(){
int n, a[20], min, max, i;
// Created a function to sort the array
int sortArray(a[20], min, max);
// For scanning the total number of elements in unsorted array
scanf("%d\n", &n);
// For scanning the array
for(i=0; i<n; i++) scanf("%d ", &a[i]);
// Function called for sorting
a = sortArray(a, 0, n);
//Output of the sorted Array
for(i = 0; i < n; i++) printf("%d\t", &a[i]);
getch();
}
int sortArray(a, min, max){
/*
** As we are checking before sending to the function
** if(min==max) return a; <- Not Required
*/
int p = a[min], i = min, j = max, temp;
/*
** The loop shall run until the j does not overtakes i
** i.e., j < i
*/
while(j >= i){
if((a[i] > p) && (a[j] < p)){
temp = a[i];
a[i] = a[j];
a[j] = temp;
}
else if(a[i] <= p) i++;
else if(a[j] > p) j--;
}
/*
** When j overtakes i as j < i
** The pivot element has to swap
*/
temp = p;
p = a[j];
a[j] = temp;
/*
** Checking before sending to the function that it does not have only 1 element to sort
*/
if(j-1 != 0){
// For Sorting the Left Side of the Shifted pivot
a = sortArray(a, 0, j-1);
}
if(j+1 != max){
// For Sorting the Right Side of the Shifted pivot
a = sortArray(a, j+1, max);
}
return a;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment