Welcome! Log In Create A New Profile

Advanced

Merge sort

Posted by maccaroo 
Announcements Last Post
Announcement SoC Curricula 09/30/2017 01:08PM
Announcement Demarcation or scoping of examinations and assessment 02/13/2017 07:59AM
Announcement School of Computing Short Learning Programmes 11/24/2014 08:37AM
Announcement Unisa contact information 07/28/2011 01:28PM
Merge sort
July 07, 2006 04:19PM
Hello again,

Okay, Ass2 Q5. Does anyone know how to chop a list in half? We can use [First|Rest] to get the first element of a list, and difference lists can be used to get the last element, but how do you chop it in half?

conc(LeftBit, RightBit, List) doesn't guarantee the halves will be the same size, and we've already established that it's not very efficient.

My best guess is that there's a sneaky way to use difference lists which can get you 2 similarly-sized halves, but I honestly can't see a solution. Please could someone throw me a bone here? Ta
Anonymous User
Re: Merge sort
July 07, 2006 05:27PM
perhaps get the length of the list and then divide that number by 2? Don't think we have to use difference lists for this question.

could you do the bagof, setof and findall questions??
Re: Merge sort
July 07, 2006 06:04PM
Hi Celene,

Yes, you're right. I figured that the length was necessary, but couldn't work out a better way to split the list other than conc, so that's the way I've done it. It's a monster though... so I hope I don't get marked down for efficiency.

Yeah, I managed to get Q4. I din't use findall at all, and (b) was easier to do without using the special procedures. Do you need help with anything in particular?
avatar Re: Merge sort
July 15, 2006 05:12PM
This is sort of cheating I guess, but it was the simplest/most efficient way I could think of and technically satisfys the criteria of what it's supposed to do.

divide([],[],[]).
divide([],[],[],_).
divide([H|T],L1,[H|L2],_):-divide(T,L1,L2).
divide([H|T],[H|L1],L2):-divide(T,L1,L2,_).%split a list into two halves (odd and even not down the middle)

what do you think?
Re: Merge sort
July 18, 2006 08:27AM
I managed to split it using length/2. It's not the most efficient solution, but at least it works.
Sorry, only registered users may post in this forum.

Click here to login