-
Notifications
You must be signed in to change notification settings - Fork 293
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #1389 from karthikyandrapu/dev
Added Partition Sort
- Loading branch information
Showing
2 changed files
with
145 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,84 @@ | ||
#include <stdio.h> | ||
#include <stdlib.h> | ||
|
||
// Function to swap two elements in the array | ||
void swap(int *a, int *b) | ||
{ | ||
int tmp = *a; | ||
*a = *b; | ||
*b = tmp; | ||
} | ||
|
||
// Partition function implementing Hoare's partitioning scheme | ||
// It rearranges the elements around a pivot and returns the index where partitioning ends | ||
int partition(int arr[], int low, int high) | ||
{ | ||
int pivot = arr[low]; // Selecting the first element as the pivot | ||
int i = low - 1, j = high + 1; | ||
|
||
while (1) | ||
{ | ||
// Move `i` right until an element >= pivot is found | ||
do | ||
{ | ||
i++; | ||
} while (arr[i] < pivot); | ||
|
||
// Move `j` left until an element <= pivot is found | ||
do | ||
{ | ||
j--; | ||
} while (arr[j] > pivot); | ||
|
||
// If pointers cross, partitioning is complete | ||
if (i >= j) | ||
return j; | ||
|
||
// Swap elements at `i` and `j` to move them to correct sides of the pivot | ||
swap(&arr[i], &arr[j]); | ||
} | ||
} | ||
|
||
// Recursive function to apply partition sort to subarrays | ||
void partitionSort(int arr[], int low, int high) | ||
{ | ||
if (low < high) | ||
{ | ||
// Partition the array and get the partition index | ||
int value = partition(arr, low, high); | ||
|
||
// Recursively sort elements before and after partition | ||
partitionSort(arr, low, value); | ||
partitionSort(arr, value + 1, high); | ||
} | ||
} | ||
|
||
// Function to print the elements of the array | ||
void printArray(int arr[], int n) | ||
{ | ||
for (int i = 0; i < n; i++) | ||
printf("%d ", arr[i]); | ||
printf("\n"); | ||
} | ||
|
||
int main() | ||
{ | ||
int arr[20]; // Array to hold random numbers | ||
int range = 100; // Range of random numbers to generate | ||
|
||
// Fill the array with random numbers from 1 to 100 | ||
for (int i = 0; i < 20; i++) | ||
{ | ||
arr[i] = rand() % range + 1; | ||
} | ||
|
||
int size = sizeof(arr) / sizeof(arr[0]); // Calculate the size of the array | ||
printf("Array: \n"); | ||
printArray(arr, size); // Print the unsorted array | ||
|
||
partitionSort(arr, 0, size - 1); // Sort the array using partition sort | ||
printf("Sorted Array: \n"); | ||
printArray(arr, size); // Print the sorted array | ||
|
||
return 0; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,61 @@ | ||
# Partition Sort Algorithm | ||
|
||
This document provides an overview of the Partition Sort algorithm, an implementation that uses the Hoare Partition scheme. This is a variant of the QuickSort algorithm that partitions the array around a pivot and recursively sorts the partitions. | ||
|
||
## Code Description | ||
|
||
The algorithm uses the following functions: | ||
|
||
- `swap(int *a, int *b)`: Swaps two elements in an array. | ||
- `partition(int arr[], int low, int high)`: Partitions the array based on the Hoare Partition scheme. It selects the first element as a pivot, then rearranges elements such that all elements smaller than the pivot are on the left, and all elements greater are on the right. | ||
- `partitionSort(int arr[], int low, int high)`: Recursively sorts the array by partitioning it until each element is individually sorted. | ||
- `printArray(int arr[], int n)`: Prints the contents of an array. | ||
|
||
## Example Usage | ||
|
||
The following example demonstrates the use of the algorithm with an array of random integers: | ||
|
||
```c | ||
#include <stdio.h> | ||
#include <stdlib.h> | ||
|
||
// Function implementations ... | ||
|
||
int main() | ||
{ | ||
int arr[20]; | ||
int range = 100; | ||
for (int i = 0; i < 20; i++) | ||
{ | ||
arr[i] = rand() % range + 1; | ||
} | ||
|
||
printf("Unsorted Array: \n"); | ||
printArray(arr, 20); | ||
|
||
partitionSort(arr, 0, 19); | ||
|
||
printf("Sorted Array: \n"); | ||
printArray(arr, 20); | ||
|
||
return 0; | ||
} | ||
``` | ||
|
||
## Steps in the Algorithm | ||
|
||
1. **Generate Random Array**: Create an array of random integers. | ||
2. **Partitioning**: Select a pivot element and partition the array, moving elements less than the pivot to the left and greater ones to the right. | ||
3. **Recursive Sort**: Recursively apply the `partitionSort` function to each partitioned segment until the entire array is sorted. | ||
4. **Print the Sorted Array**: Use `printArray` to output the sorted array. | ||
|
||
## Time Complexity | ||
|
||
- **Best Case**: \(O(n \log n)\) - When the pivot divides the array into two equal halves. | ||
- **Average Case**: \(O(n \log n)\) - Expected with random pivots. | ||
- **Worst Case**: \(O(n^2)\) - Occurs when the pivot is always the smallest or largest element, leading to unbalanced partitions (for example, when the array is already sorted). | ||
|
||
## Space Complexity | ||
|
||
- **Space Complexity**: \(O(\log n)\) - This comes from the recursion stack used by the recursive calls of the `partitionSort` function. | ||
|