Writing C++ Code
Example Prompt:
Example Response:
#include
#include
void quicksort(std::vector& arr, int low, int high) {
if (low < high) {
int pivot = arr[high]; // Choosing the last element as pivot
int i = low - 1; // Index of smaller element
for (int j = low; j < high; j++) {
// If current element is smaller than or equal to pivot
if (arr[j] <= pivot) {
i++; // Increment index of smaller element
std::swap(arr[i], arr[j]);
}
}
std::swap(arr[i + 1], arr[high]); // Swap the pivot element with the element at i + 1
int partitionIndex = i + 1;
// Recursively sort elements before and after partition
quicksort(arr, low, partitionIndex - 1);
quicksort(arr, partitionIndex + 1, high);
}
}
int main() {
std::vector arr = {10, 7, 8, 9, 1, 5};
int n = arr.size();
quicksort(arr, 0, n - 1);
std::cout << "Sorted array: ";
for (int i : arr) {
std::cout << i << " ";
}
return 0;
}
Related Tasks:
Identifying and Fixing Bugs
Example Prompt:
Example Response:
Related Tasks:
Optimizing C++ Code
Example Prompt:
Example Response:
Related Tasks:
Creating Technical Documentation
Example Prompt:
Example Response:
Related Tasks:
Code Review and Feedback
Example Prompt:
Example Response:
Related Tasks: