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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 11, 20262026-06-11T08:06:42+00:00 2026-06-11T08:06:42+00:00

I am trying to get the math correct in my algorithm, but I am

  • 0

I am trying to get the math correct in my algorithm, but I am having a really hard time understanding the “Analysis of algorithms” calculating i.e f(n) = 1 or f(n) = n-1 etc..

I have created a method which loops through a sorted array of integers, and counting how many different integers there are (i.e {1,3,3,3,5,6,8} = 5). How do I calculate the worst case scenario?

Basically, the code is like this:

int length = a.length;
int diffCount = 1; //The number of different integers

for(int i = 1; i < length; i++)
{
    int b = a[i];
    int c = a[i-1];

    if(c>b)
         throw new IllegalStateException("unsorted array");

    if(c!=b)
         diffCount++;
}
return diffCount;

There’s some other stuff there aswell, but that’s just to prevent bugs like empty arrays and such, so I didn’t include it.

And what is the worst case scenario here..? If the condition c!=b is true every time?

  • 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-11T08:06:43+00:00Added an answer on June 11, 2026 at 8:06 am

    In terms of run-time analysis, this algorithms is always O(n), if the array is sorted. If not, it will throw an exception before that (but worst case still is O(n)).

    There are slightly different outcomes depending on the values. See Peter’s answer for an explanation of branch prediction. But I don’t think that this will influence the run time significantly.

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

Sidebar

Related Questions

Not the biggest whiz on math here... having a hard time figuring this out.
I'm trying to get back into OpenGL, but my knowledge and math are rusty
I have a basic math question. I am trying to get a percentage from
I'm trying to get the average time a user spends on my site using
I'm currently trying to get something to run involving trigonometry but I've run across
I'm trying to get an intuitive understanding of how much I could speed up
I am trying to get the correct Big-O of the following code snippet: s
I am trying to get some math out of a string, with no decimals
I am trying the atan2 Math Function but i am getting wrong value in
So i am trying get 2 div-containers which both should contain centered text (Both

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.