TheAlgorithms-C/sorting/quick_sort.c

105 lines
2.2 KiB
C
Raw Normal View History

2017-08-16 19:53:09 +03:00
#include <stdio.h>
2019-05-05 10:23:58 +03:00
#include <stdlib.h>
2017-08-16 19:53:09 +03:00
/*Displays the array, passed to this method*/
2019-05-05 10:23:58 +03:00
void display(int arr[], int n)
{
2017-08-16 19:53:09 +03:00
2019-05-05 10:23:58 +03:00
int i;
for (i = 0; i < n; i++)
{
printf("%d ", arr[i]);
}
2017-08-16 19:53:09 +03:00
2019-05-05 10:23:58 +03:00
printf("\n");
2017-08-16 19:53:09 +03:00
}
/*Swap function to swap two values*/
2019-05-05 10:23:58 +03:00
void swap(int *first, int *second)
{
2017-08-16 19:53:09 +03:00
2019-05-05 10:23:58 +03:00
int temp = *first;
*first = *second;
*second = temp;
2017-08-16 19:53:09 +03:00
}
/*Partition method which selects a pivot
and places each element which is less than the pivot value to its left
and the elements greater than the pivot value to its right
arr[] --- array to be partitioned
lower --- lower index
2017-08-16 19:53:09 +03:00
upper --- upper index
*/
2019-05-05 10:23:58 +03:00
int partition(int arr[], int lower, int upper)
{
2017-08-16 19:53:09 +03:00
2019-05-05 10:23:58 +03:00
int i = (lower - 1);
2017-08-16 19:53:09 +03:00
2019-05-05 10:23:58 +03:00
int pivot = arr[upper]; // Selects last element as the pivot value
2017-08-16 19:53:09 +03:00
2019-05-05 10:23:58 +03:00
int j;
for (j = lower; j < upper; j++)
{
2017-08-16 19:53:09 +03:00
2019-05-05 10:23:58 +03:00
if (arr[j] <= pivot)
{ // if current element is smaller than the pivot
2017-08-16 19:53:09 +03:00
2019-05-05 10:23:58 +03:00
i++; // increment the index of smaller element
swap(&arr[i], &arr[j]);
}
}
2017-08-16 19:53:09 +03:00
swap(&arr[i + 1], &arr[upper]); // places the last element i.e, the pivot to
// its correct position
2017-08-16 19:53:09 +03:00
2019-05-05 10:23:58 +03:00
return (i + 1);
}
2017-08-16 19:53:09 +03:00
/*This is where the sorting of the array takes place
arr[] --- Array to be sorted
lower --- Starting index
upper --- Ending index
2017-08-16 19:53:09 +03:00
*/
2019-05-05 10:23:58 +03:00
void quickSort(int arr[], int lower, int upper)
{
2017-08-16 19:53:09 +03:00
2019-05-05 10:23:58 +03:00
if (upper > lower)
{
2017-08-16 19:53:09 +03:00
// partitioning index is returned by the partition method , partition
// element is at its correct poition
2017-08-16 19:53:09 +03:00
2019-05-05 10:23:58 +03:00
int partitionIndex = partition(arr, lower, upper);
2017-08-16 19:53:09 +03:00
2019-05-05 10:23:58 +03:00
// Sorting elements before and after the partition index
quickSort(arr, lower, partitionIndex - 1);
quickSort(arr, partitionIndex + 1, upper);
}
2017-08-16 19:53:09 +03:00
}
2019-05-05 10:23:58 +03:00
int main()
{
2017-08-16 19:53:09 +03:00
2019-05-05 10:23:58 +03:00
int n;
printf("Enter size of array:\n");
scanf("%d", &n); // E.g. 8
2017-08-16 19:53:09 +03:00
2019-05-05 10:23:58 +03:00
printf("Enter the elements of the array\n");
int i;
int *arr = (int *)malloc(sizeof(int) * n);
for (i = 0; i < n; i++)
{
scanf("%d", &arr[i]);
}
2017-08-16 19:53:09 +03:00
2019-05-05 10:23:58 +03:00
printf("Original array: ");
display(arr, n); // Original array : 10 11 9 8 4 7 3 8
2017-08-16 19:53:09 +03:00
2019-05-05 10:23:58 +03:00
quickSort(arr, 0, n - 1);
2017-08-16 19:53:09 +03:00
2019-05-05 10:23:58 +03:00
printf("Sorted array: ");
display(arr, n); // Sorted array : 3 4 7 8 8 9 10 11
getchar();
return 0;
}