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

  • Home
  • SEARCH
  • 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 1107305
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 17, 20262026-05-17T01:55:06+00:00 2026-05-17T01:55:06+00:00

i have written a code for insertion sort but it is nt sorting.. kindly

  • 0

i have written a code for insertion sort but it is nt sorting..
kindly help me out in making the desired changes to mycode.
thanx

#include<stdio.h>

int compare(int a,int b)

{

if(a>b)

return 1;

else if( a<b)

return -1;

else if(a==b)

return 0;

}


void insert(int m, int ms[], int size)

{   

    int j,k;

    for (j=0; j<size; j++)   // Find the right position

       { if (compare(m, ms[j])) break;

    // Assertion: j is the right position for m

          for (k=size-1; k>=j; k--)  // Right shift values>m

       ms[k+1] = ms[k];

    ms[j] = m; } // Insert m

}



void insertSort(int ms[], int size)

{   

    int j;

    // Loop Invariant: 

    //  The sublist from ms[0] to ms[j-1] is sorted

    for (j=1; j<size; j++) {

         insert(ms[j], ms, j);

    }

}



void main()

{

int i,arr[5]={55,66,22,44,39};

printf("sorting\n");

insertSort(arr,5);



for( i=0;i<5;i++)

{

printf("\n%d",arr[i]);

}

}

this is my xact code..
i have to use the two functions insert and insert sort!

  • 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-17T01:55:07+00:00Added an answer on May 17, 2026 at 1:55 am

    You’re testing the return value of compare incorrectly. You probably want something like this:

    if (compare(m, ms[j]) <= 0) break;
    

    Also, remove the curly braces inside function insert, they are screwing the structure of your code. The statements following the break should not be inside the first loop’s body.

    By the way, if you make your code simpler and tidier, you’ll be less likely to make silly mistakes. Proper indentation will help you visualize the structure of your code easily. For example:

    void insort(int a[], int n, int e) {
        int j;
        for (j = n - 1; j >= 0 && a[j] > e; j--)
            a[j + 1] = a[j];
        a[j + 1] = e;
    }
    void insertion_sort(int a[], int n) { 
        int i;
        for (i = 1; i < n; i++)
            insort(a, i, a[i]);
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Ask A Question

Stats

  • Questions 544k
  • Answers 544k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer If you've got 25,000 text files on disk, 'you're doing… May 17, 2026 at 6:59 am
  • Editorial Team
    Editorial Team added an answer Perhaps it's your browser's DOM parser that is putting the… May 17, 2026 at 6:59 am
  • Editorial Team
    Editorial Team added an answer setInterval(function(){ var now = new Date(); $('#yourSpanId').text(now.toUTCString()); // or whatever… May 17, 2026 at 6:59 am

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

Related Questions

I have written code that automatically creates CSS sprites based on the IMG tags
I have written some code in my VB.NET application to send an HTML e-mail
I have written jQuery code, in files Main.html and ajax.php . The ajax.php file
I have written a few modules of code in Access vba. Each code runs
Here's a snippet of code from a shell script I have written: for src
Put it another way: what code have you written that cannot fail. I'm interested
I have just been re-working an old bit of compiler-like code written using bison.
I have a code snippet written in PHP that pulls a block of text
I have some code I've written in PHP for consuming our simple webservice, which
I have the source code of an application written in C++ and I just

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.