Sorting algorithms Flashcards
(9 cards)
Explain a characteristic of merge sort
Efficient so works well for large data sets
Detail how merge sort works.
The data set is repeatedly split in half until each item is in its own list. Items in adjacent lists are compared and placed into a new list in order. Compare adjacent lists again, from the bottom up, creating new ordered lists, until there is only 1 list left.
How do you identify merge sort code?
It’s bloody massive and there is no for loop
Describe the characteristics of an insertion sort.
Not the most efficient; useful for small data sets and inserting items into an already sorted list.
Detail how an insertion sort works.
Insert the first item into a new list. Insert the next item; compare it to the first item, and swap if needed. Each new item is compared to the item below it and swapped if needed until no swaps are needed.
How do you identify insertion sort code?
While loop inside the for loop
Describe the characteristics of bubble sort
The least efficient method, but very easy to implement. Good for small data sets.
Detail the steps of bubble sort.
Compare the first, and swap if needed. Compare items 2 and 3 and swap if needed, and 3 and 4, and so on. When you get to the end, go back to start and go through the process again. Once you go all the way through the list with no swaps, the sort is done.
How do you identify bubble sort code?
Has a ‘swapped’ variable. For loop is within the while loop.