
We are Step 3.5 Flash, a large language model developed by StepFun. The user asks: "write a quicksort program in c". We need to provide a C program that implements quicksort. We should include a main function that demonstrates the sorting, perhaps with a sample array. The code should be clear, with comments. We can also explain briefly how it works. Since the user just asked to write the program, we can provide the code directly.
We need to be mindful: The user is speaking English. So we respond in English. The date is 2026-02-10, but that doesn't affect the code. We'll produce a standard quicksort implementation. Possibly we can include both recursive and iterative versions? But the simplest is recursive. We'll write a function quicksort that takes an array and the low and high indices. We'll use a partition function. We'll choose the pivot as the last element or maybe median-of-three? Keep it simple: pivot = arr[high]. We'll swap elements accordingly.
We'll also include a print function to show before and after.
We should ensure it's correct. We'll test mentally.
We'll write:
#include <stdio.h>
void swap(int *a, int *b) { int t=*a; *a=*b; *b=t; }
int partition(int arr[], int low, int high) {
int pivot = arr[high];
int i = low - 1;
for (int j = low; j <= high-1; j++) {
if (arr[j] <= pivot) {
i++;
swap(&arr[i], &arr[j]);
}
}
swap(&arr[i+1], &arr[high]);
return i+1;
}
void quicksort(int arr[], int low, int high) {
if (low < high) {
int pi = partition(arr, low, high);
quicksort(arr, low, pi-1);
quicksort(arr, pi+1, high);
}
}
void printArray(int arr[], int size) {
for (int i=0; i<size; i++) printf("%d ", arr[i]);
printf("\n");
}
int main() {
int arr[] = {10, 7, 8, 9, 1, 5};
int n = sizeof(arr)/sizeof(arr[0]);
printf("Original array: ");
printArray(arr, n);
quicksort(arr, 0, n-1);
printf("Sorted array: ");
printArray(arr, n);
return 0;
}
That's fine. We could also include a more optimized version with random pivot to avoid worst-case, but not necessary.
We might also mention that the time complexity is O(n log n) average, O(n^2) worst-case.
We'll provide the code and a brief explanation.
We need to follow the guidelines: be helpful, safe. This is a straightforward programming request, no issues.
We'll output the code in a markdown code block. We'll also include comments in the code for clarity.
We'll ensure the code compiles with standard C (C99 or later). We'll use stdio.h only.
We might also include a note about stack overflow for large arrays due to recursion, but not needed.
We'll produce final answer.
Here's a simple implementation of the Quicksort algorithm in C. The program sorts an integer array in ascending order using the classic recursive approach with the last element as the pivot.
How it works:
partition function selects the last element as the pivot. It then rearranges the subarray so that all elements less than or equal to the pivot come before it, and all greater elements come after it. The pivot ends up in its final sorted position.quicksort function calls itself on the left and right subarrays (excluding the pivot) until the entire array is sorted.Complexity:
Note: For large arrays or worst-case inputs, you might want to improve the pivot selection (e.g., using a random pivot or median-of-three) to avoid quadratic performance. This basic version is great for learning and small to medium datasets.
We are going to read the array from standard input. Let's assume the input format is:
First line: an integer n (the number of elements)
Second line: n integers separated by spaces
We'll adjust the program accordingly.
Here's an updated version of the Quicksort program in C that reads input from stdin:
Input format:
Example:
Output:
malloc() to create an array of size n read from input.scanf() return values to ensure correct reading of integers.stderr and exits with non-zero status on failure.free(arr) to avoid memory leaks.Then type/paste the input (or pipe from a file/another command):
n integers after the first line. If fewer are provided, it will wait for more input or fail on invalid data.n, consider using iterative Quicksort to avoid deep recursion (stack overflow). This version is fine for moderate sizes (up to ~10⁵ on typical systems).