Docsity

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

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