0% found this document useful (0 votes)
6 views8 pages

Quick Sort and Bubble Sort

The document presents an overview of two sorting algorithms: Quick Sort and Bubble Sort. Quick Sort is efficient with a time complexity of O(n log n) and works by selecting a pivot element to partition the array, while Bubble Sort is simpler but less efficient, running in O(n^2) time. It includes definitions, explanations, Big O analysis, and example implementations for both algorithms.

Uploaded by

kadeeagles0409
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
6 views8 pages

Quick Sort and Bubble Sort

The document presents an overview of two sorting algorithms: Quick Sort and Bubble Sort. Quick Sort is efficient with a time complexity of O(n log n) and works by selecting a pivot element to partition the array, while Bubble Sort is simpler but less efficient, running in O(n^2) time. It includes definitions, explanations, Big O analysis, and example implementations for both algorithms.

Uploaded by

kadeeagles0409
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

Katlanah is proud to

present

Quick Sort and


Bubble sort
Mrs English’s Favorite
Definitions

Quick Sort - Quick sort is a sorting


algorithm based off picking a pivot and
basing an array around it.

Bubble Sort - Bubble Sort is a sorting


algorithm based off swapping the adjacent
elements if they aren’t in order.

A KadKorp Presentation
Bubble Sort is a simple sorting algorithm
that repeatedly steps through the list,
comparing adjacent elements and swapping
them if they're in the wrong order until
Explanation of Bubble correct.

Sort and Quicksort


Quicksort is a fast sorting algorithm that
organizes data by picking a "pivot" element
and moving all smaller numbers to its left
and larger numbers to its right. It repeats
this process recursively on each side until
the list is fully sorted.

Shaylan Patel and Noah lee 3


Big O analysis of bubble Bubble Sort runs in O(n^2) time and O(1) space. It
is inefficient for large datasets because it relies on
sort nested loops to repeatedly scan the list and swap
adjacent elements. While it requires almost no extra
memory, the sheer number of comparisons makes it
practical only for very small or nearly sorted lists

4
Big O analysis of quick Quick Sort typically runs in O(n log n) time and
O(log n) space. It is highly efficient because it
sort recursively divides the input into smaller partitions
around a "pivot" element. Although it can degrade to
O(n^2) if a poor pivot is chosen (like a sorted array),
it is generally the fastest option for general-purpose
sorting in memory.

5
Demonstration

Bubble sort Quick sort

6
Quick Sort Bubble Sort 1. Outer Loop (Pass Control): This

loop runs for n-1 iterations, where


n is the number of elements in the
array. Each iteration ensures that
the next largest unsorted element
is placed in its correct final
position at the end of the unsorted
def bubble_sort(arr):
def quick_sort(arr): portion of the array.
"""Sorts an array using the Bubble Sort algorithm."""
"""Sorts an array using the Quick Sort algorithm.""" n = len(arr) 2. Inner Loop (Comparison and
if len(arr) <= 1: # Outer loop to traverse through all array elements
Swap): In each pass, this loop
return arr for i in range(n):
else: # Keep track if any swapping happened in this pass iterates through the unsorted
# Choose the first element as the pivot swapped = False portion of the array. It compares
pivot = arr[0] # Inner loop to compare adjacent elements
for j in range(0, n - i - 1): each element with the one
# Elements less than the pivot
less = [i for i in arr[1:] if i <= pivot] # Swap if the element found is greater than the next element immediately following it.
# Elements greater than the pivot if arr[j] > arr[j + 1]:
3. Swapping: If the elements are in
greater = [i for i in arr[1:] if i > pivot] arr[j], arr[j + 1] = arr[j + 1], arr[j]
swapped = True the wrong order (e.g., the first is
# Recursively sort the sub-arrays and combine them
# If no two elements were swapped by inner loop, then the list is sorted greater than the second when
return quick_sort(less) + [pivot] + quick_sort(greater)
if not swapped:
break sorting in ascending order), their
# Example Usage:
return arr values are swapped.
my_list = [3, 6, 8, 10, 1, 2, 1]
print(f"Original list: {my_list}") # Example Usage:
sorted_list = quick_sort(my_list) my_list = [64, 34, 25, 12, 22, 11, 90]
print(f"Sorted list: {sorted_list}") print(f"Original list: {my_list}")
sorted_list = bubble_sort(my_list)
print(f"Sorted list: {sorted_list}")
Kade slide 7
References

[Link]

[Link]

[Link]
bubble-sort/

You might also like