[ create a new paste ] login | about

Link: http://codepad.org/YeU6tmzB    [ raw code | output | fork ]

C++, pasted on Jun 8:
#include <iostream>

template <typename T>
void printArray(T * array, int size, const std::string& space)
{
    for (int i = 0; i != size; ++i)
    {
        if (i != size - 1)
            std::cout << array[i] << space;
        else
            std::cout << array[i];
    }

    std::cout << std::endl;
}

template <typename T>
void numberSort(T * array, int size)
{
    bool bDone = false;

    printArray(array, size, "-");

    while (!bDone)
    {
        bDone = true;

        for (int i = 0; i != size - 1; ++i)
        {
            if ( array[i] > array[i + 1] )
            {
                T tmp = array[i];
                array[i] = array[i+1];
                array[i+1] = tmp;
                bDone = false;

                printArray(array, size, "-");
            }
        }
    }

    printArray(array, size, "-");
}

int main()
{
    int array[] = { 50, 10, 20, 1, 5 }; //Array to sort
    int size = sizeof(array)/sizeof(int);

    numberSort<int> (array, size);
}


Output:
1
2
3
4
5
6
7
8
9
10
50-10-20-1-5
10-50-20-1-5
10-20-50-1-5
10-20-1-50-5
10-20-1-5-50
10-1-20-5-50
10-1-5-20-50
1-10-5-20-50
1-5-10-20-50
1-5-10-20-50


Create a new paste based on this one


Comments: