The bubble sort algorithm - C++ source code - Learning C++
![]()
![]()
![]()
![]()
![]()
Precondition: the function accepts an unsorted array and integer size that is the size of the array:
Post condition: Sorted array
#include "stdafx.h"
#include<iostream.h>
int* BubbleSort ( int size, int array[]);
void exchange (int array[], int a, int b);
int main(int argc, char* argv[])
{
int array[10];
int i=0;
while (i < 10)
{
cout<<"Please enter a number."<<endl;
cin>>array[i];
i++;
}
cout<<"Unsorted array:"<<endl;
for(int k=0; k<10;k++)
cout<<array[k];
cout<<endl;
//call the sorting function
int* arr=BubbleSort (i,array);
cout<<"Sorted array:"<<endl;
for(int n=0; n<10;n++)
cout<<arr[n];
cout<<endl;
return 0;
}
int* BubbleSort ( int size, int array[])
{
int current = 0;
int* pointer;
bool sorted = false;
pointer=array;
while (( current < size)&&(sorted == false))
{
int walker = size-1;
sorted = true;
while (walker > current)
{
if ( array[walker] < array[walker - 1] )
{
sorted = false;
exchange( array, walker, walker-1);
}
walker--;
}
current++;
}
return pointer;
}
void exchange (int array[], int a, int b)
{
int temp=array[a];
array[a]=array[b];
array[b]=temp;
}
Please rate the tutorial
Learn SQL Programming By Examples [Kindle Edition]2.99
Learn PHP Programming by Examples [Kindle Edition] $2.99
Learn Visual Basic 6.0 [Kindle Edition] $1.99
How to Build Your Own Web Site from Scratch [Kindle Edition] $1.49
New-trip.com website source code
| Comments | |
|---|---|
- Home
- bubsortcpp
- Batch Files
- Java Properties
- Form Validation
- Display Image PHP
- Upload File PHP
- phpMyAdmin
- Environment Variables
- Delete Trojan horse
- C ++ Code Examples
- Start C++
- C ++ Continue
- C ++ Pointer
- Variable Scope
- Linked List
- Bubble Sort
- Two Bubble Sort
- Insertion Sort
- Selection Sort
- Two Selection Sort
- Database Design
- Password Keeper
- MySQL and Excel
- Learn MySQL
- Learn PHP
- Learn Visual Basic
- Learn Java
- Web Site Tips
- User_Auth. Demo
- Merge Bin Sort
- Advertise here
- Using Twitter
- Site map
- Registration
Web programming Tips