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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 11, 20262026-06-11T15:51:51+00:00 2026-06-11T15:51:51+00:00

I am working on a homework assignment in which I am required to find

  • 0

I am working on a homework assignment in which I am required to find the number of comparisons made by a modified bubble sort on a data set of size n. The data set to be considered is a sorted list where the first and last elements are swapped for example: 52341. Below is the pseudocode for the algorithm:

i <- n-1; new_i <- i
while i > 0 do 
    for j=1 to i do
        if A[j] > A[j+1] do
            A[j] <=> A[j+1]
            new_i <- j
        endif
    endfor
    i <- new_i - 1; new_i <- i
endwhile

A is the data set and <=> is a swap.

I am trying to find a way to represent this algorithm with summations to be simplified into an expression of the amount of comparisons on the given type of data set.

Without giving the answer, can anybody push me in the right direction?

  • 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-11T15:51:53+00:00Added an answer on June 11, 2026 at 3:51 pm

    In your Bubblesort implementation, the number of comparisons is completely determined alone by the size of the input, i.e. the order of the elements in the input list doesn’t matter.

    If you can prove this point (which is not very hard), it suffices to count the number of comparisons for an input list of size N. Since we have already shown that order doesn’t matter, this is again fairly easy:

    We have two loops, the outer while loop and the inner for loop. The outer loop goes from n to 1 (or n-1 to 0, but certainly not from n-1 to 1, as suggested by your code), i.e. we have N iterations. The inner loop goes from 1 to i.

    Hence we have N comparisons in the first iteration of the outer loop, N-1 in the second, N-2 in the third, …, 0 in the N-N’th.

    I presume that you came this far by yourself. Otherwise, you’re screwed anyway.

    What your teacher now wants to see is that you know the following formula, the so called “Gaußsche Summenformel,” also known as “Gauss Sum”:

    N + (N-1) + (N-2) + … + (N-N+1) + (N-N) = N^2/2 + N/2

    So what you’re supposed to do here is:

    • show that the number of comparisons only depends on the size of the input
    • show that the number of comparisons equals N + (N-1) + (N-2) + … + (N-N+1) + (N-N)
    • mention that this is the same as N^2/2 + N/2, referring to Mr. Gauß.

    😉

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

Sidebar

Related Questions

I'm working on a homework assignment in which I'm required to use char arrays
I'm working on a homework assignment (a project), for which one criterion is that
I'm currently working on a homework assignment where I need to set up a
I am working on a homework assignment that deals with binary search trees and
I am working on this homework assignment and I am stuck on what I
I am working on a homework assignment looking at inheritance in java. I am
For a homework assignment, I'm working with the following. It's an assigned class structure,
I'm working on a small homework assignment and I'm supposed to make a food
I am working on a homework assignment and I'm trying to figure out a
I'm working on a homework assignment and I ran into a little snag. I'm

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.