Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
0% found this document useful (0 votes)
7 views

Sorting Algorithms

This a paper of rfesafsdf

Uploaded by

holay42177
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
7 views

Sorting Algorithms

This a paper of rfesafsdf

Uploaded by

holay42177
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 4

Bubble Sort

23 May 2024 14:14

Compares the first item in the list with second item and swaps them if
necessary. Then compares second and third item until end of the list. If all the
elements are compared it is in first pass, then the list is compared again for
second pass.
Time and Space complexities

Time Best Case: O(n)

When the array is already sorted, bubble sort is completed in single pass and
runs the loop once.

Time Average Case: O(n²)

On average, each pair of elements are compared multiple times and requires to
run in loops.

Time Worst Case: O(n²)

The array needs to be sorted in reverse so requires maximum number of


comparisons and swaps (loops)

Space Complexity: O(1)

Requires a constant amount of additional memory space regardless of the size


of the array.

def bubblesort(array):
for i in range(0, len(array)-1):
sorted = True
for j in range(0, len(array)-1):
if array[j]>array[j+1]:
array[j] , array[j+1] = array[j+1], array[j]
sorted = False

if sorted:
break
return array
array = [5, 0, 1, 34, 11, 10, 98 ,2,-1]
print(bubblesort(array))

Insertion Sort

Sorting Algorithm Page 1


Splits the array into two lists - one sorted list and a unsorted list.
The first item in the unsorted list is shuffled down into correct location into the sorted
item.

Time and Space complexities

Time Best Case: O(n)

When the array is already sorted, insertion sort is completed in single pass and runs the
loop once.

Time Average Case: O(n²)

On average, each element is compared with about half of the sorted portion of the array
before being placed in its correct position

Time Worst Case: O(n²)

The array needs to be sorted in reverse so requires maximum number of comparisons


and swaps (loops) as every elements needs to be compared.

Space Complexity: O(1)

Requires a constant amount of additional memory space regardless of the size of the
array.

def insertion_sort(array):

for i in range(1, len(array)):# runs the loop from 2nd element to


the end of the array
item = array[i] # current element is stored in variable: item
j = i -1 #initialise the previous element before i
while j >= 0 and array[j] > item: #compares elements
array[j+1] = array[j] # shift previous to the right
j = j-1 # moves the element [i] into [j] location
array[j+1] = item # this inserts element [i] into [j] location
return array #returns the sorted array
array = [5, 0, 1, 34, 11, 10, 98 ,2,-1]
print(insertion_sort(array))

Merge Sort
Splits the list in half and repeats until the elements in a pair. In pair, the elements are
compared and swap. Then this sorted pair is merge back with other pairs into one sorted
list.
Time and Space complexities
Sorting Algorithm Page 2
Time and Space complexities

Time Best Case: O(n log n)

The list still goes through the recursive process, even if the array is sorted.

Time Average Case: O(n log n)

On average, merge sort behaviour does not change and repeats the same splitting and
merging operation using recursion

Time Worst Case: O(n log n)

Same as average case

Space Complexity: O(n )

Requires extra memory to perform merging and splitting operation by using temporary
arrays to store those pairs.

def mergesort(array):

if len(array) > 1:
mid = len(array) // 2 # Find the middle of the array
left_half = array[:mid] # Divide the array into two halves
right_half = array[mid:]
mergesort(left_half) # Recursively sort the left half
mergesort(right_half) # Recursively sort the right half
i = j = k = 0
# Copy data to temporary arrays left_half and right_half
while i < len(left_half) and j < len(right_half):
if left_half[i] < right_half[j]:
array[k] = left_half[i]
i += 1
else:
array[k] = right_half[j]
j += 1
k += 1
# Checking if any element was left
while i < len(left_half):
array[k] = left_half[i]
i += 1
k += 1
while j < len(right_half):
array[k] = right_half[j]
j += 1
k += 1
return array
Sorting Algorithm Page 3
return array
# Example usage:
array = [5, 0, 1, 34, 11, 10, 98 ,2,-1]
print(mergesort(array))

Quick Sort
Picks an item and call it the pivot. Split the list into two one less than the pivot and
one list where elements are greater than pivot. Then using recursion, splits the list
further into sub lists and then combines at the end.
Time and Space complexities

Time Best Case: O(n log n)

When the pivot divides the array into two nearly equal parts, the recursion depth is
minimized. Each level of recursion handles half of the elements.

Time Average Case: O(n log n)

On average, the pivot will split the array into parts that are not exactly equal but not
extremely unbalanced either.

Time Worst Case: O(n²)

The array needs to be sorted in reverse so requires maximum number of


comparisons and swaps (loops) as every elements needs to be compared.

Space Complexity: O(n log n)

The space complexity is primarily due to the recursive function calls. The depth of the
recursion tree depends on how balanced the partitions are and the depth of the
recursion tree is log(n), so the space complexity is O(log n).

def quicksort(array):
if len(array) <= 1:
return array
else:
pivot = array[0]
left = [x for x in array[1:] if x < pivot]
right = [x for x in array[1:] if x >= pivot]
return quicksort(left) + [pivot]+ quicksort(right)
array = [5, 0, 1, 34, 11, 10, 98 ,2,-1]
print(quicksort(array))

Sorting Algorithm Page 4

You might also like