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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 16, 20262026-06-16T02:07:48+00:00 2026-06-16T02:07:48+00:00

Following it the code for selection sort and when I try to swap the

  • 0

Following it the code for selection sort and when I try to swap the minimum element with bigger one it doesn’t work using XOR operator.It displays 0 in place of values.But swap is working on two constant integers.Why?

import java.io.*;

class selection {

    public static void main(String s[])throws IOException {
        BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
        System.out.println("Enter n");
        int n=Integer.parseInt(br.readLine());
        System.out.println("Enter array");
        int a[]=new int[n];
        for(int i=0;i<n;++i) {
            a[i]=Integer.parseInt(br.readLine());
        }

        int min;
        System.out.println("Entered Array : ");
        for(int i=0;i<n;++i)
            System.out.print(a[i]+" ");

        for(int i=0;i<n;++i) {
            min=i;
            for(int j=i+1;j<n;++j) {
                if(a[min]>a[j])
                    min=j;
            }
            a[min]=a[min]^a[i];
            a[i]=a[min]^a[i];
            a[min]=a[min]^a[i];

        }

        System.out.println("\nSorted Array : ");
        for(int i=0;i<n;++i) {
            System.out.print(a[i]+" ");
        }
    }
}

OUTPUT is :

Enter n
8
Enter array
1
5
4
6
2
8
9
7
Entered Array : 
1 5 4 6 2 8 9 7 
Sorted Array : 
0 2 0 5 0 7 8 0 
  • 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-16T02:07:49+00:00Added an answer on June 16, 2026 at 2:07 am

    Swapping elements with XOR will not work if the elements are equal and this is explicitly pointed out in most books. What happens here is the following – if the i-th element is the minimal on the i-th iteration you will try to swap it with itself which causes its value to become 0.

    In order to avoid this problem, before swapping numbers using XOR check if thier values are equal. In your case if you are sure the array does not contain equal elements simply do not swap if min==i.

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

Sidebar

Related Questions

I am using following code for selection sort but it always return 0, 0,
I'm using the following code to sort the results in a UIPicker. The results
Given the following code: $(.force-selection).blur(function() { var value = $('matched-item').val(); //check if the input's
I use the following code to crop an image according to a rectangular selection
I have the following code, which I want to load values on a selection
I'm trying to change the current selection of a NSTableView with the following code:
I have the following jquery code: jQuery(function(){ jQuery(select#rooms).change(function(){ var options = ''; jQuery.getJSON(/admin/selection.php,{id: jQuery(this).val(),
I have the following code to sort the items in a dropdown list: function
I have the following code; String[] projection = { MediaStore.Video.VideoColumns._ID, MediaStore.Video.VideoColumns.DATA }; //String selection
I need reverse(sort in descending order) a NSMutablearray.I tried the following code and it

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.