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

The Archive Base Latest Questions

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

I’m trying to keep track of number of comparison between array elements being made

  • 0

I’m trying to keep track of number of comparison between array elements being made in merge sort. The program is written in Lua. I am trying to use multiple result and keep track of the counter but it isn’t working out. Is there other suggestions?

This is my first post so sorry if it’s messy. Thank you!

function merge_sort (src_array)

  if #src_array <= 1 then 
    return src_array 
  else
   local a1, a2 = split_array(src_array)        -- splitting array to sort
   return merge(            -- merge the results recursively
         merge_sort(a1),    
         merge_sort(a2))
   end
end 
  • 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:29:51+00:00Added an answer on May 26, 2026 at 3:29 pm

    I am trying to use multiple result and keep track of the counter

    If you want to do it using multiple results, you’ll need to change your recursive call.
    Each function should return a sorted array together with the number of comparisons needed to sort it:

    local s1, n1 = merge_sort(a1)
    local s2, n2 = merge_sort(a2)
    local s, n = merge(s1, s2)
    return s, n1 + n2 + n
    

    Your base case will look like

    return src_array, 0
    

    I think probably the extra result is not worth it and you are better off with a local counter and a nested merge function:

    function merge_sort (src_array)
    
      local n = 0   -- number of comparisons
    
      local function merge(a1, a2)
        -- merge a1 and a2, incrementing n at each comparison
        -- return merged array
      end
      local function sort(a)
        if #a <= 1 then 
          return a
        else
          local a1, a2 = split_array(a)        -- splitting array to sort
          return merge(            -- merge the results recursively
                sort(a1),    
                sort(a2)) -- recursive call to `sort` *not* `merge_sort`
        end
      end
      local sorted = sort(src_array)
      return sorted, n
    end 
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am trying to understand how to use SyndicationItem to display feed which is
Basically, what I'm trying to create is a page of div tags, each has
link Im having trouble converting the html entites into html characters, (&# 8217;) i
I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
I'm trying to decode HTML entries from here NYTimes.com and I cannot figure out
I'm trying to use string.replace('’','') to replace the dreaded weird single-quote character: ’ (aka
I'm trying to create an if statement in PHP that prevents a single post
I'm having trouble keeping the paragraph square between the quote marks. In firefox the
I am trying to loop through a bunch of documents I have to put

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.