Efficient Array Sorting in C++: Static Overloaded Functions for Floats and Integers
This C++ program defines a class ArraySort that contains static member functions for sorting arrays of floats and integers.
It uses the bubble sort algorithm for sorting. Below is an explanation of the code –
#include<iostream>
#include<string.h>
using namespace std;
class ArraySort
{
public:
static void sort_array(float f[5]);
static void sort_array(int f[5]); //static member function overloading
static void swap(float &a,float &b);
static void swap(int &a,int &b);
};
void ArraySort :: sort_array(float f[5]) //static member function definition
{
int i,j;
for(i=0; i<5-1; i++)
{
for(j=0; j<5-1; j++)
{
if(f[j] > f[j+1])
swap(f[j],f[j+1]); //nesting static member function
}
}
cout<<"sorted float array is : "<<endl;
for(i=0; i<5; i++)
cout<<f[i]<<endl;
}
void ArraySort :: sort_array(int f[5]) //static member function overloading
{
int i,j;
for(i=0; i<5-1; i++)
{
for(j=0; j<5-1; j++)
{
if(f[j] > f[j+1])
swap(f[j],f[j+1]); //nesting static member function
}
}
cout<<"sorted integer array is : "<<endl;
for(i=0; i<5; i++)
cout<<f[i]<<endl;
}
void ArraySort :: swap(float &a,float &b)
{
float temp = a;
a = b;
b = temp;
}
void ArraySort :: swap(int &a,int &b)
{
int temp = a;
a = b;
b = temp;
}
//member functions definition
int main()
{
ArraySort a1;
float fary[5];
cout<<"Enter float array : ";
int i;
for(i=0; i<5; i++)
cin>>fary[i];
a1.sort_array(fary);
// static member function calling and passing float array
//sort_array(float) invoked
int ary[5];
cout<<"Enter Integer array : ";
for(i=0; i<5; i++)
cin>>ary[i];
a1.sort_array(ary);
// static member function calling and passing int array
//sort_array(int) invoked
return 0;
}
Class Definition
- The class ArraySort is defined with public static member functions and static member function overloading.
- Two static member functions are declared for sorting arrays of floats and integers: sort_array(float f[5]) and sort_array(int f[5]).
- Two static member functions for swapping elements in arrays: swap(float &a, float &b) and swap(int &a, int &b).
Sorting Functions
- The sort_array(float f[5]) function takes an array of floats and sorts it using the bubble sort algorithm.
- The sort_array(int f[5]) function takes an array of integers and sorts it using the same bubble sort algorithm.
Swap Function
- The swap functions are helper functions used for swapping two elements in an array.
Main Function
- In the main function, an object a1 of the class ArraySort is created.
- The user is prompted to enter values for a float array (fary) and an integer array (ary).
- The sort_array static member functions of the ArraySort class are then called with the respective arrays.
- The sorted arrays are displayed as output.
Explanation of Execution
- The user inputs values for the float array and integer array.
- The sort_array functions are invoked to sort the arrays using the bubble sort algorithm.
- The sorted arrays are then displayed as output.
