From e7883ae3af93a87d52854aac830972a2cdb45150 Mon Sep 17 00:00:00 2001 From: jainaman224 Date: Thu, 2 Jun 2016 18:29:00 +0530 Subject: [PATCH 1/3] Added C++ implementation to bubble-sort --- Algorithms-BubbleSort.md | 35 ++++++++++++++++++++++++++++++++--- 1 file changed, 32 insertions(+), 3 deletions(-) diff --git a/Algorithms-BubbleSort.md b/Algorithms-BubbleSort.md index d4e25a368..a1e0b44bd 100644 --- a/Algorithms-BubbleSort.md +++ b/Algorithms-BubbleSort.md @@ -21,7 +21,7 @@ Second Pass: ( 1 4 2 5 8 ) –> ( 1 2 4 5 8 ), Swap since 4 > 2 ( 1 2 4 5 8 ) –> ( 1 2 4 5 8 ) ( 1 2 4 5 8 ) –> ( 1 2 4 5 8 ) -Now, the array is already sorted, but our algorithm does not know if it is completed. The algorithm needs one whole pass without any +Now, the array is already sorted, but our algorithm does not know if it is completed. The algorithm needs one whole pass without any swap to know it is sorted. Third Pass: @@ -31,7 +31,36 @@ Third Pass: ( 1 2 4 5 8 ) –> ( 1 2 4 5 8 ) ``` +#### C++ Implementation + +```c++ +// Function to implement bubble sort +void bubble_sort(int array[], int n) +{ + // Here n is the number of elements in array + int temp; + + for(int i = 0; i < n-1; i++) + { + // Last i elements are already in place + for(int j = 0; j < n-i-1; j++) + { + if (array[j] > array[j+1]) + { + // swap elements at index j and j+1 + temp = array[j]; + array[j] = array[j+1]; + array[j+1] = temp; + } + } + } +} +``` + +:rocket: [Run Code](https://repl.it/CXif) + #### Python Implementation + ```python def bubble_sort(arr): exchanges = True # A check to see if the array is already sorted so that no further steps gets executed @@ -43,7 +72,7 @@ def bubble_sort(arr): exchanges = True arr[j], arr[j+1] = arr[j+1], arr[j] i -= 1 - + arr = [5,3,23,1,43,2,54] bubble_sort(arr) print(arr) # Prints [1, 2, 3, 5, 23, 43, 54] @@ -54,4 +83,4 @@ print(arr) # Prints [1, 2, 3, 5, 23, 43, 54] **Worst and Average Case Time Complexity:** O(n*n). Worst case occurs when array is reverse sorted i.e. the elements are in decreasing order -**Best Case Time Complexity:** O(n). Best case occurs when array is already sorted. +**Best Case Time Complexity:** O(n). Best case occurs when array is already sorted. From fb929f65a4468ba290703a3ff5303d5fda17cd5c Mon Sep 17 00:00:00 2001 From: jainaman224 Date: Thu, 2 Jun 2016 19:51:32 +0530 Subject: [PATCH 2/3] implemented suggestion --- Algorithms-BubbleSort.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Algorithms-BubbleSort.md b/Algorithms-BubbleSort.md index a1e0b44bd..8845d5e7a 100644 --- a/Algorithms-BubbleSort.md +++ b/Algorithms-BubbleSort.md @@ -1,4 +1,4 @@ -# BubbleSort +# Algorithm Bubble Sort Bubble sort is a simple sorting algorithm. This sorting algorithm is comparison based algorithm in which each pair of adjacent elements is compared and elements are swapped if they are not in order. This algorithm does sorting in-place i.e. it does not creates a new array while From c6b056b95214ab895fa796847e101ac104b508a3 Mon Sep 17 00:00:00 2001 From: jainaman224 Date: Thu, 2 Jun 2016 19:54:30 +0530 Subject: [PATCH 3/3] implemented suggestion --- Algorithms-BubbleSort.md => Algorithms-Bubble-Sort.md | 0 1 file changed, 0 insertions(+), 0 deletions(-) rename Algorithms-BubbleSort.md => Algorithms-Bubble-Sort.md (100%) diff --git a/Algorithms-BubbleSort.md b/Algorithms-Bubble-Sort.md similarity index 100% rename from Algorithms-BubbleSort.md rename to Algorithms-Bubble-Sort.md