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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T21:30:21+00:00 2026-06-15T21:30:21+00:00

For an example I need to sort: ?- mergesort([8,1,[4,6,3],2],Sorted). The outcome should be: Sorted

  • 0

For an example I need to sort:

?- mergesort([8,1,[4,6,3],2],Sorted).

The outcome should be:

Sorted = [1, 2, 8, [3, 4, 6]]

Unfortunately I’m not sure what to do. I’ve tried to find an answer myself, but didn’t find a solution. Currently the outcome is:

Sorted = [1, 2, 8, [4, 6, 3]]

My code:

mergesort([],[]).
mergesort([A],[A]).
mergesort([A,B|R],S) :-  
   split([A,B|R],L1,L2),
   mergesort(L1,S1),
   mergesort(L2,S2),
   merge(S1,S2,S).

split([],[],[]).
split([A],[A],[]).
split([A,B|R],[A|Ra],[B|Rb]) :-  split(R,Ra,Rb).

merge(A,[],A).
merge([],B,B).
merge([A|Ra],[B|Rb],[A|M]) :-  A @=< B, merge(Ra,[B|Rb],M).
merge([A|Ra],[B|Rb],[B|M]) :-  A @> B,  merge([A|Ra],Rb,M).

Big thanks in advance!

  • 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-15T21:30:22+00:00Added an answer on June 15, 2026 at 9:30 pm

    I would write a simple preprocessing step:

    sortl([H|T],[Hs|Ts]) :-
        (   is_list(H)
        ->  mergesort(H,Gs),
            sortl(Gs,Hs)
        ;   Hs = H
        ),
        sortl(T,Ts).
    sortl([],[]).
    

    then you could use it this way

    ?- sortl([8,1,[4,6,[3,4],[1,8],3],2,[1,3,2]],T),mergesort(T,Sorted).
    T = [8, 1, [3, 4, 6, [1, 8], [3|...]], 2, [1, 2, 3]],
    Sorted = [1, 2, 8, [1, 2, 3], [3, 4, 6, [...|...]|...]]
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I need to sort filenames that can have a common root, but are then
There are a few ways to do a merge sort, but I specifically need
I need to sort the strings in a ListBox , but it is bound
i need to sort an string Array and it MUST be sorted by ascii.
I need some sort of dataset which has related items in it. For example,
I need to sort an example XML in a way that all the ShippingPoints
I need to sort some elements depend on it attribute. For an example: <div
I need to sort on column's partial value. For example , I've a table
i have a group of arrays that i need to sort numerically: example: $a=
For Example: I need to develop a Small Inventory program for nearby Super Market

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.