The task is to shuffle the digits of an input number to produce the smallest number feasible, making sure that consecutive digits form even-odd pairs. This is commonly referred to as the "swapping adjacent even-odd pairs" dilemma when programming in C++.
- Algorithm Using Bubble Sort
- Strategy Employing Greedy Technique
1. Bubble Sort Approach
A widely known and straightforward sorting technique for efficiently solving a range of sorting challenges is bubble sort. This sorting algorithm offers a straightforward approach for determining the smallest number achievable by exchanging neighboring even and odd pairs.
When applying this technique, we initiate the process by examining the digits of the number in a left-to-right manner. Progressively moving through the digits, we assess each pair that is side by side. Whenever we encounter a pair where the first digit is even and the second digit is odd, and the order is incorrect, we interchange them to rectify the sequence. This iterative procedure continues until no further exchanges are needed to guarantee that consecutive digits establish even-odd pairs.
Algorithm steps:
Step-1: Input an array of unsorted numerical values.
Create a loop that will iterate through the array continuously until each element is arranged in order.
Step-3: Within the loop, iterate through each pair of consecutive elements in the array.
Verify that in each pair, the initial element is an odd number while the subsequent element is an even number. If this condition holds true, interchange the positions of the elements.
Step 5: Examine the swapped element and compare it with the preceding element in the array. If needed, substitute the previously swapped element with the smaller one.
Step-6: Continuously follow this process until every pair is confirmed and organized.
If no exchanges occur in any of the iterations, the array has been sorted, and the loop can be stopped.
Display the array in its sorted order once the sorting process is finished.
Example:
Let's consider an illustration to demonstrate the problem of swapping adjacent even-odd pairs in C++ using the Bubble sort technique.
#include <iostream>
using namespace std;
void BubbleSort(int arr_values[], int num)
{
for (int q = 0; q < num - 1; q++)
{
bool swapped = false;
for (int k = 0; k < num - q - 1; k++)
{
if ((arr_values[k] % 2 == 0 && arr_values[k+1] % 2 != 0) || (arr_values[k] % 2 == arr_values[k+1] % 2 && arr_values[k] > arr_values[k+1]))
{
swap(arr_values[k], arr_values[k+1]);
swapped = true;
}
}
if (!swapped) break;
}
}
int main()
{
int num;
cout << "Please enter the number of elements: ";
cin >> num;
int arr_values[num];
cout << "Enter the elements separated by space: ";
for (int l = 0; l < num; l++)
{
cin >> arr_values[l];
}
BubbleSort(arr_values, num);
for (int l = 0; l < num; l++)
{
cout << arr_values[l] << " ";
}
cout << endl;
return 0;
}
Output:
Please enter the number of elements: 8
Enter the elements separated by space: 7 2 5 9 10 12 8 1
1 5 7 9 2 8 10 12
Explanation:
This C++ program arranges a set of integers through the Bubble Sort technique. This method ensures that integers with matching parity remain in their original order and that even-odd pairs are arranged in ascending order. The BubbleSort function performs iterative swaps of neighboring elements in the array based on specific criteria. It conducts swaps depending on whether adjacent elements create even-odd pairs or share the same parity but are not in the correct sequence in each cycle. When no swaps occur within a cycle, the swapped flag signifies that the array is already sorted, enhancing the sorting efficiency by exiting the loop.
Following that, the primary function prompts the user to input the elements of the array and the total number of elements. It then proceeds to arrange the array utilizing the BubbleSort algorithm before displaying the sorted array.
2. Greedy Approach
- Start from the leftmost digit of the number.
- Iterate the process from left to right through the digits of the number.
- Swap the digits if the one before it is even and the one after it is odd.
- Repeat this process until the end of the number is reached or there are no more swaps available.
- By swapping adjacent even-odd pairs, the resulting number will be the smallest possible number.
Example:
Let's consider a scenario to demonstrate the Greedy method for solving the problem of swapping adjacent pairs of even and odd numbers in C++.
#include <iostream>
#include <string>
using namespace std;
string smallestNumberSwappingPairs(string s)
{
int num_ber = s.length();
for (int q = 0; q < num_ber - 1; q++)
{
if (q % 2 == 0 && s[q] % 2 != 0)
{
for (int l = q + 1; l < num_ber; l++)
{
if (l % 2 != 0 && s[l] % 2 == 0)
{
swap(s[q], s[l]);
break;
}
}
}
}
return s;
}
int main()
{
string s;
cout << "Please enter the number: ";
cin >> s;
string smallestNum = smallestNumberSwappingPairs(s);
cout << "The smallest number after swapping the adjacent even-odd pairs is: " << smallestNum << endl;
return 0;
}
Output:
Please enter the number: 2145367
The smallest number after swapping the adjacent even-odd pairs is: 2145637
Explanation:
This C++ script exchanges adjacent even-odd pairs within a provided number presented as a string to determine the smallest feasible number. By traversing the string, the smallestNumberSwappingPairs function identifies even digits at even positions and odd digits at odd positions. To preserve the even-odd pairing, it swaps the digits when encountering such a pair. Following this, the main function prompts the user for a number input and executes the smallestNumberSwappingPairs function to swap adjacent even-odd pairs and obtain the smallest number. Subsequently, the outcome is displayed. In essence, the code efficiently employs a swapping technique to reduce the number while ensuring the sequence of even-odd pairs remains unchanged.