From 6e888bf4ea8571b5149bea822d5fd1f5e40a6ef4 Mon Sep 17 00:00:00 2001 From: Vedant Gautam Date: Mon, 7 Jun 2021 22:41:10 +0530 Subject: [PATCH 1/3] Updated Documentation --- docs/en/Sorting/Bubble-Sort.md | 57 ++++++++++++++++++++++++++++++++++ docs/en/Sorting/Merge-Sort.md | 17 +++++++--- 2 files changed, 70 insertions(+), 4 deletions(-) create mode 100644 docs/en/Sorting/Bubble-Sort.md diff --git a/docs/en/Sorting/Bubble-Sort.md b/docs/en/Sorting/Bubble-Sort.md new file mode 100644 index 000000000..ff9620c84 --- /dev/null +++ b/docs/en/Sorting/Bubble-Sort.md @@ -0,0 +1,57 @@ +# Bubble Sort + +Bubble Sort also known as Sinking Sort is the simplest sorting algorithm. It swaps the numbers if they are not in correct order. The Worst Case Time Complexity is O(n^2) + +## Steps + +1. Compares the first element with the next element. +2. If the first element is larger than the next element then the elements are swapped. +3. Step 2 is peformed untill the selected number is put to its correct position then the next element is compared. +4. Multiple passes are made untill the sorting is completed. + +## Example + +Given array is +**5 1 4 2 8** + +Sorted array is +**1 2 4 5 8** + +Steps +**First Pass** +.._ ( **5 1** 4 2 8 ) → ( **1 5** 4 2 8 ), Here, algorithm compares the first two elements, and swaps since 5 > 1. +.._ ( 1 **5 4** 2 8 ) → ( 1 **4 5** 2 8 ), Swap since 5 > 4 +.._ ( 1 4 **5 2** 8 ) → ( 1 4 **2 5** 8 ), Swap since 5 > 2 +.._ ( 1 4 2 **5 8** ) → ( 1 4 2 **5 8** ), Now, since these elements are already in order (8 > 5), algorithm does not swap them. + +**Second Pass** +.._ ( **1 4** 2 5 8 ) → ( **1 4** 2 5 8 ) +.._ ( 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 the algorithm does not know if it is completed. The algorithm needs one additional whole pass without any swap to know it is sorted. + +**Third Pass** +.._ ( 1 2 4 5 8 ) → ( 1 2 4 5 8 ) +.._ ( 1 2 4 5 8 ) → ( 1 2 4 5 8 ) +.._ ( 1 2 4 5 8 ) → ( 1 2 4 5 8 ) +.._ ( 1 2 4 5 8 ) → ( 1 2 4 5 8 ) + +## Implementation + +- [C](../../../algorithms/C/sorting/bubble-sort.c) +- [C++](../../../algorithms/CPlusPlus/Sorting/bubble-sort.cpp) +- [CSharp](../../../algorithms/CSharp/src/Sorts/bubble-sort.cs) +- [Go](../../../algorithms/Go/sorting/bubble-sort.go) +- [Java](../../../algorithms/Java/sorting/bubble-sort.java) +- [JavaScript](../../../algorithms/JavaScript/src/sorting/bubble-sort.js) +- [Python](../../../algorithms/Python/sorting/bubble_sort.py) + +## Video URL + +[Youtube Video about Bubble Sort](https://www.youtube.com/watch?v=Jdtq5uKz-w4&ab_channel=mycodeschool) + +## Others + +[Wikipedia](https://en.wikipedia.org/wiki/Bubble_sort) diff --git a/docs/en/Sorting/Merge-Sort.md b/docs/en/Sorting/Merge-Sort.md index d07b09fe2..02426d895 100644 --- a/docs/en/Sorting/Merge-Sort.md +++ b/docs/en/Sorting/Merge-Sort.md @@ -1,25 +1,34 @@ # Merge Sort + Merge Sort is a Divide and Conquer algorithm. It divides the input array into two halves, calls itself for the two halves, and then merges the two sorted halves. ## Steps + 1. Find the middle point to divide the array into two halves. 2. Call mergeSort for first half. 3. Call mergeSort for second half. 4. Merge the two halves sorted in step 2 and 3. ## Example -Given array is -**12 11 13 5 6 7** -Sorted array is +Given array is +**12 11 13 5 6 7** + +Sorted array is **5 6 7 11 12 13** ## Implementation + - [Java](../../../algorithms/Java/sorting/merge-sort.java) - [C](../../../algorithms/C/sorting/merge-sort.c) +- [C++](../../../algorithms/CPlusPlus/Sorting/merge-sort.cpp) +- [JavaScript](../../../algorithms/JavaScript/src/sorting/merge-sort.js) +- [Python](../../../algorithms/Python/sorting/merge_sort.py) ## Video URL -[Youtube Video about Merge Sort](https://www.youtube.com/watch?v=jlHkDBEumP0) + +[Youtube Video about Merge Sort](https://www.youtube.com/watch?v=jlHkDBEumP0) ## Others + [Wikipedia](https://en.wikipedia.org/wiki/Merge_sort) From 2742acf187171bf2f7287fa39132d159f152531a Mon Sep 17 00:00:00 2001 From: Vedant Gautam Date: Mon, 7 Jun 2021 22:51:11 +0530 Subject: [PATCH 2/3] updated identation --- docs/en/Sorting/Bubble-Sort.md | 27 +++++++++++++++------------ 1 file changed, 15 insertions(+), 12 deletions(-) diff --git a/docs/en/Sorting/Bubble-Sort.md b/docs/en/Sorting/Bubble-Sort.md index ff9620c84..3f9b05853 100644 --- a/docs/en/Sorting/Bubble-Sort.md +++ b/docs/en/Sorting/Bubble-Sort.md @@ -19,24 +19,27 @@ Sorted array is Steps **First Pass** -.._ ( **5 1** 4 2 8 ) → ( **1 5** 4 2 8 ), Here, algorithm compares the first two elements, and swaps since 5 > 1. -.._ ( 1 **5 4** 2 8 ) → ( 1 **4 5** 2 8 ), Swap since 5 > 4 -.._ ( 1 4 **5 2** 8 ) → ( 1 4 **2 5** 8 ), Swap since 5 > 2 -.._ ( 1 4 2 **5 8** ) → ( 1 4 2 **5 8** ), Now, since these elements are already in order (8 > 5), algorithm does not swap them. + +- ( **5 1** 4 2 8 ) → ( **1 5** 4 2 8 ), Here, algorithm compares the first two elements, and swaps since 5 > 1. +- ( 1 **5 4** 2 8 ) → ( 1 **4 5** 2 8 ), Swap since 5 > 4 +- ( 1 4 **5 2** 8 ) → ( 1 4 **2 5** 8 ), Swap since 5 > 2 +- ( 1 4 2 **5 8** ) → ( 1 4 2 **5 8** ), Now, since these elements are already in order (8 > 5), algorithm does not swap them. **Second Pass** -.._ ( **1 4** 2 5 8 ) → ( **1 4** 2 5 8 ) -.._ ( 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** ) + +- ( **1 4** 2 5 8 ) → ( **1 4** 2 5 8 ) +- ( 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 the algorithm does not know if it is completed. The algorithm needs one additional whole pass without any swap to know it is sorted. **Third Pass** -.._ ( 1 2 4 5 8 ) → ( 1 2 4 5 8 ) -.._ ( 1 2 4 5 8 ) → ( 1 2 4 5 8 ) -.._ ( 1 2 4 5 8 ) → ( 1 2 4 5 8 ) -.._ ( 1 2 4 5 8 ) → ( 1 2 4 5 8 ) + +- ( **1 2** 4 5 8 ) → ( **1 2** 4 5 8 ) +- ( 1 **2 4** 5 8 ) → ( 1 **2 4** 5 8 ) +- ( 1 2 **4 5** 8 ) → ( 1 2 **4 5** 8 ) +- ( 1 2 4 **5 8** ) → ( 1 2 4 **5 8** ) ## Implementation From 4d78b904296283df4b00b320e8eb89945169e382 Mon Sep 17 00:00:00 2001 From: Vedant Gautam Date: Tue, 8 Jun 2021 13:01:18 +0530 Subject: [PATCH 3/3] error check --- docs/en/Sorting/Bubble-Sort.md | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/docs/en/Sorting/Bubble-Sort.md b/docs/en/Sorting/Bubble-Sort.md index 3f9b05853..a917c6af4 100644 --- a/docs/en/Sorting/Bubble-Sort.md +++ b/docs/en/Sorting/Bubble-Sort.md @@ -6,8 +6,8 @@ Bubble Sort also known as Sinking Sort is the simplest sorting algorithm. It swa 1. Compares the first element with the next element. 2. If the first element is larger than the next element then the elements are swapped. -3. Step 2 is peformed untill the selected number is put to its correct position then the next element is compared. -4. Multiple passes are made untill the sorting is completed. +3. Step 2 is performed until the selected number is put to its correct position then the next element is compared. +4. Multiple passes are made until the sorting is completed. ## Example