Divide and conquer
- divide and conquer is a recursive problem solving paradigm that breaks down a complex problem into smaller manageable ones and each one is solved independently and their solutions are combined to derive the solution to the original problem.
- Base case:
- Identifying a base case that can be solved without further decomposition and define a criteria to define this case.
- Recursive decomposition:
- determine how to breakdown the problem into smaller similar subproblems until reaching the base case.
- Solve the base case
- Complete the smallest possible operation directly.
- Combine subproblems
- now logically recombine the data to complete your solution
Quicksort
Quicksort is an efficient, in-place, divide and conquer sorting algorithm. It is widely used due to its average-case time complexity of $𝑂(𝑛log𝑛)$ and its ability to sort large datasets efficiently. The key steps involved in QuickSort are:
-
Choose a Pivot:
- Select a pivot element from the array. The pivot can be any element, but commonly the first, last, or a random element is chosen.
-
Partition the Array:
- Rearrange the elements such that:
- Elements less than the pivot are moved to its left.
- Elements greater than the pivot are moved to its right.
- This process places the pivot in its correct sorted position.
- Rearrange the elements such that:
-
Recursively Apply QuickSort:
- Apply QuickSort to the subarray on the left of the pivot.
- Apply QuickSort to the subarray on the right of the pivot.
-
Base Case:
- The recursion ends when the subarray has one or zero elements, which means it is already sorted.
# quick sort better named pivot sort:
# uses 2 functions main recursive one lets call it quick sort and a second one to comapre and swap values inplace call partition.
def quicksort (list ,low = 0, high = None ):
if high == None:
high = len(list) - 1
if high>low:
pivot = partition(list,low,high)
quicksort(list,low,pivot-1)
quicksort(list,pivot+1,high)
return list
def partition (list ,left, right):
i = left
j = right
pivot = list[left]
while i < j:
while i < right and list[i] < pivot:
i+=1
while j > left and list[j] > pivot:
j-=1
if i < j:
list[i],list[j] = list[j],list[i]
if list[j] < pivot:
list[j],list[left] = list[left],list[j]
return i
Explanation of the Code
-
Quicksort Function:
- Checks if the
high
parameter isNone
and initialises it. - If
low < high
, it calls thepartition
function to find the pivot index. - Recursively sorts the subarrays to the left and right of the pivot.
- Checks if the
-
Partition Function: - Chooses the first element as the pivot. - Uses two indices,
i
starting fromleft + 1
andj
fromright
. - Movesi
rightwards as long as elements are less than or equal to the pivot. - Movesj
leftwards as long as elements are greater than or equal to the pivot. - Swaps elements ati
andj
ifi
is still less than or equal toj
. - Wheni
andj
cross, the loop exits, and the pivot is swapped into its correct position. - Returns the index of the pivot.** Divide and Conquer** a recursive problem-solving paradigm that breaks down a complex problem into smaller, manageable subproblems, solves each independently, and combines their solutions to solve the original problem. The key steps are:
- Base Case: Identify a simple case that can be solved directly.
- Recursive Decomposition: Break down the problem into smaller, similar subproblems until the base case is reached.
- Solve the Base Case: Solve the smallest subproblems directly.
- Combine Subproblems: Combine the solutions of the subproblems to form the solution to the original problem. Quicksort Quicksort is an efficient, in-place, divide and conquer sorting algorithm with an average-case time complexity of $𝑂(𝑛log𝑛)$. The steps involved are:
- Choose a Pivot: Select a pivot element from the array.
- Partition: Rearrange elements so those less than the pivot are on its left, and those greater are on its right. The pivot is then in its correct position.
- Recursively Apply: Apply QuickSort to the subarrays on the left and right of the pivot.
- Base Case: Recursion stops when subarrays have one or zero elements, meaning they are already sorted.