A short BubbleSort algoritm for Integer Arrays

Submitted by:Tim Kelly

Date added:29 December, 2014

Category:C++

Example code for a short BubbleSort algoritm for Integer Arrays

Tags: short bubblesort , algoritm , integer arrays

Code Snippet:

/*
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;
}
*/
 
 

Comments