Skip to content

[FEATURE] Add implementation of IntroSort in C - #1489

Closed
kokatesaurabh wants to merge 1 commit into
TheAlgorithms:masterfrom
kokatesaurabh:master
Closed

kokatesaurabh wants to merge 1 commit into
TheAlgorithms:masterfrom
kokatesaurabh:master

Conversation

@kokatesaurabh

Copy link
Copy Markdown

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
Copy Markdown
Author

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

@github-actions

github-actions Bot commented Nov 5, 2025

Copy link
Copy Markdown
Contributor

This pull request has been automatically marked as abandoned because it has not had recent activity. It will be closed if no further activity occurs. Thank you for your contributions.

@github-actions github-actions Bot added the Stale label Nov 5, 2025
@github-actions

Copy link
Copy Markdown
Contributor

Please ping one of the maintainers once you commit the changes requested or make improvements on the code. If this is not the case and you need some help, feel free to ask for help in our Gitter channel or our Discord server. Thank you for your contributions!

@github-actions github-actions Bot closed this Nov 13, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[FEATURE] Add implementation of Intro Sort

1 participant