Javatpoint Logo
Javatpoint Logo

Python Algorithms

An algorithm goes further than computational thinking. It is a step-by-step process that specifies a list of commands to be carried out in a specific order to get the intended result. We can execute a single algorithm in many programming languages because algorithms are typically constructed irrespective of the supporting languages.

What are Algorithms?

Algorithms are finite sets of rules developed to be executed in the defined sequence to solve problems and produce the desired outcomes, a. They provide problem-solving pseudocode before writing the main code and are not written in support of any particular language, so we can use them with many different languages.

How do you Write Algorithms?

Typically, user-friendly language and a few popular coding languages are used to write algorithms. Although they are frequently documented in steps, it is not necessarily required to do so. There are no set guidelines for creating algorithms, but you should bear the following things in mind:

  1. Determine what the question is precisely.
  2. Choose where you should begin.
  3. Identify the necessary stopping point.
  4. Create the stepping stones in between.
  5. Examine your actions.

Binary Search

In short, this search method utilizes a set of components that have already been sorted by dismissing half after only one comparison.

  • X and the middle element are compared.
  • We return the middle element's index if x matches the middle element.
  • Otherwise, if x does not match with the middle element and it is bigger than the middle element, it can only be located in the right half (where elements are greater) subarray. The method is then used once more for the right side.
  • The x must be located in the left half (where elements are smaller) if x is smaller. The method is then used for the left side.

Code

Output:

The given element 59 is present at the index 4
The given element 35 is not present in the array

Linear Search

A straightforward method is to perform a linear search, that is, to compare every member of the array[] one at a time to x.

Return the position if x matches any element of the array.

Returning -1 if x does not match any one of the array's elements.

Code

Output:

The given element 59 is present at the index 4
The given element 35 is not present in the array

Insertion Sort

A straightforward sorting algorithm called insertion sort operates similarly to how we arrange playing cards.

Code

Output:

The sorted array is:
[3, 19, 23, 36, 42, 49, 83]

Selection Sort

The selection sort technique sorts any given array by repeatedly picking the element with the lowest value out of the unsorted segment and placing it at the beginning (while making an ascending order). The lowest element from the unsorted subarray is always selected and moved to the arranged subarray during a selection sort (while making an ascending order).

Code

Output:

The sorted array is:
[3, 19, 23, 36, 42, 49, 83]

Bubble Sort

If the items are in the wrong order, the simplest sorting method, Bubble Sort, repeatedly swaps nearby components.

Code

Output:

The sorted array is:
[3, 19, 23, 36, 42, 49, 83]

Merge Sort

A Divide-and-Conquer technique is Merge Sort. The original array is split in half, recursive calls are made for each half, and the two ordered halves are combined. To combine two portions, we will use the for a loop.

Code

Output:

The given array is
[23, 42, 3, 83, 36, 49, 19]
The sorted array is:
[3, 19, 23, 36, 42, 49, 83]

Quick Sort

Quick Sort is a Divide-and-conquer algorithm, just like Merge Sort. It chooses an element to act as the hinge and divides the supplied array from the chosen hinge. There are numerous variations of quickSort that select hinges in various ways.

Code

Output:

The sorted array is: [3, 19, 23, 36, 42, 49, 83]

Next TopicPython descriptors





Youtube For Videos Join Our Youtube Channel: Join Now

Feedback


Help Others, Please Share

facebook twitter pinterest

Learn Latest Tutorials


Preparation


Trending Technologies


B.Tech / MCA