WGU C949 Data Types STUDY GUIDE
and answers
ArrayA data structure that stores an ordered list of items, with each item is
directly accessible by a positional index.
Linked ListA data structure that stores ordered list of items in nodes, where each
node stores data and has a pointer to ...
WGU C949 - Searching and Sorting
Algorithms answers
Binary search✔✔A faster algorithm for searching a list if the list's elements are
sorted and directly accessible (such as an array). Binary search first checks the
middle element of the list. If the search key is found, the algorithm returns the
matching location. If the search key is not found, the algorithm repeats the search on
the remaining left sublist (if the search key was less than the middle element) or the
remaining right sublist (if the search key was greater than the middle element).
Binary search efficiency✔✔For an N element list, the maximum number of steps
required to reduce the search space to an empty sublist is [ log2 N ] + 1
Selection sort✔✔Sorting algorithm that treats the input as two parts, a sorted part
and an unsorted part, and repeatedly selects the proper next value to move from the
unsorted part to the end of the sorted part.
Selection sort efficiency✔✔If a list has N elements, the outer loop executes N times.
For each of those N outer loop executions, the inner loop executes an average of
N/2 times. So the total number of comparisons is proportional to N * (N/2), or O(N^2)
, ........else:
............return mid
....return -1
if __name__ == "__main__":
....numlist = [ 11, 33, 55, 77, 99 ]
....for val in (11, 22, 55, 88, 99):
........print(f"Index of {val}: {BinarySearch(val, numlist)}")
Insertion sort✔✔The list is split into a sorted half and unsorted half. Values from the
unsorted part are picked and placed at the correct position in the sorted part (left) by
shifting all the elements towards the right (unsorted portion).
Insertion sort efficiency✔✔Time complexity of O(n*2)
Insertion sort (python)✔✔# replace "^\.+" with space
def InsertionSort(numbers):
....for idx in range(1, len(numbers)):
........val = numbers[idx]
........comp = idx-1
........while comp >=0 and val < numbers[comp]:
............numbers[comp+1] = numbers[comp]
............comp -= 1
........numbers[comp+1] = val
if __name__ == "__main__":
....numlist = [ 99, 77, 33, 55, 11 ]
....print("Before: " + str(numlist))
....InsertionSort(numlist)
....print("After: " + str(numlist))
Shell sort✔✔Sorting algorithm that splits the input list into a number of lists based on
the gap value. The number of interleaved lists is equal to the gap value and the
value of interleave_a[0] = input_list[0], interleave_b[0] = input_list[gap value],
interleave_c[0] = input_list[gap value * 2] and so on. The interleaved lists are
individually insertion sorted, merged and insertion sorted to get the final sorted list.
Shell sort efficiency✔✔Worst case efficiency is O(N^2), best case is O(N*log N)
Shell sort (python)✔✔# replace "^\.+" with space
def ShellSort(numbers):
....gap = len(numbers) // 2
....while gap > 0:
........for idx in range(gap, len(numbers)):
............temp = numbers[idx]
............comp = idx
............while comp >= gap and numbers[comp - gap] > temp:
................numbers[comp] = numbers[comp - gap]
................comp = comp - gap
............numbers[comp] = temp
........gap = gap // 2
The benefits of buying summaries with Stuvia:
Guaranteed quality through customer reviews
Stuvia customers have reviewed more than 700,000 summaries. This how you know that you are buying the best documents.
Quick and easy check-out
You can quickly pay through credit card or Stuvia-credit for the summaries. There is no membership needed.
Focus on what matters
Your fellow students write the study notes themselves, which is why the documents are always reliable and up-to-date. This ensures you quickly get to the core!
Frequently asked questions
What do I get when I buy this document?
You get a PDF, available immediately after your purchase. The purchased document is accessible anytime, anywhere and indefinitely through your profile.
Satisfaction guarantee: how does it work?
Our satisfaction guarantee ensures that you always find a study document that suits you well. You fill out a form, and our customer service team takes care of the rest.
Who am I buying these notes from?
Stuvia is a marketplace, so you are not buying this document from us, but from seller WorkAce. Stuvia facilitates payment to the seller.
Will I be stuck with a subscription?
No, you only buy these notes for $7.99. You're not tied to anything after your purchase.