My professor assigned homework to write a function that takes in an array of integers and sorts all zeros to the end of the array while maintaining the current order of non-zero ints. The constraints are:
Cannot use the STL or other templated containers. Must have two solutions: one that emphasizes speed and another that emphasizes clarity.
I wrote up this function attempting for speed:
#include <iostream>
#include <cstdio>
#include <cstdlib>
using namespace std;
void sortArray(int array[], int size)
{
    int i = 0;
    int j = 1;
    int n = 0;
    for (i = j; i < size;)
    {
        if (array[i] == 0)
        {
            n++;
            i++;
        }
        else if (array[i] != 0 && j != i)
        {
            array[j++] = array[i++];
        }
        else
        {
            i++;
            n++;
        }
    }
    while (j < size)
    {
        array[j++] = 0;
    }
}
int main()
{
    //Example 1
    int array[]{20, 0, 0, 3, 14, 0, 5, 11, 0, 0};
    int size = sizeof(array) / sizeof(array[0]);
    sortArray(array, size);
    cout << "Result :\n";
    for (int i = 0; i < size; i++)
    {
        cout << array[i] << " ";
    }
    cout << endl << "Press any key to exit...";
    cin.get();
    return 0;
}
It outputs correctly, but;
- I don't know what the speed of it actually is, can anyone help me figure out how to calculate that?
- I have no idea how to go about writing a function for "clarity"; any ideas?
 
     
     
     
     
    