2.3.3 sorting algorithms

0.0(0)
studied byStudied by 0 people
learnLearn
examPractice Test
spaced repetitionSpaced Repetition
heart puzzleMatch
flashcardsFlashcards
Card Sorting

1/14

encourage image

There's no tags or description

Looks like no tags are added yet.

Study Analytics
Name
Mastery
Learn
Test
Matching
Spaced

No study sessions yet.

15 Terms

1
New cards

big 0 cheat sheet

knowt flashcard image
2
New cards

how does a bubble sort work

  • start at the beginning of the list

  • compare the current item with the next one

  • swap if they’re in the wrong order

  • repeat until the largest item "bubbles up" to the end

  • repeat the process for the remaining unsorted part

<ul><li><p>start at the beginning of the list</p></li><li><p>compare the current item with the next one</p></li><li><p>swap if they’re in the wrong order</p></li><li><p>repeat until the largest item "bubbles up" to the end</p></li><li><p>repeat the process for the remaining unsorted part</p></li></ul><p></p>
3
New cards

bubble sort complexities

  • time = O(n²)

  • space O(1)

  • inefficient but easy to implement, so good for small data sets

4
New cards

how does an insertion sort work

  • mark the first element in the array as sorted

  • compare the first element in the unsored part of the array with items before it

  • shift elements to the right until you find the correct spot

  • insert the element into the sorted list

  • repeat for all remaining elements

<ul><li><p>mark the first element in the array as sorted</p></li><li><p>compare the first element in the unsored part of the array with items before it</p></li><li><p>shift elements to the right until you find the correct spot</p></li><li><p>insert the element into the sorted list</p></li><li><p>repeat for all remaining elements</p></li></ul><p></p>
5
New cards

insertion sort complexities

  • time = O(n²)

  • space = O(1)

  • useful for small data sets

  • inefficient for large data sets

6
New cards

how does a merge sort work

  • divide and conquer

  • deconstruction and reconstruction

  • continuously split the array in half (recursion) until each sub-lists contains only 1 item

  • compare the individual items

  • merge them into temporary arrays with the smallest item going at the start

  • continuously merge the smaller arrays into a larger one, inserting items in the correct order

<ul><li><p>divide and conquer</p></li><li><p>deconstruction and reconstruction</p></li><li><p>continuously split the array in half (recursion) until each sub-lists contains only 1 item</p></li><li><p>compare the individual items</p></li><li><p>merge them into temporary arrays with the smallest item going at the start</p></li><li><p>continuously merge the smaller arrays into a larger one, inserting items in the correct order</p></li></ul><p></p>
7
New cards

merge sort complexities

  • time = O(nlogn) takes same amount of time even if elements are ordered

  • space = O(n)

  • suitable for all data sets but works better for large data sets where memory is not a concern

  • idea for parallel processing environments where the concept of divide and conquer can be used

  • hard to code

8
New cards

how does a quick sort work

  • pick a pivot element from the list

  • the pivot will be used to split the list

  • go through the list and put

    • smaller values on the left

    • larger values on the right

  • repeat quick sort on the left and right sub-lists

  • the right element becomes the pivot for that sub-list

  • the final sorted list is the sorted left + pivot + sorted right

<ul><li><p>pick a pivot element from the list</p></li><li><p>the pivot will be used to split the list</p></li><li><p>go through the list and put</p><ul><li><p>smaller values on the left</p></li><li><p>larger values on the right</p></li></ul></li><li><p>repeat quick sort on the left and right sub-lists</p></li><li><p>the right element becomes the pivot for that sub-list</p></li><li><p>the final sorted list is the sorted left + pivot + sorted right</p></li></ul><p></p>
9
New cards

quick sort complexities

  • time, worst case = O(n²)

  • space = O(1)

  • fast as it uses divide and conquer

  • more efficient than merge sort as it uses less memory

  • suitable for any data set but better with larger ones

  • ideal for parallel processing environments where the concept of divide and conquer can be used

10
New cards

bubble sort in pseudocode

knowt flashcard image
11
New cards

bubble sort in python

knowt flashcard image
12
New cards

insertion sort in pseudocode

knowt flashcard image
13
New cards

insertion sort in python

knowt flashcard image
14
New cards

merge sort stages when programming

knowt flashcard image
15
New cards

quick sort stages when programming

knowt flashcard image