Coder Social home page Coder Social logo

zaahidali / algorithms-python Goto Github PK

View Code? Open in Web Editor NEW
0.0 2.0 0.0 6 KB

This repository contains Python implementations of various sorting and searching algorithms. Explore the code examples and learn how these algorithms work.

Jupyter Notebook 100.00%
python3 bubble-sort linear-search binary-search binarysearch bubblesort linearsearch python-programming searching-algorithms sorting-algorithms

algorithms-python's Introduction

Bubble Sort and Searching Algorithms

This repository contains Python implementations of the Bubble Sort algorithm and various searching algorithms, including Linear Search and Binary Search.

Bubble Sort Algorithm

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

Usage

Here is an example usage of the Bubble Sort algorithm:

l1 = [4,2,1,6,7,5]

lsorted=True
while lsorted==True:
    lsorted=False
    for i in range(len(l1)-1):
        if l1[i] > l1[i+1]:
            l1[i],l1[i+1] = l1[i+1],l1[i]
            lsorted=True
print(l1)

Output:

[1, 2, 4, 5, 6, 7]

Linear Search Algorithm

The Linear Search algorithm is a simple searching algorithm that sequentially checks each element in a list until a match is found or the end of the list is reached.

Usage

Here is an example usage of the Linear Search algorithm:

l2 = [5,4,7,8,9,3,2,1,11,14,198,412,25,6]
sno = 6
element = -1
for i in range(len(l2)):
    if l2[i] == sno:
        element = l2[i]
        print("The element "+str(l2[i])+": found at index: "+str(i))
        break
if element== -1:
    print("The element does not exist")

Output:

The element 6: found at index: 13

Binary Search Algorithm

The Binary Search algorithm is a more efficient searching algorithm for sorted lists. It works by repeatedly dividing the search interval in half until the target value is found or the interval is empty.

Usage

Here is an example usage of the Binary Search algorithm:

l = [2, 3, 5, 8, 11, 12, 18]
search_for = 11
slice_start = 0
slice_end = len(l) - 1
found = False

while slice_start <= slice_end and not found:
    location = (slice_start + slice_end) // 2
    if l[location] == search_for:
        found = True
        print("The value found at index : "+str(location))
    else:
        if search_for < l[location]:
            slice_end = location - 1
        else:
            slice_start = location + 1
print(found)
print(location)

Output:

The value found at index : 4
True
4

Feel free to explore and modify the code to understand the algorithms better.

License

This repository is licensed under the MIT License.

algorithms-python's People

Contributors

zaahidali avatar

Watchers

 avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.