Tree sort is a sorting algorithm that is based on Binary Search Tree data structure. It first creates a binary search tree from the elements of the input list or array and then performs an in-order traversal on the created binary search tree to get the elements in sorted order.
Step 1: Take the elements input in an array. Step 2: Create a Binary search tree by inserting data items from the array into the binary search tree. Step 3: Perform in-order traversal on the tree to get the elements in sorted order.
2 4 5 7 11
Average Case Time Complexity : O(n log n) Adding one item to a Binary Search tree on average takes O(log n) time. Therefore, adding n items will take O(n log n) time
Worst Case Time Complexity : O(n2). The worst case time complexity of Tree Sort can be improved by using a self-balancing binary search tree like Red Black Tree, AVL Tree. Using self-balancing binary tree Tree Sort will take O(n log n) time to sort the array in worst case.
Auxiliary Space : O(n)
This article is contributed by Harsh Agarwal. If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to firstname.lastname@example.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above.
Don’t stop now and take your learning to the next level. Learn all the important concepts of Data Structures and Algorithms with the help of the most trusted course: DSA Self Paced. Become industry ready at a student-friendly price.
- Comparisons involved in Modified Quicksort Using Merge Sort Tree
- Count of distinct numbers in an Array in a range for Online Queries using Merge Sort Tree
- Comparison among Bubble Sort, Selection Sort and Insertion Sort
- Why Quick Sort preferred for Arrays and Merge Sort for Linked Lists?
- Add elements in start to sort the array | Variation of Stalin Sort
- Program to sort an array of strings using Selection Sort
- Bucket Sort To Sort an Array with Negative Numbers
- Insertion sort to sort even and odd positioned elements in different orders
- Serial Sort v/s Parallel Sort in Java
- Sort an Array which contain 1 to N values in O(N) using Cycle Sort
- Odd Even Transposition Sort / Brick Sort using pthreads
- Java Program for Odd-Even Sort / Brick Sort
- Merge Sort vs. Insertion Sort
- Quick Sort vs Merge Sort
- C/C++ Program for Odd-Even Sort / Brick Sort
- sort() vs. partial_sort() vs. nth_element() + sort() in C++ STL
- Odd-Even Sort / Brick Sort
- Check if a given Binary Tree is height balanced like a Red-Black Tree
- Sort all even numbers in ascending order and then sort all odd numbers in descending order
- Minimum swap required to convert binary tree to binary search tree