From cfc4e5ed8f9cd989938530a42f286eed3bf7e75c Mon Sep 17 00:00:00 2001 From: spyic <67299567+spyic@users.noreply.github.com> Date: Sun, 4 Oct 2020 19:22:25 +0530 Subject: [PATCH] Create Bubble_sort Bubble Sort is the simplest sorting algorithm --- Bubble_sort | 44 ++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 44 insertions(+) create mode 100644 Bubble_sort diff --git a/Bubble_sort b/Bubble_sort new file mode 100644 index 0000000..069a806 --- /dev/null +++ b/Bubble_sort @@ -0,0 +1,44 @@ +// C++ program for implementation of Bubble sort +#include +using namespace std; + +void swap(int *xp, int *yp) +{ + int temp = *xp; + *xp = *yp; + *yp = temp; +} + +// A function to implement bubble sort +void bubbleSort(int arr[], int n) +{ + int i, j; + for (i = 0; i < n-1; i++) + + // Last i elements are already in place + for (j = 0; j < n-i-1; j++) + if (arr[j] > arr[j+1]) + swap(&arr[j], &arr[j+1]); +} + +/* Function to print an array */ +void printArray(int arr[], int size) +{ + int i; + for (i = 0; i < size; i++) + cout << arr[i] << " "; + cout << endl; +} + +// Driver code +int main() +{ + int arr[] = {64, 34, 25, 12, 22, 11, 90}; + int n = sizeof(arr)/sizeof(arr[0]); + bubbleSort(arr, n); + cout<<"Sorted array: \n"; + printArray(arr, n); + return 0; +} + +// This code is contributed by rathbhupendra