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 8559439
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 11, 20262026-06-11T16:02:48+00:00 2026-06-11T16:02:48+00:00

I want to write a list-lie class in python, that automatically sorts itself when

  • 0

I want to write a list-lie class in python, that automatically sorts itself when numbers are added. This is for fun, not for a class :p What I’m wondering is what’s the most efficient way to go about doing this. Should I just do something like this? :

class SortedList:
    def __init__(self, data):
        self.data = list(data)

    def add(self, num):
        self.data.append(num)
        self.data.sort()

Is there a better way of doing this?

  • 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-11T16:02:50+00:00Added an answer on June 11, 2026 at 4:02 pm

    -Edit-
    sort() is O(n log n). Adding n numbers to the list by sorting after each number added will be O(n^2 log n) (but in practice this may turn out to be faster than O(n^2) algorithms, depending on the optimization of the sort).

    A better O implementation is taking O(n) time to search for the insertion position and O(n) time to add a new number to the list (total time still O(n)). Adding n numbers to the list will be O(n^2). Code follows:

    def addFaster(self, num):
        index = 0
        if(len(self.data) == 0):
            self.data = [num]
        else:
            for i in range(0, len(self.data)):
            if(self.data[i] >= num):
                self.data.insert(i, num)
                return
            self.data.append(num)
    

    We can do better by binary searching our way to the right index in O(log n) time. Adding a new number to a list is O(n). Since this entire operation is (a better than before) O(n), adding n numbers to the list is O(n^2) with a lower constant. (Code not included)

    If you were to add new numbers in large groups, however, it will speed up the algorithm considerably if you add them all at once (use extend()) and then sort. Then, adding n numbers will be done in O(n log n) time (same time as sorting).

    See http://wiki.python.org/moin/TimeComplexity

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

Sidebar

Related Questions

I want to write a class that subclasses the Python list and notifies a
I want to write a function that takes a list of numbers and returns
I want to write a class which provides basically a list of entries. This
I want to write an extension method for the List class that takes an
I want to write this program to find a keyword in a list. If
I want to write a list to a text file, preserving the names. This
I want to write a Makefile that reads a file list.txt and produces result.tar
I want to write a C++ function that can give me a list of
I want to write a function that takes items from a list and groups
I am writing my testcode and I do not want wo write: List<string> nameslist

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.