Run Code
|
API
|
Code Wall
|
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
7
MY FIRST OBJECT ORIENTED PROGRAM
Array-Based Heap Example Solution
virtual members
std::function copies
why
auto Keyword Example
Union-Struct-uint32_t byte order
pack expansion
ljblblljkl