0% found this document useful (0 votes)
11 views2 pages

Quick Sort Implementation in C/C++

The document outlines a C/C++ program that implements the Quick Sort algorithm to sort a set of n integer elements and measures its time complexity. It includes a partition function and a quicksort function, along with a main function that generates random integers and sorts them. The program also tracks the number of basic operations performed during sorting.

Uploaded by

vijethviju04
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
11 views2 pages

Quick Sort Implementation in C/C++

The document outlines a C/C++ program that implements the Quick Sort algorithm to sort a set of n integer elements and measures its time complexity. It includes a partition function and a quicksort function, along with a main function that generates random integers and sorts them. The program also tracks the number of basic operations performed during sorting.

Uploaded by

vijethviju04
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd

Program 10

Design and implement C/C++ Program to sort a given set of n integer elements using
Quick Sort method and compute its time complexity. Run the program for varied values
of n> 5000 and record the time taken to sort. Plot a graph of the time taken versus n.
The
elements can be read from a file or can be generated using the random number
generator.

#include<stdio.h>
#include<stdlib.h>
#include<time.h>

int count=0;
int partition(int a[], int low,int high)
{
int pivot=a[low],temp,i=low+1,j=high;
while(1)
{
//Traverse i from left to right, segregating element of left group
while(i<=high && a[i]<=pivot)//a[i]<=pivot used for avoiding multiple duplicates
{
i++; count++;
}
//Traverse j from right to left, segregating element of right group
while(j>0 && a[j]>pivot)
{
j--; count++;
}
count+=2;
//If grouping is incomplete
if(i<j)
{
temp = a[i];
a[i] = a[j];
a[j] =temp;
}
else if(i>j)//If grouping is completed
{
temp = a[low];
a[low] = a[j];
a[j] = temp;
return j;
}
else //Duplicate of Pivot found
return j;
}
}

void quicksort(int a[],int low, int high)


{
int s;
if(low<high)
{
//partition to place pivot element in between left and right group
s = partition(a,low,high);
quicksort(a,low,s-1);
quicksort(a,s+1,high);
}
}

int main()
{
int a[10000],n;
printf("Enter the number of elements in an array:");
scanf("%d",&n);
printf("All the elements:");
srand(time(0));
for(int i=0;i<n;i++)
{
a[i]=rand();
printf("%d ",a[i]);
}
quicksort(a,0,n-1);
printf("\nAfter sorting\n");
for(int i=0;i<n;i++)
printf("%d ", a[i]);
printf("\nNumber of basic operations = %d\n",count);
}

Sample Input and Output:


Enter the number of elements in an array:5
All the elements:
24442 6310 12583 16519 22767
After sorting
6310 12583 16519 22767 24442
Number of basic operations = 18

Common questions

Powered by AI

The partition function segregates elements by moving them across the pivot such that elements less than or equal to the pivot are on the left and greater elements on the right. The function uses two indices to traverse the list from both ends towards the center, swapping elements when necessary. The significance of its return value is that it indicates the new pivot position, needed to divide the array into subarrays to recursively apply the quicksort further, thus ensuring proper sorting .

The time complexity of the implemented quicksort algorithm is primarily illustrated through experimental runs with various input sizes `n > 5000`. By recording the execution time across these test cases and plotting a graph of `time taken versus n`, the empirical complexity can be observed. Typically, this demonstrates O(n log n) behavior for average cases. Measuring the real-time taken for different input sizes helps in visually and quantitatively assessing how the algorithm scales, hence providing practical validation of its theoretical time complexity .

Using a graph to represent the time taken versus `n` in sorting algorithms aids in visualizing the scalability and efficiency of the algorithm across different input sizes. Such a graph typically helps in identifying the algorithm's empirical performance trends, revealing average-case complexities, and potential anomalies such as unexpected spikes indicating worst-case scenarios. Drawing insights from the curve shapes, one can discern whether the algorithm is operating closer to O(n log n) or O(n^2) curves, thus validating theoretical expectations against experimental results .

In this implementation, recursion is utilized by repeatedly calling the `quicksort` function on subarrays derived from partitioning the main array around a pivot. Initially, the entire array is sorted by partitioning it, placing its pivot in the correct position. The function then recursively sorts the left subarray (elements lower than the pivot) and the right subarray (elements greater than the pivot). This recursive partitioning ensures the entire array becomes sorted as base cases are reached (subarrays of length 1 or 0).

The `count` variable tracks the number of basic operations performed during the execution of the quicksort algorithm. It is incremented during swaps and comparisons, providing a basic measure of workload handled by the algorithm. By counting these operations, it helps in analyzing the algorithm’s efficiency, offering insights into computational complexity beyond traditional time-based measures, especially useful for theoretical and comparative analysis .

The use of `srand(time(0))` is crucial for seeding the random number generator with a current time-based value to produce different sequences of random numbers for each execution. If omitted, the program would default to the same seed value each time, resulting in the same sequence of 'random' numbers for every run. This would affect the testing variability and may lead to biased performance assessments as the algorithm would repeatedly encounter the same input scenario .

The program allows elements to either be read from a file or generated randomly. Reading from a file can provide specific test cases and reproducibility; however, it limits the variability of input unless the file is frequently updated. Random generation offers varied sequences, which is beneficial in testing algorithmic robustness and performance under different conditions but lacks predictability. Optimal performance is often achieved with random generation as it helps in analyzing average-case behaviors rather than worst-case scenarios commonly seen with sorted or reversely sorted file inputs .

When an already sorted array is inputted to this quicksort implementation with a pivot as the first element, the partitioning process performs inefficiently by consistently making the worst possible split, leading to O(n^2) time complexity. Each partition call only reduces the problem size by one element due to unbalanced pivot placements, resulting in a high number of operations resembling a degenerate quicksort. The `count` of basic operations would be significantly higher, highlighting inefficiencies .

Selecting a pivot at the `low` index helps avoid additional complexity in selecting pivots dynamically and simplifies the code logic. However, it may lead to performance issues if the input is already sorted or reversely sorted, causing poor partitioning and thus degrading quicksort performance to O(n^2). Alternatively, selecting a random pivot or using median-of-three selection can reduce the risk of encountering worst-case scenarios and might lead to more balanced partitions, generally improving average-case performance and maintaining O(n log n) complexity .

The primary challenge in the quicksort algorithm with duplicate elements is to prevent unnecessary swaps and recursive calls that do not lead to partitions. In the provided C++ implementation, the code tackles duplicate handling by using a condition `a[i]<=pivot` to continue incrementing the left index `i`, which avoids infinite loops and multiple duplications during sorting .

You might also like