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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 16, 20262026-06-16T10:00:44+00:00 2026-06-16T10:00:44+00:00

So I have tried to make this algorithm to work but everytime I run

  • 0

So I have tried to make this algorithm to work but everytime I run it I get [10,11,7,10,7,5,7,5 ] as my output. The goal was to get results in descending order. Furthermore I do not understand why there are 8 results when my array only contains 6 values. Please Help.. Thank you.
public class searches {

public static void main (String[]args)
{
    int[] array = {10,7,11,5,13,8};
    //ExchangeSort(array);

};

public static void ExchangeSort(int[] num)
{
    int i,j,temp;

    for(i=1;i<num.length-1;i++)
    {
        for(j=i+1;j<num.length;j++)
        {
            if(num[i]<num[j])
            {
                temp = num[i];
                num[i] = num[j];
                num[j] = temp;

                //System.out.println(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-06-16T10:00:46+00:00Added an answer on June 16, 2026 at 10:00 am

    You are ignoring first element in sorting as array index start from 0. Your first loop should start with index 0 and go up to length-1 as:

         for(i=0;i<num.length-1;i++)
    

    Also you need to print the sorted array outside the loops or in the main method using Arrays.toString (no loop required) as:

         // this will print the array elements
         System.out.println("Sorted Array: "+Arrays.toString(num));
    

    and remove the semicolons ; from the end of the methods.

    Observatory Note: You method name shouldn’t start with Upper Case letter.

    EDIT: Your full corrected code is as below:

    public static void main(String[] args) {
        int[] array = {10,7,11,5,13,8};
        exchangeSort(array);
        System.out.println("Sorted Array: "+Arrays.toString(array));
    }
    public static void exchangeSort(int[] num) {
        int temp;
        for(int i=0;i<num.length-1;i++){
            for(int j=i+1;j<num.length;j++){
                if(num[i]<num[j]){
                    temp = num[i];
                    num[i] = num[j];
                    num[j] = temp;
                }
            }
        }
        System.out.println("Sorted Array: "+Arrays.toString(num));
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

This might get a little confusing as I have tried everything to make this
How to make a sticky footer i have tried on google found some results
I tried to make admin panel and I am using sessions , but have
I have tried searching over the internet about this problem but not able to
I have tried to make the fullscreen feature of a SDI application with splitter
How do I make the following url forbidden in apache; main/index.php?site=ing I have tried
Have tried to find solutions for this and can't really come up with anything.
I need to work out a decryption/encryption algorithm, but I'm confused regarding SHA256 /
In an attempt to rewrite PHP's similar_text algorithm I have tried a few different
I have a brute force algorithm, but never fully understood it. I have a

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.