Found 10784 Articles for Python

Python Program for QuickSort

Pavitra
Updated on 20-Dec-2019 06:46:04

6K+ Views

In this article, we will learn about the solution to the problem statement given below.Problem statement − We are given an array, we need to sort it using the concept of quicksortHere we first partition the array and sort the separate partition to get the sorted array.Now let’s observe the solution in the implementation below −Example Live Demo# divide function def partition(arr,low,high):    i = ( low-1 )    pivot = arr[high] # pivot element    for j in range(low , high):       # If current element is smaller       if arr[j]

Python Program for Odd-Even Sort / Brick Sort

Pavitra
Updated on 20-Dec-2019 06:43:29

198 Views

In this article, we will learn about the solution to the problem statement given below.Problem statement − We are given an array, we need to sort it using brick sort.Here we have two phases: Odd and Even Phase. In the odd phase, bubble sort is performed on odd indexed elements and in the even phase, bubble sort is performed on even indexed elements.Now let’s observe the solution in the implementation below−Exampledef oddEvenSort(arr, n):    # flag    isSorted = 0    while isSorted == 0:       isSorted = 1       temp = 0       ... Read More

Python Program for Number of stopping station problem

Pavitra
Updated on 20-Dec-2019 06:39:26

238 Views

In this article, we will learn about the solution to the problem statement given below.Problem statement − We are given that there are 13 intermediate stations between two places A and B. We need to find the number of ways in which a train can be stopped at 2 intermediate stations, such that there are no consecutive stations?Now let’s observe the solution in the implementation below −Example Live Demo# stop station def stopping_station( p, n):    num = 1    dem = 1    s = p    # selecting specified position    while p != 1:       dem ... Read More

Python Program for Min Cost Path

Pavitra
Updated on 20-Dec-2019 06:36:28

654 Views

In this article, we will learn about the solution to the problem statement given below.Problem statement − We are given a cost matrix and a position (m, n), we need to find the cost of minimum cost path to reach (m, n) from (0, 0). Each cell represents a cost to traverse from one cell to another.Now let’s observe the solution in the implementation below −Example Live Demo# dynamic approach R = 3 C = 3 def minCost(cost, m, n):    # initialization    tc = [[0 for x in range(C)] for x in range(R)]    # base case    tc[0][0] ... Read More

Python Program for Merge Sort

Pavitra
Updated on 20-Dec-2019 06:33:27

617 Views

In this article, we will learn about the solution to the problem statement given below.Problem statement − We are given an array, we need to sort it using the concept of merge sortHere we place the maximum element at the end. This is repeated until the array is sorted.Now let’s observe the solution in the implementation below −Example#merge function def merge(arr, l, m, r):    n1 = m - l + 1    n2 = r- m    # create arrays    L = [0] * (n1)    R = [0] * (n2)    # Copy data to arrays   ... Read More

Python Program for Maximum height when coins are arranged in a triangle

Pavitra
Updated on 20-Dec-2019 06:30:11

156 Views

In this article, we will learn about the solution to the problem statement given below.Problem statement − We are given N coins where we need to arrange them in form of a triangle, i.e. in the first row will have 1 coin, the second row will have 2 coins and so on, we need to display the maximum height that can be achieved by the help N coins.Now let’s observe the solution in the implementation below −Example Live Demo# squareroot def squareRoot(n):    # initial approximation    x = n    y = 1    e = 0.000001 # allowed error ... Read More

Python Program for Iterative Quick Sort

Pavitra
Updated on 20-Dec-2019 06:27:27

442 Views

In this article, we will learn about the solution to the problem statement given below.Problem statement − We are given an array, we need to sort it using the concept of quick sort using iterative wayHere we first partition the array and sort the separate partition to get the sorted array.Now let’s observe the solution in the implementation below−Example Live Demo# iterative way def partition(arr, l, h):    i = ( l - 1 )    x = arr[h]    for j in range(l , h):       if arr[j] = 0:       # Pop       h ... Read More

Python Program for Iterative Merge Sort

Pavitra
Updated on 20-Dec-2019 06:23:49

464 Views

In this article, we will learn about the solution to the problem statement given below.Problem statement − We are given an array, we need to sort it using the concept of merge sort by iteration.Here we place the maximum element at the end. This is repeated until the array is sorted.Now let’s observe the solution in the implementation below −Example# iterative way def mergeSort(a):    current_size = 1    # traversing subarrays    while current_size < len(a) - 1:       left = 0       # subarray being sorted       while left < len(a)-1:     ... Read More

Python Program for Heap Sort

Pavitra
Updated on 20-Dec-2019 06:17:43

1K+ Views

In this article, we will learn about the solution to the problem statement given below.Problem statement − We are given an array, we need to sort it using the concept of heapsort.Here we place the maximum element at the end. This is repeated until the array is sorted.Now let’s observe the solution in the implementation below−Example Live Demo# heapify def heapify(arr, n, i):    largest = i # largest value    l = 2 * i + 1 # left    r = 2 * i + 2 # right    # if left child exists    if l < n and ... Read More

Python Program for Gnome Sort

Pavitra
Updated on 20-Dec-2019 06:13:54

236 Views

In this article, we will learn about the solution to the problem statement given below.Problem statement − We are given an array, we need to sort it using gnome sort.Algorithm1. Firstly we traverse the array from left to right. 2. Now, if the current element is larger or equal to the previous element then we traverse one step ahead 3. otherwise, if the current element is smaller than the previous element then swap these two elements and traverse one step back. 4. Repeat steps given above till we reach the end of the arrayNow let’s observe the solution in the ... Read More

Advertisements