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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 4, 20262026-06-04T05:56:38+00:00 2026-06-04T05:56:38+00:00

I am trying a simple insertion sort. But the swapping I am trying isn’t

  • 0

I am trying a simple insertion sort. But the swapping I am trying isn’t working when I use bit-wise operation.

But when I use another temporary variable the swapping works. When I compile this code I always get some extra 0s.

How can I figure out what is wrong with my code?

#include<cstdio>
#include<cstdlib>
#include<algorithm>
#include<algorithm>

using namespace std;

void swap(int* a, int* b)
{
*a=*a^*b;
*b=*a^*b;
*a=*a^*b;
}

int main(void)

{
int arr[size];
int i,j,min;

for(i=0; i<size; i++)
    arr[i]=rand()%100;

for(i=0; i<size; i++)
    printf("%d ",arr[i]);
putchar('\n');

for(i=0; i<size;i++)
{
    min=i;
    for(j=i+1;j<size;j++)
        if(arr[j]<arr[min])
            min=j;
    swap(&arr[i],&arr[min]);
}

for(i=0; i<size; i++)
    printf("%d ",arr[i]);
putchar('\n');

return 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-04T05:56:40+00:00Added an answer on June 4, 2026 at 5:56 am

    In

    for(i=0; i<size;i++)
    {
        min=i;
        for(j=i+1;j<size;j++)
            if(arr[j]<arr[min])
                min=j;
        swap(&arr[i],&arr[min]);
    }
    

    if arr[i] is the smallest array element, you try to swap it with itself, so the two pointers you pass to swap point to the same memory location, hence the xor zeros out that location.

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

Sidebar

Related Questions

I'm trying to understand the follow code below: /** * Simple insertion sort. *
Trying to make simple minesweeper game in python, but have one problem. I have
I'm trying to add simple login page to ASP.NET MVC app. I actually use
I'm trying to write simple proxy server for some purpose. In it I use
I'm trying to use Simple Savant within my application, to use SimpleDB I currently
I'm trying to add a new entry into my database, but it's not working.
I am trying to achieve lookup method injection using a simple example. But, seems
So I can't figure out why my insertion operator isn't working for my list
I'm trying to connect to an HTTPS URL, but I need to use client
I'm trying to use jquery with a simple php code: $('#some').click(function() { <?php require_once('some1.php?name=some'

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.