Bubble Sort Program

Posted: July 22, 2011 in Uncategorized

Here is a program that demostrates a bubble sort:

 

#include <iostream>


using namespace std;

void printArray(int *array, int length);
void bubbleSort(int *array, int length);

int main()
{
  int array[] = {9,6,5,23,2,6,2,7,1,8};
  bubbleSort(array, 10);
  printArray(array, 10);
}

void printArray(int *array, int length)
{
  int i = 0;
  for(i =0; i < length; i++)
    cout << array[i] << endl;
}

void bubbleSort(int *array, int length)
{
  int i, j;
  for(i = 0; i < length; i++)
    {
      for(j = 0; j < i; j++)
	{
	  if(array[i] > array[j])
	    {
	      int temp = array[i];
	      array[i] = array[j];
	      array[j] = temp;
	    }
	}
    }
}
About these ads

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s