Docsity

Sorting Algorithms: Big-O Analysis and Comparison, Slides of Data Structures and Algorithms

Partial preview of the text

Download Sorting Algorithms: Big-O Analysis and Comparison and more Slides Data Structures and Algorithms in PDF only on Docsity!

Sorting

Algorithms and Analysis

Refresher on Big-O

  • O(2^N) Exponential
  • O(N^2) Quadratic
  • O(N log N) Linear/Log
  • O(N) Linear
  • O(log N) Log
  • O(1) Constant Hierarchy of Big-O functions from slowest to fastest

O(N log N) vs. O(N^2) 0 5000 10000 15000 20000 0 1000 O(N log N) O(N^2)

Two Common Categories Sorting Algorithms of O(N^2)

  • Bubble Sort
  • Selection Sort
  • Insertion Sort Sorting Algorithms of O(N log N)
  • Heap Sort
  • Merge Sort
  • Quick Sort

O(N^2) Sorts

  • Easy to program
  • Simple to understand
  • Very slow, especially for large values of N
  • Almost never used in professional software

Bubble Sort

  • The most inefficient of the O(n^2) algorithms
  • Simplest sorting algorithm available
  • Works by comparing sequential items, and swapping them if the first one is larger than the second. It makes as many passes through an array as are needed to complete the sort

Bubble Sort – Pass 2

Bubble Sort – Pass 3

Selection Sort

  • More efficient than Bubble Sort, but not as efficient as Insertion Sort
  • Works by finding the largest element in the list and swapping it with the last element, effectively reducing the size of the list by 1.

Selection Sort – Pass 1-

Insertion Sort

  • One of the most efficient of the O(n^2) algorithms
  • Roughly twice as fast as bubble sort
  • Works by taking items from unsorted list and inserting them into the proper place.

Insertion Sort

O(N log N) Sorts

  • Fast
  • Efficient
  • Complicated, not easy to understand
  • Most make extensive use of recursion and complex data structures

Heap Sort

  • Slowest O(N log N) algorithm.
  • Although the slowest of the O(N log N) algorithms, it has less memory demands than Merge and Quick sort.