Understand Quicksort with DDD
Sorting is one of the most common functions performed by a computer, and Quicksort is one of the most efficient ways to do it. This article demonstrates the usefulness of a graphical debugger for learning how Quicksort works.
DDD (Data Display Debugger) is a free, visual front end that can control many popular debuggers. This article uses DDD to work through a simple C implementation of Quicksort.
First, find a copy of DDD and install it. Binary and source packages are available for RPM-based distributions at rpmfind.net, and Debian packages are available at debian.org. This article was written using DDD version 3.3.1 on Red Hat 7.3. This article also makes the following assumptions: 1) you have a GNU/Linux-enhanced computer and it is plugged in; 2) you know basic C concepts including arrays, loops and recursion; and 3) you have a capable C compiler, such as GNU's GCC. Even if you don't know anything about programming, try stepping through the code anyway. It's good for you.
CAR Hoare described the Quicksort algorithm in a much-cited 1962 paper, and it is still in common use 40 years later. The divide-and-conquer approach of Quicksort is probably where it got the prefix quick; by segregating smaller elements from larger elements it eliminates the need for many comparisons. In contrast, a selection sort compares every element to every other element. This is not to say that Quicksort is always faster or that it's the best way to sort; it's simply cool to know. The implementation in this article is not an optimized or extensible quicksort. It only works on integer arrays.
This code was largely borrowed from The Practice of Programming by Brian W. Kernighan and Rob Pike:
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
/* from: The Practice of Programming (pp: 32-34)
* by: Brian W. Kernighan and Rob Pike
*/
/* swap: interchange v[i] and v[j] */
void swap( int v[], int i, int j )
{
int tmp;
tmp = v[i];
v[i] = v[j];
v[j] = tmp;
}
/* quicksort: sort v[0]..v[n-1] into increasing
* order
*/
void quicksort( int v[], int n )
{
int i = 0, last = 0;
if ( n <= 1 ) /* nothing to do */
return;
swap( v, 0, rand() % n ); /* move pivot elem
to v[0] */
for ( i = 1; i < n; i++ ) /* partition */
if ( v[i] < v[0] )
swap( v, ++last, i );
swap( v, 0, last ); /* restore pivot */
quicksort( v, last ); /* sort smaller
values */
quicksort( v+last+1, n-last-1 ); /* sort larger
values */
}
void print_array( const int array[], int elems )
{
int i;
printf("{ ");
for ( i = 0; i < elems; i++ )
printf( "%d ", array[i] );
printf("}\n");
}
#define NUM 9
int main( void )
{
int arr[NUM] = { 6, 12, 4, 18, 3,
27, 16, 15, 19 };
/* commented out to aid in predictability
* srand( (unsigned int) time(NULL) ); */
print_array(arr, NUM);
quicksort(arr, NUM);
print_array(arr, NUM);
return EXIT_SUCCESS;
}
Save the code to a file called easy_qsort.c. Next, compile the code:
$ gcc -Wall -pedantic -ansi -o qsortof -g \ easy_qsort.c
The most important argument to GCC is notably -g, which adds debugging symbols to the code.
Try running the program to make sure everything is kosher:
$ ./qsortof
{ 6 12 4 18 3 27 16 15 19 }
{ 3 4 6 12 15 16 18 19 27 }
The first line of output is the unsorted array, the second line is the array after running through the quicksort function.
So how does it work? Let's turn to our new friend DDD:
$ ddd qsortof
This should bring up DDD. Close any Tips and About:Help windows that pop up, and you should see something like Figure 1.
It would be a good idea to turn on line numbering now. Click the check box next to Display Source Line Numbers in the Edit-->Preferences-->Source menu. Now we can add a breakpoint and start debugging.
First, select the nothing to do line by clicking on its line number in the margin. Then, click the Set/Delete Breakpoint at () button, and click the Run button in the floating command tool.
At this point you should see a red stop sign at the line with the breakpoint and a green arrow on the same line (the code that is about to execute). Let's use DDD to display some inside information.
To begin, select Data-->Display Local Variables. Next, select Data-->Display Arguments, and then select Status-->Backtrace. Finally, type graph display v[0]@n into the console window, and press Enter. This displays elements 0 through n of the v[] array (see Figure 2).
Realizing the promise of Apache® Hadoop® requires the effective deployment of compute, memory, storage and networking to achieve optimal results. With its flexibility and multitude of options, it is easy to over or under provision the server infrastructure, resulting in poor performance and high TCO. Join us for an in depth, technical discussion with industry experts from leading Hadoop and server companies who will provide insights into the key considerations for designing and deploying an optimal Hadoop cluster.
Sponsored by AMD
Built-in forensics, incident response, and security with Red Hat Enterprise Linux 6
Every security policy provides guidance and requirements for ensuring adequate protection of information and data, as well as high-level technical and administrative security requirements for a system in a given environment. Traditionally, providing security for a system focuses on the confidentiality of the information on it. However, protecting the data integrity and system and data availability is just as important. For example, when processing United States intelligence information, there are three attributes that require protection: confidentiality, integrity, and availability.
Learn more about catching the bad guy in this free white paper.
Sponsored by DLT Solutions
| Designing Electronics with Linux | May 22, 2013 |
| Dynamic DNS—an Object Lesson in Problem Solving | May 21, 2013 |
| Using Salt Stack and Vagrant for Drupal Development | May 20, 2013 |
| Making Linux and Android Get Along (It's Not as Hard as It Sounds) | May 16, 2013 |
| Drupal Is a Framework: Why Everyone Needs to Understand This | May 15, 2013 |
| Home, My Backup Data Center | May 13, 2013 |
Enter to Win an Adafruit Pi Cobbler Breakout Kit for Raspberry Pi

It's Raspberry Pi month at Linux Journal. Each week in May, Adafruit will be giving away a Pi-related prize to a lucky, randomly drawn LJ reader. Winners will be announced weekly.
Fill out the fields below to enter to win this week's prize-- a Pi Cobbler Breakout Kit for Raspberry Pi.
Congratulations to our winners so far:
- 5-8-13, Pi Starter Pack: Jack Davis
- 5-15-13, Pi Model B 512MB RAM: Patrick Dunn
- 5-21-13, Prototyping Pi Plate Kit: Philip Kirby
- Next winner announced on 5-27-13!
Featured Jobs
| Linux Systems Administrator | Houston and Austin, Texas | Host Gator |
| Senior Perl Developer | Austin, Texas | Host Gator |
| Technical Support Rep | Houston and Austin, Texas | Host Gator |
| UX Designer | Austin, Texas | Host Gator |
| Web & UI Developer (JavaScript & j Query) | Austin, Texas | Host Gator |
Free Webinar: Hadoop
How to Build an Optimal Hadoop Cluster to Store and Maintain Unlimited Amounts of Data Using Microservers
Realizing the promise of Apache® Hadoop® requires the effective deployment of compute, memory, storage and networking to achieve optimal results. With its flexibility and multitude of options, it is easy to over or under provision the server infrastructure, resulting in poor performance and high TCO. Join us for an in depth, technical discussion with industry experts from leading Hadoop and server companies who will provide insights into the key considerations for designing and deploying an optimal Hadoop cluster.
Some of key questions to be discussed are:
- What is the “typical” Hadoop cluster and what should be installed on the different machine types?
- Why should you consider the typical workload patterns when making your hardware decisions?
- Are all microservers created equal for Hadoop deployments?
- How do I plan for expansion if I require more compute, memory, storage or networking?






5 hours 8 min ago
5 hours 26 min ago
7 hours 19 min ago
9 hours 12 min ago
16 hours 6 min ago
16 hours 22 min ago
18 hours 14 min ago
1 day 5 min ago
1 day 4 hours ago
1 day 4 hours ago