Swapping Techniques C Programming

How to write code to swap data elements effectively

© Guy Lecky-Thompson

Swap Node C Code, Guy W. Lecky-Thompson

When writing list management code, it is necessary to be able to swap data elements. This article looks at ways to use swapping techniques in C programming.

Introduction

When creating projects that manage data, be it in a list, file, or any other form of data storage, it is often necessary to swap data elements. The most common reason for wanting to swap two data elements is when sorting the list.

When we talk about swapping and swapping techniques in C programming, we mean the ability to either:

In the first instance, we are taking a variable that contains an item of data, and swapping it with another variable containing the same type. The actual values are sapped, so that variable one contains the value of variable two, and variable two contains the value of variable one.

Reference swapping performs the same function, but it does not actually swap the value, but only a pointer to that value in memory. This has benefits, and drawbacks, which we shall discuss presently.

Array Swapping

Consider an array of a known length, containing numerical data elements. We might have declared such an array thus:

int nArray[50];

This contains an array of 50 items, indexed 0 through 49. Each item, or element, is an integer; this being a built-in C data type. Since it is a built-in data type, we can directly assign values to each element.

The swapping process takes three stages:

The C code for this is actually simpler than the description:

int nTemp;
nTemp = nArray[1];
nArray[1] = nArray[2];
nArray[2] = nTemp;

Of course, the above makes more sense when placed into a function:

void SwapElements( int * nArray, int nFirst, int nSecond)
{
  int nTemp = nArray[nFirst];
  nArray[nFirst] = nArray[nSecond];
  nArray[nSecond] = nTemp;
}

Please feel free to clip the code sample, and use it in your own projects. Of course, if you need to swap different data types, then the code will need to be adjusted.

Structure Swapping

If we have a user defined structure, the above solution will not work, as the data structure will not have a built-in copy operator. In other words, we can not assign the value of a data structure from one variable to another.

Instead, we need to apply the theory as follows:

It is important to note that each member of the structure must be copied, one at a time. If there are nested structures within the structure, these must be copied in the same way. This makes the process much more involved, and much more tedious.

Pointer Swapping

The solution to the above problem is to swap references rather than actual objects. This requires that we have performed a few pre-requistite operations:

With these three items in place, we can perform the exact same process as for swapping items in an array consisting of built-in types. This is possible because the pointer is also a built-in type. The code for swapping pointers is akin to:

void SwapPointers(void * pOne, void * pTwo)
{
  void * pTemp;
  pTemp = pOne;
  pOne = pTwo;
  pTwo = pTemp;
}

The 'void *' data type is used to indicate that the pointer points to data of a type that is not necessarily known at the time the code is written. It will, therefore, need to be cast when the program is compiled, to the required data type.

Conclusion

The most efficient way to swap to data elements is to swap a pair of pointers. However, the programming overhead required to do this might negate the performance increase. A simple array of built-in types might be better swapped using Array Swapping, while a full struct containing user defined data types might benefit more from Pointer Swapping.

The reason for this is simple - a user defined structure contains more data, and the act of copying each item from one place to another will be more computationally expensive than swapping two pointers. However, a pointer has the same size as many built-in data types, and so swapping an integer (for example) by reference will not save as much time, as it requires copying the same number of bytes as the pointer.


The copyright of the article Swapping Techniques C Programming in C Programming is owned by Guy Lecky-Thompson. Permission to republish Swapping Techniques C Programming must be granted by the author in writing.




Post this Article to facebook Add this Article to del.icio.us! Digg this Article furl this Article Add this Article to Reddit Add this Article to Technorati Add this Article to Newsvine Add this Article to Windows Live Add this Article to Yahoo Add this Article to StumbleUpon Add this Article to BlinkLists Add this Article to Spurl Add this Article to Google Add this Article to Ask Add this Article to Squidoo