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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 14, 20262026-05-14T00:13:13+00:00 2026-05-14T00:13:13+00:00

Bubble sort is O(n) at best, O(n^2) at worst, and its memory usage is

  • 0

Bubble sort is O(n) at best, O(n^2) at worst, and its memory usage is O(1) . Merge sort is always O(n log n), but its memory usage is O(n).

Which algorithm we would use to implement a function that takes an array of integers and returns the max integer in the collection, assuming that the length of the array is less than 1000. What if the array length is greater than 1000?

  • 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-05-14T00:13:14+00:00Added an answer on May 14, 2026 at 12:13 am

    A sequential scan of a dataset, looking for a maximum, can be done in O(n) time with O(1) memory usage.

    You just set the current maximum to the first element then run through all the other elements, setting the current maximum to the value if the value is greater. Pseudo-code:

    max = list[first_index]
    for index = first_index+1 to last_index:
        if list[index] > max:
            max = list[index]
    

    The complexity doesn’t change regardless of the number of elements in the list so it doesn’t matter how many there are.


    The running time will change however (since the algorithm is O(n) time) and, if it’s important to find the maximum fast, there are a number of possibilities. These all hinge on doing work when the list changes, not every time you want the information, hence they’re better for a list that is read more often than written, so the cost can be amortised.

    Option 1 is to keep the list sorted so you can just grab the last element. This is probably overkill for just keeping a record of the maximum.

    Option 2 is to recalculate the maximum (and number of elements holding it) when you insert into, or delete from, the list. Initially set max to 0 and maxcount to 0 for an empty list.

    For an insert:

    • if maxcount is 0 (the list is empty), set max to this value and maxcount to 1.
    • otherwise, if this value is greater than max, set max to this value and maxcount to 1.
    • otherwise, if this value is equal to max, add 1 to maxcount.

    For a deletion:

    • if this value is equal to max, subtract 1 from maxcount.
    • then, if maxcount is 0, rescan list to get new max and maxcount.

    This way, at any time, you have the maximum value (the count is simply an extra “trick” to speed up the algorithm where there is more than one element holding the maximum value). I’ve used this once before in a data analysis application and it turned out to be much faster than re-sorting – I had to store both minimum and maximum in that case, but it’s the same idea.

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

Sidebar

Ask A Question

Stats

  • Questions 382k
  • Answers 382k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer I can give u the answer but better you read… May 14, 2026 at 10:33 pm
  • Editorial Team
    Editorial Team added an answer myImage.addEventListener('load', function() { ... }, false); Code inside the above… May 14, 2026 at 10:33 pm
  • Editorial Team
    Editorial Team added an answer I think it (might) be because your view is inheriting… May 14, 2026 at 10:33 pm

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

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.