1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52
|
//BubbleSort.cpp
//Program that sorts an array of 10 elements.
#include <iostream>
using std::cout;
using std::endl;
void BubbleSort(int array[],int size); //function prototype
void printArray(const int array[],int size); //function prototype
int main(){
const int SIZE=15;
int myArray[SIZE]={1,3,5,2,6,4,7,10,9,8,15,12,14,11,13};
cout<<"\nUnsorted array"<<endl;
printArray(myArray,SIZE);
cout<<"\nSorting the array"<<endl;
BubbleSort(myArray,SIZE);
return 0; //indicates success
}//end main
void BubbleSort(int array[],int size){
bool swap=false;
int aux=size;
for(int times=0;times<size-1;times++){
for(int i=0;i<aux;i++){
if(array[i+1]<array[i]){
int aux;
aux=array[i];
array[i]=array[i+1];
array[i+1]=aux;
swap=true;
}//end if
}//end inner for
aux--;
if(swap){
swap=false;
printArray(array,size);
}else{
break;
}//end if...else
}//end outer for
}//end function bubble sort
void printArray(const int array[],int size){
for(int index=0;index<size;index++)
cout<<array[index]<<' ';
cout<<endl;
}//end function printArray
|