Junior — Middle
Do you have examples of tasks completed with particular satisfaction?
sobes.tech AI
Answer from AI
One example of a task completed with particular satisfaction was implementing an efficient sorting algorithm for a large data array in C++. The task required optimization for both time and memory. I used a quicksort algorithm with improvements such as median-of-three pivot selection and switching to insertion sort for small subarrays. This significantly sped up the program.
#include <iostream>
#include <vector>
void insertionSort(std::vector<int>& arr, int left, int right) {
for (int i = left + 1; i <= right; ++i) {
int key = arr[i];
int j = i - 1;
while (j >= left && arr[j] > key) {
arr[j + 1] = arr[j];
--j;
}
arr[j + 1] = key;
}
}
int medianOfThree(std::vector<int>& arr, int left, int right) {
int mid = left + (right - left) / 2;
if (arr[right] < arr[left]) std::swap(arr[left], arr[right]);
if (arr[mid] < arr[left]) std::swap(arr[mid], arr[left]);
if (arr[right] < arr[mid]) std::swap(arr[right], arr[mid]);
return mid;
}
void quickSort(std::vector<int>& arr, int left, int right) {
if (right - left <= 10) {
insertionSort(arr, left, right);
return;
}
int pivotIndex = medianOfThree(arr, left, right);
int pivot = arr[pivotIndex];
std::swap(arr[pivotIndex], arr[right]);
int i = left;
for (int j = left; j < right; ++j) {
if (arr[j] < pivot) {
std::swap(arr[i], arr[j]);
++i;
}
}
std::swap(arr[i], arr[right]);
quickSort(arr, left, i - 1);
quickSort(arr, i + 1, right);
}
int main() {
std::vector<int> data = {10, 7, 8, 9, 1, 5};
quickSort(data, 0, data.size() - 1);
for (int num : data) {
std::cout << num << " ";
}
return 0;
}
The result was a stable and fast sorting process, which improved the project's performance.