



Prepara tus exámenes y mejora tus resultados gracias a la gran cantidad de recursos disponibles en Docsity
Gana puntos ayudando a otros estudiantes o consíguelos activando un Plan Premium
Esta página no es visible en la vista previa
¡No te pierdas las partes importantes!




Quicksort Quicksort is a highly efficient sorting algorithm. It uses the Divide and Conquer (D&C) strategy to sort an array, making it significantly faster than simple algorithms like Selection Sort. Divide and Conquer (D&C) Divide and Conquer is a programming technique used to solve complex problems by breaking them down into simpler subproblems. It works in three main steps:
o Elements greater than the pivot are placed in another sub-array.
Analyzing Quicksort's Time Complexity Let's review the main steps of Quicksort to see how the time complexity is calculated:
Visualizing the Process Since each level takes O(n) time and there are log n levels, the overall time complexity is: O(n)×O(log n)=O(n log n) Why Is It "Average Case"? The O(n log n) time complexity is the average case for Quicksort, which occurs when the pivot divides the array into two sub-arrays of approximately equal size at each step. This scenario is typical when: The pivot is chosen randomly. The elements in the array are not arranged in a worst-case order (like already sorted or reverse sorted in some implementations). Worst Case: O(n²) The worst case occurs when the pivot selection is poor, such as always choosing the smallest or largest element when the array is already sorted or nearly sorted. In this case: Instead of dividing the array in half, one sub-array has almost all the elements, and the other sub-array is empty or has only one element. The recursion depth becomes n, leading to a total time complexity of: O(n)×O(n)= O(n²) Summary O(n log n) occurs because: o Partitioning the array takes O(n) time per level. o There are log n levels of recursion if the array is divided evenly at each step. Efficient pivot selection is crucial to ensure Quicksort runs in O(n log n) time on average.