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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T15:31:32+00:00 2026-05-26T15:31:32+00:00

I am making a c insertion sort and it works fine except that after

  • 0

I am making a c insertion sort and it works fine except that after the sort the first number is always a weird negative number and the program errors out.

#include <stdio.h>
#include <stdlib.h>
#include <time.h>

void insertionSort(int list[], int last){
     int hold;
     int walker;
     int current;
     int count;

     count = 0;
     for (current = 1; current <= last; current++){
         hold = list[current];
         for (walker = current - 1; 
             walker >= 0 && hold < list[walker]; walker--){
                    list[walker + 1] = list[walker];
             }
         list [walker + 1] = hold;
         count++;
     }
     printf("\n\nHow many passes to sort?\n%d\n\n", count);
     return;
}

int main(int argc, char *argv[])
{
  int numbers[100];
  int i;

  srand(time(NULL));
  for (i = 0; i < 100; i++){
      numbers[i] = rand() % 100;
  }
  printf("Unsorted Numbers\n-------- -------\n");
  for (i = 0; i < 100; i++){
      printf("%d,", numbers[i]);
  }
  insertionSort(numbers, 100);
  printf("\nSorted Numbers\n-------- -------\n");
  for (i = 0; i < 100; i++){
      printf("%d,", numbers[i]);
  }
  system("PAUSE");  
  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-05-26T15:31:33+00:00Added an answer on May 26, 2026 at 3:31 pm

    You are going OVER the array size within the loop. When current = last, list[last] is list[100], the 101th element of the array… this is also not good.

    Edit. I just tested this out and it worked for me. Only thing i changed was the <= in the outer loop to n <

    void insertionSort(int list[], int last){
     int hold;
     int walker;
     int current;
     int count;
    
     count = 0;
     for (current = 1; current < last; current++){
         hold = list[current];
         for (walker = current - 1; 
             walker >= 0 && hold < list[walker]; walker--){
                    list[walker + 1] = list[walker];
             }
         list [walker + 1] = hold;
         count++;
     }
     printf("\n\nHow many passes to sort?\n%d\n\n", count);
     return;
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I coded for doubly linked list implementation in C. In that, after making insertion
Looking into possibility of making an USB distributed application that will autostart on insertion
I am making a program that deal with multiple tables (recipe, instructions on how
Making my first steps with NHibernate, I'm trying to have it creating my Tables
Making an adobe flex ui in which data that is calculated must use proprietary
Making my first steps in RIA Services (VS2010Beta2) and i encountered this problem: created
Making UML sequence diagram in VS 2010RC I've observed that there is no activation
After making some changes in my models (eg. new field in a model and
After making a few modifications to a rails app I am tinkering on, railroad
i have written a code for insertion sort but it is nt sorting.. kindly

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.