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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T12:17:38+00:00 2026-06-13T12:17:38+00:00

Here is the algorithm of insertion sort in C++ (from tutorial): void insertionSort(int arr[],

  • 0

Here is the algorithm of insertion sort in C++ (from tutorial):

void insertionSort(int arr[], int length) {
      int i, j, tmp;
      for (i = 1; i < length; i++) {
            j = i;
            while (j > 0 && arr[j - 1] > arr[j]) {
                  tmp = arr[j];
                  arr[j] = arr[j - 1];
                  arr[j - 1] = tmp;
                  j--;
            }
      }
}

and here is what I’m doing in Ruby

a = [12, 1, 18, -3, -2, 66, 31]
puts a

def insertion_sort(source)
    source.to_enum.with_index(1).each do |item, i|
        j = i
        while((j>0) && (source[j-1] > source[j]))
            source[j], source[j-1] = source[j-1], source[j]
            j -= 1  
        end
    end
end

insertion_sort(a)
puts a

it throws an error of comparison of Fixnum with nil failed (ArgumentError). Probably because of overflow.

What did I do wrong?

  • 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-13T12:17:39+00:00Added an answer on June 13, 2026 at 12:17 pm

    In the C++ version you have (i = 1; i < length; i++). Which means, it will not run the last round where i = length. That would be out of the array.

    In ruby, because you set the offset of the index to 1, the last round, you would have i = length. Hence source[length] is out of source, so returns nil.

    1 > nil  # source[j-1] > source[j] when j = length
    # ArgumentError: comparison of Fixnum with nil failed
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have tried to implement binary insertion sort algorithm. Here is my code: public
Here's the following algorithm: int encryption(int a, int b) { short int c, c2;
Here is a excise from The book Algorithm Design Manual . In the bin-packing
I'm trying to implement this algorithm description from a previous question I had here
I have simple algorithm that clean the whitespace from half string until end. Here
in an Introduction to Algorithm 2nd edition, I found insertion sort pseudo code INSERTION-SORT(A)
Just got my feet wet in sorting algorithm with Haskell. I've implemented insertion-sort and
I have something like a sort-algorithm here, and I want to pass it a
In reference to fastest sort of fixed length 6 int array , I do
Okay so here's my algorithm for finding a Cut in a graph (I'm not

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.