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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T10:35:49+00:00 2026-05-26T10:35:49+00:00

I created a simple bubblesorting script here that takes in arrays and sorts them,

  • 0

I created a simple bubblesorting script here that takes in arrays and sorts them, This is only a snippet of the code. But its the code that sorts it. I want to make it so Instead of making nine or so comparisons for example on every pass, to modify the bubble sort to make eight or one less comparisons on the second pass, seven on the third pass, and so on. I am totally lost how to implement that. What would be the best idea for that?

        int bubbleSortArray(int array[])
        {
            for(int i=0;i<10;i++)
            {
                for(int j=0;j<i;j++)
                {
                    if(array[i]>array[j])
                    {
                        swap(array[i], array[j]);
                        amountOfSwaps += 1;
                    }
                }
            }

        printArray(array);
        return amountOfSwaps;

        }


       void swap(int & value1, int & value2)
       {
              int temp=value1; 
              value1=value2;
              value2=temp;
       }
  • 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-26T10:35:49+00:00Added an answer on May 26, 2026 at 10:35 am

    Your code is already doing what you are looking for. Since j iterates to the length i, it grows by one larger each time. I think you are being confused because your code is actually implementing it backwards from your English in the question 😉

    Here is a sample array, and the modifications that would be made at each iteration. The parenthesis denote what part of the array is being checked in each iteration:

    (7)5 3 8 6 9 4 2 0 1
    (7 5)3 8 6 9 4 2 0 1
    (7 5 3)8 6 9 4 2 0 1
    (8 7 5 3)6 9 4 2 0 1
    (8 7 6 5 3)9 4 2 0 1
    (9 8 7 6 5 3)4 2 0 1
    (9 8 7 6 5 4 3)2 0 1
    (9 8 7 6 5 4 3 2)0 1
    (9 8 7 6 5 4 3 2 0)1
    (9 8 7 6 5 4 3 2 1 0)
    

    As you can see the first time through nothing is actually done, nor ever will be because you are comparing one element against itself. The second time through you are now comparing two elements, then three, then so on.

    To make your code start with comparing them all, and then doing one less each time (as your question states) you would modify you loop to the following (note j<10-i):

        for(int i=0;i<10;i++)
        {
            for(int j=0;j<10-i;j++)
            {
    

    Either way it amounts to the same thing and will work in the end. You could further skip the first comparison against itself by setting i = 1:

    for(int i=1;i<10;i++)
        {
            for(int j=0;j<10-i;j++)
            {
    

    This will leave off the first comparison above, which is the other optimization you were looking for.

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

Sidebar

Related Questions

I created simple code in MVS2010 but it doesn't work. There is just a
I have created simple script: #!/bin/sh column=${1:-1} awk '{colawk='$column'+1; print colawk}' But when I
Ok I created simple user control that look like this <%@ Control Language=C# AutoEventWireup=true
Could someone help me on this, I have created simple web services using axis2
Have created simple Ajax enabled contact forms before that have around 12 fields -
I created simple modal view photo gallery that gets picture where user clicked and
I created simple REST web app that would create Excel file and put into
I created a simple, stripped ListView style that highlights an element when the IsMouseOver
I created simple web service with Jersey that reads in an XML file and
I have created simple chat application using smack api.i can able to chat well.but

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.