Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8265277
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 8, 20262026-06-08T04:44:36+00:00 2026-06-08T04:44:36+00:00

I have to sort an array. Apart from the already existing types of sorting

  • 0

I have to sort an array. Apart from the already existing types of sorting that exists, I was wondering if an algorithm like this would work out, and what its complexity might be.

I have an array to be sorted. I create a Binary Search Tree.

So if I insert all the elements of the array into the BST, and then assign them back to the array one by one when doing a in-order traversal of the tree, I will achieve a sorted result. (Though consuming more space complexity because of the tree nodes. Not in-place sorting.)

int i=0;

void sort_by_inorder(node *n)
{
    if(n==NULL)
    {
        return;
    }
    sort_by_inorder(n->leftchild);
    array[i++]=n->data;
    sort_by_inorder(n->rightchild);
}

I know a BST does not allow duplicate insertions, so maybe we could consider modifying the BST insertion algorithm into <= for the left sub-tree, or maybe >= for the right sub-tree.

Will this be a good implementation (workable)? What would be the complexity?

Traversal complexity is on an average O(n). And insertion is just O(log n). So this should, according to me, turn out to be an efficient algorithm.

Thanks.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-08T04:44:38+00:00Added an answer on June 8, 2026 at 4:44 am

    In a general binary search tree, the insertion time is actually O(n) in the worst case. You need a balanced tree for the operations to be O(log(n)).

    So in a general BST, your approach allows you to sort in O(n^2).

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have an array MyArrayOfItems of Item objects with objects that look like this:
I have the following sort descriptors that sort an array of my business objects,
Possible Duplicate: php sort array by sub-value I have a multidimensional array like the
I have this code to sort an array of objects. The data in the
I have a homework assignment to sort an array in ascending order. Obviously, this
i have an array that looks like: array(5) { [title]=> string(22) http://example.com/288 [title2]=> string(22)
I have problem with sort array of arrays. I already build method to sort
I have to sort this array in descening order with the key likecount. How
I have an array of numbers jumbled up from 0-9. How do I sort
I have an array of filenames and need to sort that array by the

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.