0% found this document useful (0 votes)
5 views3 pages

Algoritmos PDF

The document contains Python implementations of various sorting algorithms including bubble sort, insertion sort, selection sort, quick sort, shell sort, counting sort, merge sort, and bucket sort. Each function sorts an input array and returns the sorted array. Additionally, a sample array is provided to demonstrate the output of each sorting algorithm.

Uploaded by

Melissa Guedes
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)
5 views3 pages

Algoritmos PDF

The document contains Python implementations of various sorting algorithms including bubble sort, insertion sort, selection sort, quick sort, shell sort, counting sort, merge sort, and bucket sort. Each function sorts an input array and returns the sorted array. Additionally, a sample array is provided to demonstrate the output of each sorting algorithm.

Uploaded by

Melissa Guedes
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
You are on page 1/ 3

def bubble_sort(arr):

n = len(arr)
for i in range(n):
for j in range(0, n - i - 1):
if arr[j] > arr[j + 1]:
arr[j], arr[j + 1] = arr[j + 1], arr[j]
return arr

def insertion_sort(arr):
for i in range(1, len(arr)):
key = arr[i]
j = i - 1
while j >= 0 and key < arr[j]:
arr[j + 1] = arr[j]
j -= 1
arr[j + 1] = key
return arr

def selection_sort(arr):
for i in range(len(arr)):
min_idx = i
for j in range(i + 1, len(arr)):
if arr[j] < arr[min_idx]:
min_idx = j
arr[i], arr[min_idx] = arr[min_idx], arr[i]
return arr

def quick_sort(arr):
if len(arr) <= 1:
return arr
pivot = arr[len(arr) // 2]
left = [x for x in arr if x < pivot]
middle = [x for x in arr if x == pivot]
right = [x for x in arr if x > pivot]
return quick_sort(left) + middle + quick_sort(right)

def shell_sort(arr):
n = len(arr)
gap = n // 2
while gap > 0:
for i in range(gap, n):
temp = arr[i]
j = i
while j >= gap and arr[j - gap] > temp:
arr[j] = arr[j - gap]
j -= gap
arr[j] = temp
gap //= 2
return arr

def counting_sort(arr):
if not arr:
return []
max_val = max(arr)
min_val = min(arr)
range_of_elements = max_val - min_val + 1
count = [0] * range_of_elements
output = [0] * len(arr)
for num in arr:
count[num - min_val] += 1
for i in range(1, len(count)):
count[i] += count[i - 1]
for num in reversed(arr):
output[count[num - min_val] - 1] = num
count[num - min_val] -= 1
return output

def merge_sort(arr):
if len(arr) > 1:
mid = len(arr) // 2
left_half = arr[:mid]
right_half = arr[mid:]

merge_sort(left_half)
merge_sort(right_half)

i = j = k = 0

while i < len(left_half) and j < len(right_half):


if left_half[i] < right_half[j]:
arr[k] = left_half[i]
i += 1
else:
arr[k] = right_half[j]
j += 1
k += 1
while i < len(left_half):
arr[k] = left_half[i]
i += 1
k += 1

while j < len(right_half):


arr[k] = right_half[j]
j += 1
k += 1
return arr

def bucket_sort(arr):
if len(arr) == 0:
return arr
bucket_count = 10
max_value = max(arr)
size = (max_value + 1) / bucket_count
buckets = [[] for _ in range(bucket_count)]
for num in arr:
index = int(num // size)
if index != bucket_count:
buckets[index].append(num)
else:
buckets[bucket_count - 1].append(num)
for bucket in buckets:
bucket.sort()
return [num for bucket in buckets for num in bucket]

# Listas escolhidas manualmente para teste


array = [34, 7, 23, 32, 5, 62, 78, 45, 12, 89]

print("Array original:", array)


print("Bubble Sort:", bubble_sort(array[:]))
print("Insertion Sort:", insertion_sort(array[:]))
print("Selection Sort:", selection_sort(array[:]))
print("Quick Sort:", quick_sort(array[:]))
print("Shell Sort:", shell_sort(array[:]))
print("Counting Sort:", counting_sort(array[:]))
print("Merge Sort:", merge_sort(array[:]))
print("Bucket Sort:", bucket_sort(array[:]))

You might also like