Bubble Sort - Detailed Version
itsLu
c_cpp
a year ago
1.0 kB
8
Indexable
#include <iostream> using namespace std; void Bubble_Sort (int arr[], int arraySize) { int p, c, temp, cCounter = 0, swapCounter = 0; bool isSwapped; for (p = 0 ; p < arraySize - 1 ; p++) { isSwapped = false; for (c = 0 ; c < arraySize - p - 1 ; c++) { if (arr[c] > arr[c+1]) { temp = arr[c]; arr[c] = arr[c+1]; arr[c+1] = temp; isSwapped = true; swapCounter++; } cCounter++; } if (isSwapped == false) break; } cout << endl << "Number of Comparisons: " << cCounter << "\tNumber of Swaps: " << swapCounter << endl; } int main() { int arr[5] = {5, 4, 3, 2 , 1}; Bubble_Sort(arr, 5); //اللوب الجاية دي عشان تطبع الأراي بعد الترتيب ممكن تكرفله عادي for (int k = 0 ; k < 5 ; k++) cout << arr[k] << "\t"; }
Editor is loading...
Leave a Comment