Skip to content

Conversation

kokatesaurabh
Copy link

This PR adds an implementation of the IntroSort (Introspective Sort) algorithm in C.

IntroSort is a hybrid sorting algorithm that combines QuickSort, HeapSort, and InsertionSort.
It begins with QuickSort, switches to HeapSort when recursion depth exceeds a certain limit,
and uses InsertionSort for small partitions. This ensures both fast average-case performance
and worst-case optimality. IntroSort is the algorithm behind std::sort.

Proposed Changes:

  • Add introsort.c with the full IntroSort implementation.
  • Use QuickSort as the base algorithm.
  • Switch to HeapSort if recursion depth exceeds 2 * log2(n) (bit-hack __builtin_clz used for log2).
  • Use InsertionSort for subarrays smaller than 16 elements.
  • Include helper functions: swap, heapify, printArray, and test cases in main().
  • Safe handling of empty and single-element arrays.
  • Inline comments detailing time and space complexity.

Benefits:

  1. Adds a high-performance, practical sorting algorithm used in real-world systems.
  2. Complements existing sorting algorithm collection in the repository.
  3. Provides a reference implementation for hybrid sorting strategies.
  4. Helps learners understand how QuickSort, HeapSort, and InsertionSort can be combined effectively.

Testing:

  • Verified correctness using multiple test cases: random, sorted, reverse-sorted, duplicate elements, single element, and empty array.
  • Prints original and sorted arrays for demonstration.

This implementation ensures IntroSort is easy to compile and run on any system without additional dependencies (math.h removed; log2 replaced with bit-hack).

Closes #1488.

@kokatesaurabh
Copy link
Author

@Panquesito7 @tjgurwara99 @alexpantyukhin hello can you review my pr!! i would like to contribute in your repository!!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

[FEATURE] Add implementation of Intro Sort
1 participant