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
Server Side Programming Articles
Page 585 of 2109
Python Program for Triangular Matchstick Number
In this article, we will learn how to calculate the total number of matchsticks required to build a triangular pyramid. Given the number of floors X, we need to find the total matchsticks needed to construct the entire pyramid structure. Problem statement − We are given a number X which represents the floor of a matchstick pyramid, we need to display the total number of matchsticks required to form a pyramid of matchsticks with X floors. Understanding the Pattern In a triangular matchstick pyramid: Floor 1 requires 3 matchsticks (forming 1 triangle) Floor 2 requires ...
Read MorePython Program for Subset Sum Problem
In this article, we will learn how to solve the Subset Sum Problem using Python. This is a classic computer science problem where we need to determine if there exists a subset of given numbers that adds up to a target sum. Problem Statement: Given a set of non-negative integers and a target sum, determine if there exists a subset of the given set with a sum equal to the target sum. Naive Recursive Approach The recursive approach explores all possible subsets by either including or excluding each element ? def subset_sum_recursive(numbers, n, target_sum): ...
Read MorePython Program for Stooge Sort
In this article, we will learn about the Stooge Sort algorithm and implement it in Python. Stooge Sort is a recursive sorting algorithm that divides the array into overlapping parts and sorts them multiple times to ensure correctness. Problem statement − We are given an array, we need to sort it using stooge sort. Algorithm The Stooge Sort algorithm follows these steps ? 1. Check if value at index 0 is greater than value at last index, then swap them. 2. Sort the initial 2/3rd of the array. 3. Sort the last 2/3rd of the ...
Read MorePython Program for Sieve of Eratosthenes
The Sieve of Eratosthenes is an ancient algorithm for finding all prime numbers up to a given limit. It works by iteratively marking the multiples of each prime as composite, leaving only the prime numbers unmarked. Problem statement − We are given a number n, we need to print all primes smaller than or equal to n. Constraint: n is a small number. Algorithm Overview The algorithm follows these steps: Create a boolean array of size n+1, initially all True Mark 0 and 1 as False (not prime) For each number p from 2 to √n, ...
Read MorePython 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 More