Simple Bubble Sort Program using functions in C++
Definition
Bubble sort, sometimes referred to as sinking sort, is a simple sorting algorithm that repeatedly steps through the list to be sorted, compares each pair of adjacent items and swaps them if they are in the wrong order. The pass through the list is repeated until no swaps are needed, which indicates that the list is sorted. It can be practical if the input is usually in sort order but may occasionally have some out-of-order elements nearly in position.
Example Program
/* Simple Bubble Sort Program Using Functions in C++*/
/* Data Structure C++ Programs,C++ Array Examples */
#include <iostream>
#include<conio.h>
#include<stdlib.h>
#define MAX_SIZE 5
using namespace std;
void bubble_sort(int[]);
int main() {
int arr_sort[MAX_SIZE], i;
cout << "Simple C++ Bubble Sort Example - Array and Functions\n";
cout << "\nEnter " << MAX_SIZE << " Elements for Sorting : " << endl;
for (i = 0; i < MAX_SIZE; i++)
cin >> arr_sort[i];
cout << "\nYour Data :";
for (i = 0; i < MAX_SIZE; i++) {
cout << "\t" << arr_sort[i];
}
bubble_sort(arr_sort);
getch();
}
void bubble_sort(int fn_arr[]) {
int i, j, a, t;
for (i = 1; i < MAX_SIZE; i++) {
for (j = 0; j < MAX_SIZE - 1; j++) {
if (fn_arr[j] > fn_arr[j + 1]) {
//Swapping Values
t = fn_arr[j];
fn_arr[j] = fn_arr[j + 1];
fn_arr[j + 1] = t;
}
}
cout << "\nIteration : " << i;
for (a = 0; a < MAX_SIZE; a++) {
cout << "\t" << fn_arr[a];
}
}
cout << "\n\nSorted Data :";
for (i = 0; i < MAX_SIZE; i++) {
cout << "\t" << fn_arr[i];
}
}
Sample Output
Simple Bubble Sort Example - Array and Functions
Enter 5 Elements for Sorting
677
45
32
1
17
Your Data : 677 45 32 1 17
Iteration 1 : 45 32 1 17 677
Iteration 2 : 32 1 17 45 677
Iteration 3 : 1 17 32 45 677
Iteration 4 : 1 17 32 45 677
Sorted Data : 1 17 32 45 677
Sorting Programs
- Simple Bubble Sort Program in C++
- Simple Bubble Sort Program using functions in C++
- Simple Insertion Sort Program in C++
- Simple Insertion Sort Program using functions in C++
- Simple Selection Sort Program in C++
- Simple Selection Sort Program using functions in C++
- Simple Quick Sort Program in C++
- Simple Merge Sort Program in C++
- Simple Shell Sort Program in C++
- Simple Shell Sort Program using functions in C++
- Simple Heap Sort Program in C++
Read More Articles
- Simple Merge Sort Program in C++
- Scope Resolution Operator In C++
- Simple Program for Virtual Functions Using C++ Programming
- Simple Class Example Program For Find Prime Number In C++
- Simple Example Program For Parameterized Constructor In C++
- Define Constructor in Outside Class Example Program In C++
- Simple Program for Function Overloading Using C++ Programming
- Simple Program for Single Inheritance Using C++ Programming
- Simple Example Program For Copy Constructor In C++
- Simple Program for Inline Function without Class Using C++ Programming
- Simple Example Program For Constructor Overloading In C++
- Simple Example Program for Inline Function Using C++ Programming
- Factorial Using Function Example Program In C++
- Simple Addition ( Add Two Integers ) Example Program
- Simple Example Program For Constructor In C++
- Simple Stack Program in C++ Programming
- Simple Program for Read user Input Using cin
- Factorial Using Loop Example Program In C++
- Simple Program for Friend Function Using C++ Programming
- Simple Program for Static Data and Member Function Using C++ Programming
- Simple Program for Multiple Inheritance Using C++ Programming
- Simple Program for Unary Operator Overloading Using C++ Programming
- Simple Copy Constructor Example Program For Find Factorial In C++
- Do While Loop Example Program In C++
- Simple Program for Function Template Using C++ Programming