473,804 Members | 2,117 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

My merge-sort implementation. Feedback appreciated!

Hey everyone,

I've just finished my implementation of the merge-sort algorithm in C,
and I thought I could ask for some feedback. (One can always improve,
they say)

Right now, the code sorts integers in an ascending order, which
probably isn't very useful on its own.

I know the code works for a reasonable and arbitrary size of input, but
how about using a bignum library (such as gmp) for adding the feature
of sorting inputs of any size? As well as sorting elements of any size.

Since I'm not very experienced with C yet, I could not really see if
there were any chances for memory leaks. Is it true that whenever I use
malloc() without an accompanying free(), a memory leak could occur?

Anyway, the source code is available here:
http://www.student.lu.se/~mah06jfo/c...ort-0.1.tar.gz

Thankful for any advice :)

- uppe

Jun 9 '06 #1
5 3551
"uppe" <je************ *@gmail.com> wrote in message
news:11******** **************@ j55g2000cwa.goo glegroups.com.. .
Hey everyone,

I've just finished my implementation of the merge-sort algorithm in C,
and I thought I could ask for some feedback. (One can always improve,
they say)

Right now, the code sorts integers in an ascending order, which
probably isn't very useful on its own.

I know the code works for a reasonable and arbitrary size of input, but
how about using a bignum library (such as gmp) for adding the feature
of sorting inputs of any size? As well as sorting elements of any size.

Since I'm not very experienced with C yet, I could not really see if
there were any chances for memory leaks. Is it true that whenever I use
malloc() without an accompanying free(), a memory leak could occur?

Anyway, the source code is available here:
http://www.student.lu.se/~mah06jfo/c...ort-0.1.tar.gz

Thankful for any advice :)


Always check the return of malloc(). If malloc() fails, take appropriate
action. Filter programs should not pump "Hi there" message to the console.

Use a callback function for comparison. Unless you can make it general
purpose, it does not really have any utility.

There are lots of really good merge sorts around already. What makes this
one special?
Jun 9 '06 #2
"Dann Corbit" <dc*****@connx. com> writes:
"uppe" <je************ *@gmail.com> wrote in message
news:11******** **************@ j55g2000cwa.goo glegroups.com.. .
Anyway, the source code is available here:
http://www.student.lu.se/~mah06jfo/c...ort-0.1.tar.gz
There are lots of really good merge sorts around already. What makes this
one special?


To my eye, it looks cleanly written.

I once wrote a merge sort I'm rather fond of, simply because I
consider it nicely written. It is list_sort(), here:
http://www.stanford.edu/class/cs140/.../kernel/list.c
--
"I'm not here to convince idiots not to be stupid.
They won't listen anyway."
--Dann Corbit
Jun 9 '06 #3
Ben Pfaff wrote:

"Dann Corbit" <dc*****@connx. com> writes:
"uppe" <je************ *@gmail.com> wrote in message
news:11******** **************@ j55g2000cwa.goo glegroups.com.. .
Anyway, the source code is available here:
http://www.student.lu.se/~mah06jfo/c...ort-0.1.tar.gz

There are lots of really good merge sorts around already.
What makes this
one special?


To my eye, it looks cleanly written.


I'm seeing it differently.
There shouldn't be any malloc calls in merge sort for lists.

The merge() function seems to be copying the lists,
with the insertLast() function,
instead of just merging them.

The split() function mallocs a pointer
which can never be freed.

I think there's a lot of memory leakage there.

--
pete
Jun 9 '06 #4
pete <pf*****@mindsp ring.com> writes:
Ben Pfaff wrote:

"Dann Corbit" <dc*****@connx. com> writes:
> "uppe" <je************ *@gmail.com> wrote in message
> news:11******** **************@ j55g2000cwa.goo glegroups.com.. .
>> Anyway, the source code is available here:
>> http://www.student.lu.se/~mah06jfo/c...ort-0.1.tar.gz

> There are lots of really good merge sorts around already.
> What makes this
> one special?


To my eye, it looks cleanly written.


I'm seeing it differently.
There shouldn't be any malloc calls in merge sort for lists.


I didn't really read the code. I just saw that the structure was
a simple set of calls to a simple set of functions, which is a
promising start.
--
"...what folly I commit, I dedicate to you."
--William Shakespeare, _Troilus and Cressida_
Jun 9 '06 #5
"Ben Pfaff" <bl*@cs.stanfor d.edu> wrote in message
news:87******** ****@benpfaff.o rg...
pete <pf*****@mindsp ring.com> writes:
Ben Pfaff wrote:

"Dann Corbit" <dc*****@connx. com> writes:

> "uppe" <je************ *@gmail.com> wrote in message
> news:11******** **************@ j55g2000cwa.goo glegroups.com.. .
>> Anyway, the source code is available here:
>> http://www.student.lu.se/~mah06jfo/c...ort-0.1.tar.gz

> There are lots of really good merge sorts around already.
> What makes this
> one special?

To my eye, it looks cleanly written.
I'm seeing it differently.
There shouldn't be any malloc calls in merge sort for lists.


I didn't really read the code. I just saw that the structure was
a simple set of calls to a simple set of functions, which is a
promising start.


I should point out that my intention was not to denigrate the efforts of the
O.P. but to find out why his code was to be recommended or what features he
wanted to highlight. This google query for merge sorting in C yields
305,000 hits for me:
http://www.google.com/search?hl=en&q...mergesort%29+C

I have perhaps two dozen merge sort implementations that I play with from
time to time (sorting is something of a hobby for me). I was simply curious
about what the author liked abouit his particular implementation.

While I do see a few defects in the code, I agree with Ben that it is a
promising start, at least.
--
"...what folly I commit, I dedicate to you."
--William Shakespeare, _Troilus and Cressida_

Jun 9 '06 #6

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
3251
by: William Wisnieski | last post by:
Hi Everyone, Access 2000 I have some code behind a button that performs a word merge with a query data source. The merge works fine. But what I'd like to do somehow is after the merge is generated (or before), assign the current date to the field in the main table the query data source is based on. Is this even possible? If so, how could I do it? Here is the code that works so far:
5
4578
by: stemc © | last post by:
Hi there, In work, we often mail merge letters and post them to contacts. But more and more, we've been emailing information to people instead. So far, I've been writing a single generic email, then copying and pasting the email addresses into the address fields. I start the email off with 'Dear all...' But is there a way to 'email merge' this directly into outlook emails, in the same way we do for normal printed letters? This...
2
3253
by: Aaron | last post by:
hello, i am perfoming a mail merge with the following code. Public Function MergeIt() Dim objWord As Object Set objWord = GetObject("C:\MyMerge.doc", "Word.Document") ' Make Word visible. objWord.Application.Visible = True ' Set the mail merge data source as the db3 database.
1
3138
by: Lisa | last post by:
I have a query named QryDept where one of the fields is DeptID. The query is used for the data source of a mail merge letter. I would like to control which department is to get the mail merge letters. I have a department selection pop-up form where the departments are listed in a listbox. The value of the listbox is DepartmentID. On the form is a Create Mail Merge button which is coded to open the mail merge and make the pop-up form not...
8
9530
by: Squirrel | last post by:
Hi everyone, I've created a mail merge Word doc. (using Office XP) , the data source is an Access query. Functionality I'm attempting to set up is: User sets a boolean field to true for each person for whom a mail merge letter is desired. The query reads address info from the table for each record where is true.
3
5593
by: Andy Davis | last post by:
I have set up a mail merge document in Word 2003 which gets its data from my Access 2000 database. I want to set up a button on a form that: 1. runs the query to provide the dat for the merge document in Word; 2. opens the document and runs the merge process for the new data. I have managed to write the code to perform step 1 ok, but I'm having trouble with step 2. It opens the word document fine but does not perform the mail merge of...
4
21689
by: John J. Hughes II | last post by:
Could someone explain how to merge the form menu with the mdi container window. The menu strip items on the form window merge but I either end up with a blank blue menu on the form or top list of items that don't do anything. MDI Container.IsMdiContainer = true MenuStrip.AllowMerge = true /// First menu items this.miFile.MergeAction = System.Windows.Forms.MergeAction.Insert;
4
7225
by: Tom Jones | last post by:
I have an application that was originally built using Visual Studio 2003 that I upgraded to Visual Studio 2005. When I attempt to build the *.msi file in the deployment project, I am getting a compile error stating that it cannot find: "C:\Program Files\Common Files\Merge Modules\Crystal_regwiz2003.msm" I am not sure where to get this file or even if it is appropriate (i.e., should there be a newer version for Visual Studio 2005).
7
7245
by: giladp1 | last post by:
I found Albert Kallal's great "Super easy Word Merge" code in his site at: http://www.members.shaw.ca/AlbertKallal/msaccess/msaccess.html Thanks Albert so much for sharing this. I am looking for any comments about the use of the docmd.transfertext method instead of the code Albert used for creating the text file. Also, perhaps some ideas for coding the Subject Line of each email so
24
7153
by: Henry J. | last post by:
My app needs to insert thousand value rows into a mostly empty table (data are read from a file). I can either use inserts, or use merge. The advantage of using merge is that in the few cases where the table is not empty, it can take care of the updating part, which makes the app cleaner. However, my concern is the merge state would slow dowm the insertion of new data, since in most cases the table is empty. So my questions (before I...
0
9716
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9595
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10101
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
9177
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7643
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6870
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5536
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
5675
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
3
3005
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.