Kumar-laxmi / Algorithms

A Repository for algorithms in C, C++, Python and Java
Apache License 2.0
322 stars 367 forks source link

Intro Sort in C++, Python, JAVA #1291

Closed anand-harsh closed 1 year ago

anand-harsh commented 1 year ago

πŸ› οΈ Issue (Number)

Issue no #1226

πŸ‘¨β€πŸ’» Changes proposed

βœ”οΈ Check List (Check all the applicable boxes)

πŸ“„ Note to reviewers

Intro sort is a hybrid sorting algorithm that combines quicksort, heapsort, and insertion sort to achieve efficient sorting performance. Here's a brief overview of how it works:

  1. The algorithm starts with quicksort, which selects a pivot element and partitions the array into two sub-arrays.

  2. If the recursion depth exceeds a predetermined threshold, indicating the potential for worst-case behavior, the algorithm switches to an alternative sorting algorithm (typically heapsort).

  3. The alternative sorting algorithm is used to sort the remaining unsorted sub-arrays. Heapsort has a guaranteed worst-case time complexity of O(n log n).

  4. For small sub-arrays, the algorithm may further switch to insertion sort, which is efficient for such cases.

By dynamically adapting between quicksort, heapsort, and insertion sort based on the input data and recursion depth, intro sort achieves a balance between quicksort's average-case performance and the worst-case performance guarantees of alternative sorting algorithms. This makes it a highly efficient sorting algorithm for a wide range of input scenarios.

πŸ“· Screenshots

intro_sort.cpp

image

intro_sort.py

image

intro_sort.java

image

anand-harsh commented 1 year ago

@Kumar-laxmi please assign me this issue, my repository got deleted by mistake and the PR got closed