Fix bug and format code in QuickSort.c

This commit is contained in:
Libin Yang 2019-05-05 15:23:58 +08:00 committed by GitHub
parent 2fde23c6ed
commit 67e2e98311
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

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