a short BubbleSort algoritm for Integer Arrays source code

This snippet submitted by Major_Small on 2005-01-24. It has been viewed 45116 times.
Rating of 6.2 with 328 votes

 /*
    BubbleSort.h

    USAGE:
        create an integer array and determine the amount of elements in the
        array. then Call BubbleSort with the array's pointer and the amount of
        elements in the array. BubbleSort will then return a pointer to a sorted
        array.
        
    John Shao
    
********************************************************************************
    This work is hereby released into the Public Domain. To view a copy of the
    public domain dedication, visit
    http://creativecommons.org/licenses/publicdomain/
    
    or send a letter to
    
    Creative Commons
    559 Nathan Abbott Way
    Stanford, California 94305
    USA.
*******************************************************************************/

int* BubbleSort(int* arr,int size)
{
    int swap;

    while(size>0)
    {
        for(int i=0;i<size-1;i++)
        {
            if(arr[i]>arr[i+1])
            {
                swap=arr[i];
                arr[i]=arr[i+1];
                arr[i+1]=swap;
            }
        }
        --size;
    }

    return arr;
}

/******************************************************************************/
/*                   The Following is a Test Program                          */
/******************************************************************************/
/*
#include<iostream>

int main()
{
        int*data=new int[10];
        for(int i=0;i<10;i++)
        {
            std::cout<<'>';
            std::cin>>data[i];
            std::cin.get();
        }
        data=BubbleSort(data,10);
        for(int i=0;i<10;i++)
            std::cout<<data[i]<<' ';

        std::cout<<std::flush;
        std::cin.get();
        delete [] data;
        return 0;
}
*/
 




More C and C++ source code snippets