From b8cd8b10a9c990eb5906b8bfe551c63bcc5bb8ac Mon Sep 17 00:00:00 2001 From: Ansh Verma Date: Thu, 3 Oct 2024 18:13:51 +0000 Subject: [PATCH] first commit --- BubbleSort.cpp | 112 ++++++++++++++++++++++--------------------------- 1 file changed, 51 insertions(+), 61 deletions(-) diff --git a/BubbleSort.cpp b/BubbleSort.cpp index 6199992..ec949be 100644 --- a/BubbleSort.cpp +++ b/BubbleSort.cpp @@ -1,69 +1,59 @@ -/* -Bubble Sort is comparison based sorting algorithm. In this algorithm adjacent elements are compared and swapped to make correct sequence. This algorithm is simpler than other algorithms, but it has some drawbacks also. This algorithm is not suitable for large number of data set. It takes much time to solve the sorting tasks. +#include +#include -The complexity of Bubble Sort Technique -Time Complexity: O(n) for best case, O(n2) for average and worst case +using namespace std; -Space Complexity: O(1) +void swapping(int &a, int &b) { // Swap the contents of a and b + int temp = a; + a = b; + b = temp; +} -Input − A list of unsorted data: 56 98 78 12 30 51 -Output − Array after Sorting: 12 30 51 56 78 98 +void display(const vector& array) { + for (int value : array) { + cout << value << " "; + } + cout << endl; +} -Algorihtm:- +void bubbleSort(vector& array) { + int n = array.size(); + bool swapped; -Begin - for i := 0 to size-1 do - flag := 0; - for j:= 0 to size –i – 1 do - if array[j] > array[j+1] then - swap array[j] with array[j+1] - flag := 1 - done - if flag ≠ 1 then - break the loop. - done -End + for (int i = 0; i < n - 1; i++) { + swapped = false; + int lastSwapIndex = n - 1; // Track the last swap index -Example Code:- -*/ -#include -using namespace std; -void swapping(int &a, int &b) { //swap the content of a and b - int temp; - temp = a; - a = b; - b = temp; -} -void display(int *array, int size) { - for(int i = 0; i array[j+1]) { //when the current item is bigger than next - swapping(array[j], array[j+1]); - swaps = 1; //set swap flag - } - } - if(!swaps) - break; // No swap in this pass, so array is sorted - } + for (int j = 0; j < lastSwapIndex; j++) { + if (array[j] > array[j + 1]) { // When the current item is bigger than the next + swapping(array[j], array[j + 1]); + swapped = true; // Set swap flag + lastSwapIndex = j; // Update last swap position + } + } + + if (!swapped) { + break; // No swap in this pass, so array is sorted + } + } } + int main() { - int n; - cout << "Enter the number of elements: "; - cin >> n; - int arr[n]; //create an array with given number of elements - cout << "Enter elements:" << endl; - for(int i = 0; i> arr[i]; - } - cout << "Array before Sorting: "; - display(arr, n); - bubbleSort(arr, n); - cout << "Array after Sorting: "; - display(arr, n); -} //above programme works fine don't need to do anything + int n; + cout << "Enter the number of elements: "; + cin >> n; + + vector arr(n); // Create a vector with the given number of elements + cout << "Enter elements:" << endl; + for (int i = 0; i < n; i++) { + cin >> arr[i]; + } + + cout << "Array before Sorting: "; + display(arr); + bubbleSort(arr); + cout << "Array after Sorting: "; + display(arr); + + return 0; +}