Sorting Algorithms: Selection, Bubble, Insertion, Merge, Quick, and Counting Sort, Lecture notes of Data Structures and Algorithms

An overview of various sorting algorithms, including selection sort, bubble sort, insertion sort, merge sort, quicksort, and counting sort. It covers the definition of a sorting algorithm, the importance of efficient sorting, and the classification of sorting algorithms based on stability, memory usage, and sorting method. The document also includes visualizations and code examples for each sorting algorithm.

Typology: Lecture notes

2019/2020

Uploaded on 08/19/2022

huy-cung
huy-cung 🇻🇳

5

(1)

32 documents

1 / 81

Toggle sidebar

This page cannot be seen from the preview

Don't miss anything!

bg1
Data Structures and
Algorithms
LECTURE 05: SORTING ALGORITHMS
pf3
pf4
pf5
pf8
pf9
pfa
pfd
pfe
pff
pf12
pf13
pf14
pf15
pf16
pf17
pf18
pf19
pf1a
pf1b
pf1c
pf1d
pf1e
pf1f
pf20
pf21
pf22
pf23
pf24
pf25
pf26
pf27
pf28
pf29
pf2a
pf2b
pf2c
pf2d
pf2e
pf2f
pf30
pf31
pf32
pf33
pf34
pf35
pf36
pf37
pf38
pf39
pf3a
pf3b
pf3c
pf3d
pf3e
pf3f
pf40
pf41
pf42
pf43
pf44
pf45
pf46
pf47
pf48
pf49
pf4a
pf4b
pf4c
pf4d
pf4e
pf4f
pf50
pf51

Partial preview of the text

Download Sorting Algorithms: Selection, Bubble, Insertion, Merge, Quick, and Counting Sort and more Lecture notes Data Structures and Algorithms in PDF only on Docsity!

Data Structures and

Algorithms

LECTURE 05: SORTING ALGORITHMS

  • Simple Sorting Algorithms
    • Selection and Bubble Sort
  • Shuffling
  • Advanced Sorting Algorithms
    • QuickSort, MergeSort, BucketSort

Contents

  • Efficient sorting algorithms are important for:
    • Producing human-readable output
    • Canonicalizing data – making data uniquely arranged
    • In conjunction with other algorithms, like binary searching
  • Example of sorting:

Sorting – Example

10 3 7 3 4 Unsorted list 3 3 4 7 10 Sorted list

  • Sorting algorithms are often classified by:
    • Computational complexity and memory usage
      • Worst, average and best case behavior
    • Recursive / non-recursive
    • Stability – stable / unstable
    • Comparison-based sort / non-comparison based
    • Sorting method : insertion, exchange (bubble sort and quicksort), selection (heapsort), merging, serial / parallel, etc. Sorting Algorithms: Classification
  • Selection sort – simple, but inefficient algorithm
    • Swap the first with the min element on the right, then the second, etc.
    • Memory: O(1)
    • Stable: No
    • Method: Selection

Selection Sort

Selection Sort Visualization Index Min

Selection Sort Visualization Index Min

Selection Sort Visualization Index Min

Selection Sort Visualization Index Min

Selection Sort Visualization Index Min

Selection Sort Visualization Index Min

Selection Sort Visualization

Selection Sort Code

for (int index = 0; index < arr.length; index++) { int min = index; for (int curr = index + 1; curr < arr.length; curr++) { if (arr[curr] < arr[min]) { min = curr; } } swap(arr, index, min); }

Comparison of Sorting Algorithms Name Best Average Worst Memory Stable Method Selection n 2 n 2 n 2 1 No Selection