Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
-
Economics & Finance
Python Articles
Page 692 of 855
Python Program for Recursive Insertion Sort
In this article, we will learn about implementing insertion sort using recursion. Recursive insertion sort breaks down the sorting problem into smaller subproblems by recursively sorting the first n-1 elements and then inserting the last element in its correct position. Problem statement − We are given an array, we need to sort it using the concept of recursive insertion sort. Insertion sort works by building a sorted portion of the array one element at a time. In the recursive approach, we sort the first n-1 elements recursively, then insert the nth element into its correct position. Algorithm ...
Read MorePython Program for QuickSort
QuickSort is a divide-and-conquer sorting algorithm that works by selecting a pivot element and partitioning the array around it. Elements smaller than the pivot go to the left, and larger elements go to the right. How QuickSort Works The algorithm follows these steps ? Step 1: Choose Pivot 5 2 8 3 7 (Pivot: 7) ...
Read MorePython Program for Detect Cycle in a Directed Graph
A cycle occurs when a path starts and ends at the same vertex, following the direction of the edges. In directed graphs, cycles can cause problems like infinite loops or dependency errors, so detecting them is important in areas like task scheduling and deadlock detection. We can use Depth-First Search (DFS) with a recursion stack to detect whether a cycle exists in a directed graph or not. Problem Statement You are given a directed graph represented using an adjacency list. Your task is to write a Python program to check whether the graph contains a cycle or ...
Read MorePython Program for Cutting a Rod
The Rod Cutting problem is a classic dynamic programming challenge where we cut a rod into pieces to maximize total value. Given a rod of length n and prices for different lengths, we find the optimal way to cut the rod for maximum profit. Problem Statement You are given a rod of length n and an array price[] where price[i] represents the price of a rod of length i+1. Your task is to determine the maximum value obtainable by cutting up the rod and selling the pieces. Using Recursive Approach The recursive approach tries all possible ...
Read MorePython Program for Cocktail Sort
Cocktail sort is a variation of bubble sort that sorts the array in both directions on each pass. It is also known as Bidirectional Bubble Sort or Shaker Sort. The algorithm works by traversing the list forward and backward alternatively, pushing the largest and smallest elements to their respective ends. This approach helps in reducing the number of iterations compared to Bubble Sort, especially when smaller elements are at the end of the list. How Cocktail Sort Works Cocktail Sort improves upon Bubble Sort by sorting in both directions during each pass − ...
Read MorePython Program for BogoSort or Permutation Sort
BogoSort, also known as Permutation Sort or Stupid Sort, is a highly inefficient sorting algorithm that works by generating random permutations of a list until it becomes sorted. Despite being impractical for real-world use, it serves as an interesting example of how not to design algorithms. BogoSort continues to shuffle the array randomly until the list becomes sorted. The expected time complexity is unbounded, making its average performance extremely poor compared to efficient algorithms like QuickSort or MergeSort. Algorithm Overview The BogoSort algorithm follows these simple steps ? Check if the list ...
Read MorePython Program for Binary Insertion Sort
Binary insertion sort is an improved version of the regular insertion sort algorithm. In normal insertion sort, each element is compared linearly with the sorted portion to find its correct position, taking O(n) comparisons per element. Binary insertion sort uses binary search to find the correct insertion position, reducing comparisons from O(n) to O(log n) per element. However, shifting elements still requires O(n) time in the worst case. How Binary Insertion Sort Works The algorithm combines binary search with insertion sort ? Start from the second element (index 1) ...
Read MorePython Program for array rotation
In this article, we will learn how to rotate an array by a given number of positions. Array rotation is a fundamental operation where elements are shifted left or right, with elements that fall off one end being placed at the other end. Problem statement − Given an array and a number of positions, we need to rotate the array elements by the specified number of positions. Original Array: 1 2 3 ...
Read MorePython Program for Activity Selection Problem
The activity selection problem selects the maximum number of non-overlapping activities from a given set. Each activity has a start and finish time, and a single person can perform only one activity at a time. Problem Statement You are given n activities, each defined by a start time and a finish time. The goal is to choose the maximum number of activities that a single person can perform without any overlap between the selected activities. Variable Notations Below are the variables used in the problem definition − N: Total number ...
Read MorePython Program for 0-1 Knapsack Problem
The 0-1 Knapsack Problem is a classic optimization problem where you have a knapsack with limited capacity and items with specific weights and values. The goal is to select items that maximize value without exceeding the weight limit, where each item can only be taken once (0 or 1). Problem Types There are three main variants of the knapsack problem − 0-1 Knapsack: Each item can either be selected or not (0 or 1) Fractional Knapsack: Items can be broken into fractions Unbounded Knapsack: ...
Read More