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

Related Questions

I have written some code for displaying a drop down list, but the code
I have written code that opens 16 figures at once. Currently, they all open
I have written code in Java to access web cam,and to save image... I
I have written code to perform a function that could take a while to
Hi I have written code like this @Id @Column(nullable=false) @GeneratedValue(strategy=GenerationType.AUTO) public int getUserID() {
I have written this code inside a servlet to delete certain records from three
I have written this code in JavaScript and works perfectly fine when I include
I have written some code that loads an XML document using an XmlDocument object
I have written a code for taking JSON data from a PHP and putting
I have written some code in JSP to download .docx files which is hosted

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.