Sobes.tech
Junior — Middle

Can you list different sorting algorithms you know?

sobes.tech AI

Answer from AI

There are many sorting algorithms, here are some of the most well-known:

  • Bubble Sort — a simple algorithm that repeatedly passes through the list, comparing adjacent elements and swapping them if they are in the wrong order.
  • Insertion Sort — builds a sorted array one element at a time by inserting each new element into its proper place.
  • Selection Sort — at each step, selects the minimum element from the unsorted part and swaps it with the first element of that part.
  • Merge Sort — recursively divides the array into halves, sorts them, and merges them back.
  • Quick Sort — selects a pivot element, partitions the array into elements less than and greater than the pivot, and recursively sorts the partitions.
  • Heap Sort — builds a heap structure and extracts the maximum element, restructuring the heap.

Example of quick sort in C++:

void quickSort(int arr[], int left, int right) {
    int i = left, j = right;
    int pivot = arr[(left + right) / 2];

    while (i <= j) {
        while (arr[i] < pivot) i++;
        while (arr[j] > pivot) j--;
        if (i <= j) {
            std::swap(arr[i], arr[j]);
            i++; j--;
        }
    }

    if (left < j) quickSort(arr, left, j);
    if (i < right) quickSort(arr, i, right);
}