codezonediitj / pydatastructs

A python package for data structures and algorithms
https://pydatastructs.readthedocs.io/en/stable/
Other
201 stars 267 forks source link

Added Bubble Sort Algorithm #437

Closed jaythorat closed 2 years ago

jaythorat commented 2 years ago

Short Discription

Bubble sort is a simple sorting algorithm that repeatedly steps through the list, compares adjacent elements, and swaps them if they are in the wrong order.

Brief description of what is fixed or changed

Take an array of numbers "5 1 4 2 8", and sort the array from lowest number to greatest number using bubble sort. In each step, elements written in bold are being compared. Three passes will be required;

First Pass ( 5 1 4 2 8 ) → ( 1 5 4 2 8 ), Here, the 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), the 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 )

Other comments

REF LINK - https://en.wikipedia.org/wiki/Bubble_sort

jaythorat commented 2 years ago

FAILED pydatastructs/linear_data_structures/tests/test_algorithms.py::test_bubble_sort - assert (15, 13, 31) == (12, 13, 31)

Getting the same error in local tests too.

czgdp1807 commented 2 years ago

Feel free to move ahead. Ignore this error. I will check on Sunday.

jaythorat commented 2 years ago

Feel free to move ahead. Ignore this error. I will check on Sunday.

Sure!

codecov[bot] commented 2 years ago

Codecov Report

Merging #437 (3b6a8cc) into master (bae1020) will increase coverage by 0.014%. The diff coverage is 100.000%.

@@              Coverage Diff              @@
##            master      #437       +/-   ##
=============================================
+ Coverage   98.573%   98.587%   +0.014%     
=============================================
  Files           29        29               
  Lines         3646      3753      +107     
=============================================
+ Hits          3594      3700      +106     
- Misses          52        53        +1     
Impacted Files Coverage Δ
pydatastructs/linear_data_structures/__init__.py 100.000% <ø> (ø)
pydatastructs/linear_data_structures/algorithms.py 99.740% <100.000%> (+0.019%) :arrow_up:
pydatastructs/graphs/adjacency_matrix.py 97.916% <0.000%> (-2.084%) :arrow_down:
pydatastructs/graphs/adjacency_list.py 98.000% <0.000%> (-2.000%) :arrow_down:
...tructs/miscellaneous_data_structures/algorithms.py 94.000% <0.000%> (-1.834%) :arrow_down:
docs/source/conf.py 100.000% <0.000%> (ø)
pydatastructs/__init__.py 100.000% <0.000%> (ø)
pydatastructs/graphs/graph.py 100.000% <0.000%> (ø)
pydatastructs/strings/trie.py 100.000% <0.000%> (ø)
pydatastructs/utils/__init__.py 100.000% <0.000%> (ø)
... and 15 more

Impacted file tree graph

czgdp1807 commented 2 years ago

Thanks a lot for working on this. I think we missed to add bubble sort to documentation website code. It would be great if you can make a follow up PR for adding a one line in https://github.com/codezonediitj/pydatastructs/blob/master/docs/source/pydatastructs/linear_data_structures/algorithms.rst. May be adding after, .. autofunction:: pydatastructs.quick_sort should work.

jaythorat commented 2 years ago

Yeah sure!!