For beginners in programming, Array is one of the most important concepts that they have to learn and master over the years.Â
A useful fact about arrays is that the elements stored in an array data structure are always aligned in contiguous memory locations.
But, even though most of the time the data is arranged in a sorted order, locating these elements becomes a hefty task once they are initialized in a program.
You would have to navigate through the entire data if it wasn’t for the pointers. The pointers are variables found in data structures that can effectively locate the addresses of specific elements stored in memory.
The pointers make searching through data easier since they make the process of handling data much more efficient.
In this blog, we are going to be exploring this feature of pointers and putting them to use to rearrange array alternately.
Learn how pointers can be used in two dimensional or even multi-dimensional formats with conventional methods and algorithms for rearranging data in an array.
What is an Array?
An array is one of the simplest of data structures where the elements can easily be assessed by using their specific index positions.
The elements within an array are arranged in a lexical order. This means that all the elements are arranged either alphabetically or in a numerical order.
This structural integrity of the array makes it extremely easier to arrange the elements in a sorted order. Also, did you know that you can manipulate the data stored within an array even while running a program?
This is because the pointers present in an array lets the users access any of the elements without having to traverse through the entire list.Â
Let’s discuss more about pointers in the following section.
What are Pointers in an Array?
The pointers are a form of variables in the context of arrays. These are used for storing the addresses of the elements stored within an array.
Once the user allocates memory of a specific element, the pointer then locates the exact address of the element in the memory of the program.
You can use a Unary Operator (*) for declaring a variable since it is highly efficient for returning the address of the allocated memory.
Hence, the pointers of an array are used for pointing to the memory block of the elements of an array.
You can find the usage of the following types of syntaxes for the pointers of arrays:
- Datatype
These include the type of variables or elements such as float, char, int etc
- Variable_name
This refers to name of the variable or element alloted by the user
- Size
This reflects the size of the element stored within the array.
Since one of the major functions in an array is performed by the pointers i.e location of data, they can be effectively used for multiple purposes.
One such important use of pointers that we are going to be discussing today is to rearrange arrays alternately.
In other words, we are going to learn how we can efficiently Re-arrange an array in minimum as well as maximum using a dual pointer technique.
How to Re-arrange an Array in Minimum and Maximum using the Two-Pointer Technique?
In order to rearrange arrays alternately, check out the following problem statement and we will find out how using the Two-Pointer Technique helps maintain the time complexity of the program.
Problem Statement
You have been provided an array that is sorted with a set of positive integers. You are required to rearrange this array alternately for instance, the first element of the array must contain the maximum value, the second must have a minimum value, while at the third position we shall have the second maximum value and so forth.
Answer Key
In order to Rearrange an array in the maximum and minimum format using the Two-Pointer Technique, a simple approach would be using an auxiliary array.
Approach: Using an Auxiliary Array
We will start by maintaining two different pointers. Each of these pointers will be allocated at different locations within the string.
The first pointer will be initialised to the smallest or the leftmost element within the string. Similarly, we will allocate the second pointer at the largest or the rightmost element of the string.
Both of these pointers will be moved towards each other consecutively one after the other. While the pointers are being moved within the program, we will keep adding elements to an auxiliary array by copying the elements alternatively.
Finally as the last step, we will copy all the elements from an auxiliary array to the initial array.
Check out the following algorithms for the two pointers technique:
- Start by printing maximum value at the first position, minimum value at the second position, second maximum value at the third position and second minimum value at the fourth position
- Continue the process till all the elements from the given array are covered
- Next up, we will initialize an auxiliary array for holding the modified elements. You can also create bridges in graph for incorporating these recreated elementsÂ
- Now we will be using the indexes from the smallest and the largest element from the initial array
- These indices will be used for deciding whether it is required to copy the remaining largest and smallest elements at our next position
- Once an auxiliary array is formed, store its value or the generated results in the temp[] function
- Now, copy the contents of temp[] to the initial array i.e arr[]
- Finally, run the driver code and the program will rearrange arrays alternatively by itselfÂ
Using the Two-Pointer Technique gives us the flexibility of managing the smallest and the largest elements easily as compared to the other approaches.
Since we have iterated twice over the given array, the time complexity for this approach would be: O(N)
Wrapping Up
One of the most interesting facts about pointers is that they make it extremely faster to manipulate data by providing specific address locations in the memory.
You can also explore more about pointers by solving other programming problems such as merging arrays, finding bridges in graphs or sorting a linked list.