C949 - Sorting Algorithms

studied byStudied by 0 people
0.0(0)
Get a hint
Hint

Quick Sort Algorithm

1 / 27

encourage image

There's no tags or description

Looks like no one added any tags here yet for you.

28 Terms

1

Quick Sort Algorithm

def partition(numbers, i, k):
# Pick middle value as pivot
Midpoint = i + (k - i) // 2
Pivot = numbers[midpoint]

# Initialize variables
done = False
l = i
h = k
while not done:
# Increment l while numbers[l] < pivot
while numbers[l] < pivot:
l = l + 1
# Decrement h while pivot < numbers[h]
while pivot < numbers[h]:
h = h - 1
# If there are zero or one items remaining,
# all numbers are partitioned. Return h
if l >= h:
done = True
else:
# Swap numbers[l] and numbers[h],
# update l and h
temp = numbers[l]
numbers[l] = numbers[h]
numbers[h] = temp
l = l + 1
h = h - 1
return h

New cards
2

Selection Sort

a sorting algorithm that treats the input as two parts, sorted and unsorted, and repeatedly selects the proper next value to move from the unsorted part to the end of the sorted part.

New cards
3

Selection Sort Algorithm

for i in range(len(numbers) - 1):
# Find index of smallest remaining element
index_smallest = i
for j in range(i + 1, len(numbers)):
if numbers[j] < numbers[index_smallest]:
index_smallest = j

# Swap numbers[i] and numbers[index_smallest]
temp = numbers[i]
numbers[i] = numbers[index_smallest]
numbers[index_smallest] = temp

New cards
4

Insertion Sort

a sorting algorithm that treats the input as two parts, sorted and unsorted, and repeatedly inserts the next value from the unsorted part into the correct location in the sorted part.

New cards
5

Insertion Sort Algorithm

for i in range(1, len(numbers)):
j = i
# Insert numbers[i] into sorted part
# stopping once numbers[i] in correct position
while j > 0 and numbers[j] < numbers[j - 1]:
# Swap numbers[j] and numbers[j - 1]
temp = numbers[j]
numbers[j] = numbers[j - 1]
numbers[j - 1] = temp
j = j - 1

New cards
6

Quick Sort

a sorting algorithm that repeatedly partitions the input into low and high parts (each unsorted), and then recursively sorts each of those parts.

New cards
7

Merge Sort

a sorting algorithm that divides a list into two halves, recursively sorts each half, and then merges the sorted halves to produce a sorted list.

New cards
8

Radix Sort

a sorting algorithm designed specifically for integers. The algorithm makes use of a concept called buckets and is a type of bucket sort.

New cards
9

Bubble Sort Algorithm

BubbleSort(numbers, numbersSize) {
for (i = 0; i < numbersSize - 1; i++) {
for (j = 0; j < numbersSize - i - 1; j++) {
if (numbers[j] > numbers[j+1]) {
temp = numbers[j]
numbers[j] = numbers[j + 1]
numbers[j + 1] = temp
}
}
}
}

New cards
10

Heap Sort Algorithm

Heapsort(numbers, numbersSize) {
// Heapify numbers array
for (i = numbersSize / 2 - 1; i >= 0; i--) {
MaxHeapPercolateDown(i, numbers, numbersSize)
}
for (i = numbersSize - 1; i > 0; i--) {
Swap numbers[0] and numbers[i]
MaxHeapPercolateDown(0, numbers, i)
}
}

New cards
11

Bucket Sort

a numerical sorting algorithm that distributes numbers into buckets, sorts each bucket with an additional sorting algorithm, and then concatenates buckets together to build the sorted result.

New cards
12

Pre-Order Traversal

In this traversal method, the root node is visited first, then the left subtree and finally the right subtree.
If a binary tree is traversed in this method, it can be used to duplicate the tree.

New cards
13

Bubble Sort, Selection Sort, Insertion Sort Time Complexity

Average = Θ(n^2 )
Big-O worst = O(n^2 )
Fast = No

New cards
14

Merge Sort Algorithm

def merge(numbers, i, j, k):
# Create temporary list merged_numbers
# Initialize position variables
# Add smallest element to merged numbers
while left_pos <= j and right_pos <= k:
if numbers[left_pos] <= numbers[right_pos]:
merged_numbers[merge_pos] = numbers[left_pos]
left_pos = left_pos + 1
else:
merged_numbers[merge_pos] = numbers[right_pos]
right_pos = right_pos + 1
merge_pos = merge_pos + 1
# If left partition not empty, add remaining elements
while left_pos <= j:
merged_numbers[merge_pos] = numbers[left_pos]
left_pos = left_pos + 1
merge_pos = merge_pos + 1
# If right partition not empty, add remaining elements
while right_pos <= k:
merged_numbers[merge_pos] = numbers[right_pos]
right_pos = right_pos + 1
merge_pos = merge_pos + 1
# Copy merge number back to numbers
for merge_pos in range(merged_size):
numbers[i + merge_pos] = merged_numbers[merge_pos]

New cards
15

Radix Sort Algorithm

RadixSort(array, arraySize) {
buckets = create array of 10 buckets
// Find the max length, in number of digits
maxDigits = RadixGetMaxLength(array, arraySize)

// Start with the least significant digit
pow10 = 1
for (digitIndex = 0; digitIndex < maxDigits; digitIndex++) {
for (i = 0; i < arraySize; i++) {
bucketIndex = abs(array[i] / pow10) % 10
Append array[i] to buckets[bucketIndex]
}
arrayIndex = 0
for (i = 0; i < 10; i++) {
for (j = 0; j < buckets[i]⇢size(); j++)
array[arrayIndex++] = buckets[i][j]
}
pow10 = 10 * pow10
Clear all buckets
}
}

New cards
16

Shell Sort

a sorting algorithm that treats the input as a collection of interleaved lists, and sorts each list individually with a variant of the insertion sort algorithm. Uses gap values to determine the number of interleaved lists.

New cards
17

Shell Sort Algorithm

def insertion_sort_interleaved(numbers, start_index, gap):
for i in range(start_index + gap, len(numbers), gap):
j = i
while (j - gap >= start_index) and (numbers[j] < numbers[j - gap]):
temp = numbers[j]
numbers[j] = numbers[j - gap]
numbers[j - gap] = temp
j = j - gap

New cards
18

Bubble Sort

a sorting algorithm that iterates through a list, comparing and swapping adjacent elements if the second element is less than the first element. Uses nested loops.

New cards
19

Quickselect

an algorithm that selects the k^th smallest element in a list. Ex: Running on the list (15, 73, 5, 88, 9) with k = 0, returns the smallest element in the list, or 5.

New cards
20

Quickselect algorithm

def quickselect(numbers, start_index, end_index, k):
if start_index >= end_index:
return numbers[start_index]
low_last_index = partition(numbers, start_index, end_index)
if k <= low_last_index:
return quickselect(numbers, start_index, low_last_index, k)

return quickselect(numbers, low_last_index + 1, end_index, k)

New cards
21

Heap Sort

a sorting algorithm that takes advantage of a max-heap's properties by repeatedly removing the max and building a sorted array in reverse order.

New cards
22

Bucket Sort Algorithm

BucketSort(numbers, numbersSize, bucketCount) {
if (numbersSize < 1)
return
buckets = Create list of bucketCount buckets
// Find the maximum value
maxValue = numbers[0]
for (i = 1; i < numbersSize; i++) {
if (numbers[i] > maxValue)
maxValue = numbers[i]
}
// Put each number in a bucket
for each (number in numbers) {
index = floor(number * bucketCount / (maxValue + 1))
Append number to buckets[index]
}
// Sort each bucket
for each (bucket in buckets)
Sort(bucket)
// Combine all buckets back into numbers list
result = Concatenate all buckets together
Copy result to numbers
}

New cards
23

Post-order Traversal

In this traversal method, the root node is visited last, hence the name. First we traverse the left subtree, then the right subtree and finally the root node.
If a binary tree is traversed in this method, the tree can be deleted.

New cards
24

In-order Traversal

In this traversal method, the left subtree is visited first, then the root and later the right sub-tree. We should always remember that every node may represent a subtree itself.
If a binary tree is traversed in this method, the output will produce sorted key values in an ascending order.

New cards
25

Quick Sort Time Complexity

Average = Θ(n log(n))
Big-O worst = O(n^2 )
Fast = Yes

New cards
26

Bucket Sort Time Complexity

Average = Θ(n)
Big-O worst = O(n^2 )
Fast = Yes

New cards
27

Heap Sort, Merge Sort Time Complexity

Average = Θ(n log(n))
Big-O worst = O(n log(n))
Fast = Yes

New cards
28

Radix Sort Time Complexity

Average = Θ(n)
Big-O worst = O(n)
Fast = Yes

New cards

Explore top notes

note Note
studied byStudied by 16 people
Updated ... ago
5.0 Stars(1)
note Note
studied byStudied by 3 people
Updated ... ago
5.0 Stars(1)
note Note
studied byStudied by 11 people
Updated ... ago
5.0 Stars(2)
note Note
studied byStudied by 7775 people
Updated ... ago
4.8 Stars(52)
note Note
studied byStudied by 33 people
Updated ... ago
5.0 Stars(1)
note Note
studied byStudied by 39 people
Updated ... ago
5.0 Stars(2)
note Note
studied byStudied by 11 people
Updated ... ago
5.0 Stars(1)
note Note
studied byStudied by 11 people
Updated ... ago
5.0 Stars(1)

Explore top flashcards

flashcards Flashcard37 terms
studied byStudied by 17 people
Updated ... ago
5.0 Stars(1)
flashcards Flashcard57 terms
studied byStudied by 3 people
Updated ... ago
5.0 Stars(1)
flashcards Flashcard56 terms
studied byStudied by 5 people
Updated ... ago
5.0 Stars(1)
flashcards Flashcard23 terms
studied byStudied by 17 people
Updated ... ago
4.0 Stars(1)
flashcards Flashcard110 terms
studied byStudied by 8 people
Updated ... ago
5.0 Stars(1)
flashcards Flashcard26 terms
studied byStudied by 7 people
Updated ... ago
5.0 Stars(1)
flashcards Flashcard95 terms
studied byStudied by 10 people
Updated ... ago
4.0 Stars(1)
flashcards Flashcard89 terms
studied byStudied by 71 people
Updated ... ago
5.0 Stars(1)