Run Code
|
API
|
Code Wall
|
Users
|
Misc
|
Feedback
|
Login
|
Theme
|
Privacy
|
Patreon
Bubble Sort Example
// Bubble Sort Example #include <iostream> #include <stdlib.h> // rand srand using namespace std; #define SWAP(a,b) {int _temp_ = a; a = b; b = _temp_;} int numberOfCompares = 0; void bubbleSort(int *array, int n) { bool done = false; while (!done) { done = true; for (int i=1; i<n; i++) { numberOfCompares++; if (array[i] < array[i-1]) { SWAP(array[i],array[i-1]); done = false; } } } } int main() { int n, *array; cout << "Enter n: "; cin >> n; // create array of that size array = new int[n]; cout << endl << "n = " << n << endl << endl; srand(1111); cout << "Initial array = "; for (int i=0; i<n; i++) { array[i] = n - i; cout << array[i] << " "; } cout << endl; bubbleSort(array, n); cout << "Sorted array = "; for (int i=0; i<n; i++) { cout << array[i] << " "; } cout << endl; cout << endl << "Number of comparisons = " << numberOfCompares << endl; return 0; }
run
|
edit
|
history
|
help
0
Please
log in
to post a comment.
Throttle Example in C++
Store Information in Structure and Display it
Test titlu
HTML Timetable generator.cpp
problem_name_1
MY FIRST OBJECT ORIENTED PROGRAM
Zadanie Dejwu
virtual members
function pointer
Template arguments may contain calls to constexpr functions.
Please log in to post a comment.