def print_bsearch_hint(self): message = """ FIND AN ELEMENT FROM A GIVEN SORTED LIST USING BINARY SEARCH ALGORITHM AND PRINT THE ELEMENT'S POSITION -+-+-+- In binary search we take a sorted list of elements and start looking for an element at the middle of the list. If the search value matches with the middle value in the list we complete the search. Otherwise we eleminate half of the list of elements by choosing whether to procees with the right or left half of the list depending on the value of the item searched. This is possible as the list is sorted and it is much quicker than linear search. Here we divide the given list and conquer by choosing the proper half of the list. We repeat this approcah till we find the element or conclude about it's absence in the list. -+-+-+- PSEUDOCODE: 1. Compare 'x' with the middle element 2. If x matches with the middle most element, we return the index of the middle element i.e. 'mid'. 3. Else if x is greater than the middle element, x will lie in the right sub-part of the list from the middle element.Thus, we recur the right part of the list. 4. Else, the x is smaller than the middle element, so we recur the left sub-part of the list. -+-+-+- TIME COMPLEXITY OF BINARY SEARCH ALGORITHM: Worst-case performance: O(log n) Best-case performance: O(1) Average performance: O(log n) Worst-case space complexity: O(1) -+-+-+- MORE INFO HERE: https://en.wikipedia.org/wiki/Binary_search_algorithm """ print_msg_box(message)
def isPalindromicPermutation_hint(): message = """ Palindromic Permutation ------------------------------------ Purpose :To check if the permutation of the characters in a string can make it palindromic Method : string manipulation, palindromic behaviour Time Complexity : Worst Case - O(n), n = length of the string Hint : Make a dictionary of characters and their repeatations. Pseudocode : --> Take a blank dictionary --> for i in [0,length of input string] key = input[i] if(key in dictionary) dictionary[key]+=1 else push {key:1} inside dictionary --> Check if dictioary[i] %2 == 1 Visualization: Given String : "abbca" Making a table using dictionary : Step 1 - create a blank dictionary - {} Step 2 - check if the key exists yes --> add 1 no --> push {key:1} inside the dictionary Step 3 - You have the following table +----------+----------------+ | key | repeatations | +----------+----------------+ | a | 2 | --> rem = 0, flag = 0 ----------------------------- | b | 2 | --> rem = 0, flag = 0 ----------------------------- | c | 1 | --> rem = 0, flag = 1 ----------------------------- Step 4 - check reminder, set flag = 0, initially Step 5 - return boolean Learn More about Python Dictionaries Below - https://www.w3schools.com/python/python_dictionaries.asp """ print_msg_box(message)
def m_coloring_hint(): message=""" M Coloring Problem ------------------------------------------------------------------------------------------ Purpose : Given an undirected graph and a number m, determine if the graph can be colored with at most m colors such that no two adjacent vertices of the graph are colored with same color. Method : Backtracking Time Complexity : O(m^V) Space Complexity : O(V) Hint : The idea is to assign colors one by one to different vertices, starting from the vertex 0. Before assigning a color, we check for safety by considering already assigned colors to the adjacent vertices. If we find a color assignment which is safe, we mark the color assignment as part of solution. If we do not a find color due to clashes then we backtrack and return false. Pseudocode: Begin if all vertices are checked, then return true for all colors col from available colors, do if isValid(vertex, color, col), then add col to the colorList for vertex if graphColoring(colors, colorList, vertex+1) = true, then return true remove color for vertex done return false End Visualization: Input: 2D Graph: [[0, 1, 1, 1], [1, 0, 1, 0], [1, 1, 0, 1], [1, 0, 1, 0]] m color: 3 Input Graph Representaion: (D)---(C) | / | | / | | / | (A)---(B) Solution: yes, it is 3 colorable (2)---(3) | / | | / | | / | (1)---(2) Output: 1 2 3 2 Learn More: - Backtracking - https://en.wikipedia.org/wiki/Backtracking - Graph_coloring - https://en.wikipedia.org/wiki/Graph_coloring """ print_msg_box(message)
def longest_common_subsequence_hint(): message = """ Longest Common Subsequence ------------------------------------------------------------------------------------------ Purpose : The longest common subsequence problem is finding the longest sequence which exists in both the given strings. Method : Dynamic Programming Time Complexity : O(mn) Space Complexity : O(mn) Hint : Apply Dynamic Programming on both the strings to find Longest Common Subsequence. Pseudocode: X and Y are strings m = len(X) n = len(Y) L = [[None]*(n+1) for i in range(m+1)] for i in range(m+1): for j in range(n+1): if i == 0 or j == 0 : L[i][j] = 0 elif X[i-1] == Y[j-1]: L[i][j] = L[i-1][j-1]+1 else: L[i][j] = max(L[i-1][j] , L[i][j-1]) return L[m][n] Visualization: X = "ACDEU" Y = "ABCDE" Applying recursive formula at Longest Common Subsequence 2D Array if i == 0 or j == 0 : L[i][j] = 0 elif X[i-1] == Y[j-1]: L[i][j] = L[i-1][j-1]+1 else: L[i][j] = max(L[i-1][j] , L[i][j-1]) LCS * A B C D E * 0 0 0 0 0 0 A 1 1 1 1 1 1 C 0 1 1 2 2 2 D 0 1 1 2 3 3 E 0 1 1 2 3 4 U 0 1 1 2 3 4 FINAL RESULT: L[m][n] = L[5][5] = 4 Learn More: - Maximum Subarray Problem - https://en.wikipedia.org/wiki/Longest_common_subsequence_problem """ print_msg_box(message)
def URLify_hint(): message = """ Making a URL From a String ------------------------------------ Purpose : Making a URL by replacing the spaces with a key value entered by the user Method : string manipulation Time Complexity : Worst Case - O(n), n = length of the string Hint : Take a blank string, and add data from the input string to the blank string to prepare the final URL Pseudocode : --> Take a blank string s2 --> for i in [0,length of input string] if(not a whitespace) add to s2 elif(whitespace and next place is also whitespace) return s2 elif(whitespace and next place not whitespace) add the key value to the blank string Visualization: Given String To Make URL : "Python is love" Key : "%20" Break The Given String : /*/ ----> whitespace +--------+-------+----+-------+------+ | Python | /*/ | is | /*/ | love | +--------+-------+----+-------+------+ ^ ^ ^ ^ ^ ^ ^ ^ ^ 1 2 3 We will take 1, 2 and 3 sucessively and in place of whitespaces we will concatenate the key value. Empty String Addition : +-+ +--------+ +-------+ +----+ +-------+ +------+ | | + | Python | + | %20 | + | is | + | %20 | + | love | +-+ +--------+ +-------+ +----+ +-------+ +------+ Learn More about String Concatenation Below - https://en.wikipedia.org/wiki/Concatenation """ print_msg_box(message)
def house_robber_hint(): message=""" House Robber Problem ------------------------------------------------------------------------------------------ Purpose : There are n houses build in a line, each of which contains some value in it. A thief is going to steal the maximal value of these houses, but he can’t steal in two adjacent houses because the owner of the stolen houses will tell his two neighbours left and right side. What is the maximum stolen value? Method : Dynamic Programming Time Complexity : O(n) Space Complexity : O(1) Hint : Apply Dynamic Programming on value array of houses to find maximum amount. Pseudocode: house: array of houses which contain some values Initialize: val1 = house[0] val2 = house[1] Loop(i=2,size) (a) max_val = max(house[i]+val1, val2) (b) val1 = val2 (b) val2 = max_val return max_val Visualization: Input: house = [2,1,1,3] val1 = house[0] = 2 val2 = house[1] = 1 for i=2, max_val = max(house[i]+val1, val2) = max(1+2,1) = 3 val1 = val2 = 1 val2 = max_val = 3 for i=3, max_val = max(house[i]+val1, val2) = max(3+1,3) = 4 val1 = val2 = 3 val2 = max_val = 4 FINAL RESULT = max_val = 4 Learn More: - Dynamic Programming - https://en.wikipedia.org/wiki/Dynamic_programming """ print_msg_box(message)
def fib_hint(self): message = ''' Conditions to calculate the Fibonacci series: 1. If num == 0 then Fibonacci should return 0 2. If num == 1 then Fibonacci should return 1 3. If num > 0 then add the previous number with current number Time Complexity : O(2^n) Space Complexity : O(1) Where n is number of number of Fibonacci series ''' print_msg_box(message)
def binary_search_hint(): message = """ Binary Search ------------------------------------ Purpose : searching a required number in a sorted uniformly distributed array SORTED ARRAY- Arranged in ascending order Method : Searching first in the position(index) that has maximum probability of having required element. Time Complexity: Best case- Ω(1) Worst case- O(logn) Hint : Starting from 0th element of array[] and comparing every element to x search one by one. Formula used : high+low Middle position(pos) = int (------------) 2 where low is the lowest index high is the highest index x is the number to be searched arr[i] is the ith element in the given sorted array Pseudocode: while low<=high and array[low]<=array[high] if array[low]==x return low *calculate middle position(pos) with formula above if array[pos]< x low = pos+1 else high = pos-1 Visualization: Number to search => x=3 Given Array : arr[] index- 0 1 2 3 +-----+-----+-----+-----+ | 1 | 3 | 4 | 6 | +-----+-----+-----+-----+ First Iteration : +-----+-----+-----+-----+ low = 0 high = 3 | 1 | 2 | 3 | 6 | arr[low] = 1 arr[high] = 6 +-----+-----+-----+-----+ pos = int(0+3/2) = 1 =>It will check at arr[pos]==x or not which is not and arr[pos]<x => low = pos+1 = 2 Second Iteration : +-----+-----+-----+-----+ low = 2 high = 3 | 1 | 3 | 4 | 6 | arr[low] = 3 arr[high] = 6 +-----+-----+-----+-----+ pos = int(2+3/2) = 2 =>It will check at arr[pos]==x which is true => It will return 2 If no element in the array matched x=3 then it would return -1 Learn More Here - https://www.geeksforgeeks.org/interpolation-search/ """ print_msg_box(message)
def bubble_sort_hint(): message = """ Bubble Sort ------------------------------------ Purpose : sorting in increasing order Method : Bubble Making, Swapping Time Complexity: Worst Case - O(n^2) Hint : Try to kick out the greater value to the rightmost position by using loops and value swapping. Pseudocode: --> for i in [0,length of array] for j in [0,length of array - 1] if(array[j] > array[i]) swap array[j] & array[i] Visualization: Given Array : +-----+-----+-----+ | 5 | 4 | 3 | +-----+-----+-----+ First Iteration : +-----+-----+-----+ | 4 | 5 | 3 | +-----+-----+-----+ Second Iteration : +-----+-----+-----+ | 4 | 3 | 5 | +-----+-----+-----+ Third Iteration : +-----+-----+-----+ | 3 | 4 | 5 | +-----+-----+-----+ Learn More Here - https://en.wikipedia.org/wiki/Bubble_sort """ print_msg_box(message)
def insertion_sort_hint(): message = """ Insertion Sort ------------------------------------ Purpose : sorting in increasing order Method : insert element in correct position, pushing greater elements ahead Time Complexity: Worst Case - O(n^2) Hint : In every iteration the ith element is inserted into the correct place and the elements greater than ith element are moved one position ahead of current position. Pseudocode: --> for i in [1,length of array] key = arr[i] j = i-1 while j >= 0 and key < arr[j] : arr[j+1] = arr[j] j -= 1 arr[j+1] = key Visualization: Given Array : +-----+-----+-----+ | 5 | 4 | 3 | +-----+-----+-----+ First Iteration : +-----+-----+-----+ | 4 | 5 | 3 | +-----+-----+-----+ Second Iteration : +-----+-----+-----+ | 3 | 4 | 5 | +-----+-----+-----+ Finally you have the sorted array. Learn More Here - https://en.wikipedia.org/wiki/Insertion_sort """ print_msg_box(message)
def kadanes_algorithm_hint(): message=""" Kadane’s Algorithm ------------------------------------------------------------------------------------------ Purpose : In this problem, we are given an array. Our task is to find out the maximum subarray sum. Method : Dynamic Programming Time Complexity : O(n) Space Complexity : O(1) Hint : Apply Dynamic Programming on contiguous array to find max contiguous sum. Pseudocode: Initialize: max_so_far = a[0] curr_max = a[0] Loop(i=1,size) (a) curr_max = max(a[i],curr_max + a[i]) (b) max_so_far = max(max_so_far,curr_max) return max_so_far Visualization: Input: [-2,1,2,-1] max_so_far = -2 curr_max = -2 for i=1, a[0] = -2 curr_max = max(1,-2+1) = 1 max_so_far = max(-2,1) = 1 for i=2, curr_max = max(2,1+2) = 3 max_so_far = max(1,3) = 3 for i=3, curr_max = max(-1,3-1) = 2 max_so_far = max(3,2) = 3 FINAL RESULT = max_so_far = 3 Learn More: - Maximum Subarray Problem - https://en.wikipedia.org/wiki/Maximum_subarray_problem """ print_msg_box(message)
def string_permutaions_hint(): message = """ Print All Permutations Of A Given String ------------------------------------------------------------------------------------------ Purpose : In this problem, we are given a string. Our task is to print all permutations of a given string. Method : Backtracking Time Complexity : O(n*n!) Hint : Apply Backtracking to find permutations of a given string. Pseudocode: 1. Define a string. 2. Fix a character and swap the rest of the characters. 3. Call the recursive function for rest of the characters. 4. Backtrack and swap the characters again. Visualization: Recursion Tree for Permutations of String "ABC" * represents fixed characters +---+---+---+ | A | B | C | +---+---+---+ | -------------------------------------------------------------------- swap A,A | swap A,B | swap A,C | +---+---+---+ +---+---+---+ +---+---+---+ |*A | B | C | |*B | A | C | |*C | B | A | +---+---+---+ +---+---+---+ +---+---+---+ A fixed | B fixed | C fixed | --------------------- --------------------- --------------------- swap B,B | swap B,C | swap A,A | swap A,C | swap B,B | swap B,A | +---+---+---+ +---+---+---+ +---+---+---+ +---+---+---+ +---+---+---+ +---+---+---+ |*A |*B | C | |*A |*C | B | |*B |*A | C | |*B |*C | A | |*C |*B | A | |*C |*A | B | +---+---+---+ +---+---+---+ +---+---+---+ +---+---+---+ +---+---+---+ +---+---+---+ AB fixed AC fixed BA fixed BC fixed CB fixed CA fixed FINAL RESULT = ABC, ACB, BAC, BCA, CBA, CAB Learn More: - Backtracking - https://en.wikipedia.org/wiki/Backtracking - Permutations - https://en.wikipedia.org/wiki/Permutation """ print_msg_box(message)
def oneEditAway_hint(): message = """ One Edit Away ------------------------------------ Purpose : Check if two strings are one edit (or zero) away,where edit means the following three methods, - inserting a character - removing a character - replacing a character Method : string manipulation Time Complexity : Worst Case - O(n), n = length of the greater string Hint : Divide the problem in three cases of insert, remove and replace and solve the problem. Pseudocode : For checking "replace" : --> flag = False --> for i in range(len(input1)): if(input2[i]!=input1[i]): if(flag): return False flag = True For checking "insert" & "remove" : --> index1 = 0 --> index2 = 0 --> while((index2 < len(input2)) and (index1 < len(input1))): if(input1[index1] != input2[index2]): if(index1 != index2): return False index2+=1 else: index1+=1 index2+=1 return True """ print_msg_box(message)
def climbing_stairs_hint(): message = """ Climb Stairs ------------------------------------------------------------------------------------------ Purpose : There are n stairs, a person standing at the bottom and wants to reach the top. The person can climb either 1 stair or 2 stairs at a time. Count the number of ways, the person can reach the top. Method : Dynamic Programming Time Complexity : O(m*n) Space Complexity : O(n) Hint : Apply Dynamic Programming in bottom up manner. Pseudocode: Create a res[] table in bottom up manner using the following relation: res[i] = res[i] + res[i-j] for every (i-j) >= 0 such that the ith index of the array will contain the number of ways required to reach the ith step considering all the possibilities of climbing (i.e. from 1 to i). Visualization: Input: n=3 ^ ^ ^ ------- ------- ------- ^ | | ^ | ------ ------ ------ ^ | ^ | | ------ ------ ------ ^ | ^ | ^ | ------ ------- ------- Output: 3 Learn More: - - Dynamic Programming - https://en.wikipedia.org/wiki/Dynamic_programming """ print_msg_box(message) # print(climbing_stairs(3,True))
def linear_search_hint(): message = """ Linear Search ------------------------------------ Purpose : searching a required number Method : Iterating, Comparing Time Complexity: Worst Case - O(n) Hint : Starting from 0th element of array[] and comparing every element to x search one by one. Pseudocode: --> for i in range[0,length of array] if(array[i]= x) return i return "Not found" Visualization: Number to search => x=3 Given Array : +-----+-----+-----+ | 5 | 4 | 3 | +-----+-----+-----+ First Iteration (i=0): +-----+-----+-----+ | 5 | 4 | 3 | +-----+-----+-----+ [checking if 5==x, which is not true so going on the next iteration] Second Iteration (i=1): +-----+-----+-----+ | 5 | 4 | 3 | +-----+-----+-----+ [checking if 4==x, which is not true so going on the next iteration] Third Iteration (i=2): +-----+-----+-----+ | 5 | 4 | 3 | +-----+-----+-----+ [checking if 3==x, which is true so the search returns location of x in the array ] If no element in the array matched x=3 then it would return -1 Learn More Here - https://en.wikipedia.org/wiki/Linear_search """ print_msg_box(message)
def print_tree_hint(self): message = """ Printing A Binary Tree ------------------------------------ Purpose : Printing a Binary Tree(Both Left and Right Node) Method : Recursion, Binary Tree Time Complexity : Worst Case - O(n), n = Number of nodes in a Binary Tree Hint : Print the root, use recursion and call into the left and right Pseudocode : --> if(root == None) return --> print(root.value) --> print(root.left.value) --> print(root.right.value) --> make recursion calls, print_tree(root.left) print.tree(root.right) Visualization: Given Binary Tree : +------+ | 12 | <-- root +------+ / \\ / \\ +------+ +------+ root.left --> | 6 | | 14 | <-- root.right +------+ +------+ / \\ / \\ / \\ / \\ +------+ +------+ +------+ +------+ | 3 | | 7 | | 12 | | 15 | +------+ +------+ +------+ +------+ Step 1: print root, root.left and root.right 12 : L:6, R: 14 Step 2 : call recursive functions f(root.left) : +------+ | 6 | <-- root +------+ / \\ / \\ +------+ +------+ root.left --> | 3 | | 7 | <-- root.right +------+ +------+ +------------------------+ | Repeat Step 1 & Step 2 | <-- recursion calls +------------------------+ f(root.right) : +------+ | 14 | <-- root +------+ / \\ / \\ +------+ +------+ root.left --> | 12 | | 15 | <-- root.right +------+ +------+ +------------------------+ | Repeat Step 1 & Step 2 | <-- recursion calls +------------------------+ Finally The Output : ------------------------- | 12:L: 6 ,R: 14 , | | | | 6:L: 3 ,R: 7 , | | | | 3: | | | | 7: | | | | 14:L: 12 ,R: 15 , | | | | 12: | | | | 15: | ------------------------- Learn More: - Binary Trees - https://en.wikipedia.org/wiki/Binary_tree - Recursion - https://en.wikipedia.org/wiki/Recursion_(computer_science) """ print_msg_box(message)
def Postorder_print_hint(self): message = """ Printing A Binary Tree PostOrder Traversal ------------------------------------ Purpose : Printing a Binary Tree(PostOrder Traversal) Method : Recursion, Binary Tree Time Complexity : Worst Case - O(n), n = Number of nodes in a Binary Tree Hint : print order -> LEFT -- RIGHT -- ROOT use recursion to call into the left and the right subtree, Print the root Pseudocode : --> if(root == None) return --> print(root.left.value) --> print(root.right.value) --> print(root.value) Visualization: Given Binary Tree : +------+ | 12 | <-- root +------+ / \\ / \\ +------+ +------+ root.left --> | 6 | | 14 | <-- root.right +------+ +------+ / \\ / \\ / \\ / \\ +------+ +------+ +------+ +------+ | 3 | | 7 | | 13 | | 15 | +------+ +------+ +------+ +------+ step 1 : Print the left, print right, print root +------+ | 6 | <-- root +------+ / \\ / \\ +------+ +------+ root.left --> | 3 | | 7 | <-- root.right +------+ +------+ output : LEFT -- RIGHT -- ROOT 3 7 6 Finally The Output : --------------------------------- | 3, 7, 6, 13, 15, 14, 12 | --------------------------------- Learn More: - Binary Trees - https://en.wikipedia.org/wiki/Binary_tree - Recursion - https://en.wikipedia.org/wiki/Recursion_(computer_science) """ print_msg_box(message)
def Inorder_print_hint(self): message = """ Printing A Binary Tree InOrder Traversal ------------------------------------ Purpose : Printing a Binary Tree(InOrder Traversal) Method : Recursion, Binary Tree Time Complexity : Worst Case - O(n), n = Number of nodes in a Binary Tree Hint : print order -> LEFT -- ROOT -- RIGHT Call into left recursively till exist,Print the root, use recursion to call into the right Pseudocode : --> if(root == None) return --> print(root.left.value) --> print(root.value) --> print(root.right.value) Visualization: Given Binary Tree : +------+ | 12 | <-- root +------+ / \\ / \\ +------+ +------+ root.left --> | 6 | | 14 | <-- root.right +------+ +------+ / \\ / \\ / \\ / \\ +------+ +------+ +------+ +------+ | 3 | | 7 | | 13 | | 15 | +------+ +------+ +------+ +------+ step : call recursive functions on root.left, print root, call on right f(root.left) : +------+ | 3 | <-- root +------+ / \\ / \\ +------+ +------+ root.left --> | None | | None | <-- root.right +------+ +------+ output : LEFT -- ROOT -- RIGHT None 3 None +------+ | 6 | <-- root +------+ / \\ / \\ +------+ +------+ root.left --> | 3 | | 7 | <-- root.right +------+ +------+ output : LEFT -- ROOT -- RIGHT 3 6 7 f(root.right) : +------+ | 14 | <-- root +------+ / \\ / \\ +------+ +------+ root.left --> | 13 | | 15 | <-- root.right +------+ +------+ output : LEFT -- ROOT -- RIGHT 13 14 15 Finally The Output : --------------------------------- | 3, 6, 7, 12, 13, 14, 15, | --------------------------------- Learn More: - Binary Trees - https://en.wikipedia.org/wiki/Binary_tree - Recursion - https://en.wikipedia.org/wiki/Recursion_(computer_science) """ print_msg_box(message)
def rat_in_a_maze_hint(): message = """ Rat in a Maze problem via Backtracking ------------------------------------------------------------------------------------------ Pupose: Consider a rat placed at (0, 0) in a maze of order N*M. It has to reach the destination at (N-1, M-1). Find a possible paths that the rat can take to reach from source to destination. The directions in which the rat can move are 'U'(up), 'D'(down), 'L' (left), 'R' (right). Maze is in the form of a binary matrix containing 0's and 1's only. By default 0 is considered as wall cell whereas 1 is considered to way cell. The rat can only on the way cell. Method: Backtracking Time Complexity: Worst case- O(2^N*M) Best case- O(N*M) Hint: Say the position of the rat is (X,Y). Then check for all the cells accessable through the current possition i.e (X+1,Y), (X-1,Y), (X,Y+1) and (X,Y-1) and see whether they are way of wall. If wall, then move the rat to that position and repeat the process. In case there is no possible move from the current position, backtrack the previous move and check for the next possible move and repeat the process until you encounter (N-1,M-1). Pseudocode: 1) Mantain a boolean matrix visited to keep a note on already visited node and a list of all possible moves from a given position i.e direction. 2) Check for the base case if destnation or source contains wall: return False else return move_rat(0,0,maze) 3) In the recursive function, first check whether the current position is the destination or not if current is destination return ans else check for all the possible move from that position if move is valid and not visited and next_move is way return move_rat(next_move) if no move is valid return False Visualization: maze=[[1,0,0], [1,1,1], [0,0,1]] Source := (0,0) Destination := (2,2) Wall:= X grapical representaion of maze: +-------+-------+-------+ | | | | | R | X | X | | | | | +-------+-------+-------+ | | | | | | | | | | | | +-------+-------+-------+ | | | | | X | X | | | | | | +-------+-------+-------+ Base Condition check, whether the the Source and destination are not wall Now the Rat is at (0,0) UP, move not valid DOWN, move valid +-------+-------+-------+ | | | | | * | X | X | | | | | +-------+-------+-------+ | | | | | R | | | | | | | +-------+-------+-------+ | | | | | X | X | | | | | | +-------+-------+-------+ Now the Rat is at (1,0) UP, already visited DOWN, invalid move, WALL RIGHT, valid move +-------+-------+-------+ | | | | | * | X | X | | | | | +-------+-------+-------+ | | | | | * | R | | | | | | +-------+-------+-------+ | | | | | X | X | | | | | | +-------+-------+-------+ Now the Rat is at (1,1) UP, invalid move, Wall DOWN, invalid move, WALL RIGHT, valid move +-------+-------+-------+ | | | | | * | X | X | | | | | +-------+-------+-------+ | | | | | * | * | R | | | | | +-------+-------+-------+ | | | | | X | X | | | | | | +-------+-------+-------+ Now the Rat is at (1,2) UP, invalid move, Wall DOWN, valid move +-------+-------+-------+ | | | | | * | X | X | | | | | +-------+-------+-------+ | | | | | * | * | * | | | | | +-------+-------+-------+ | | | | | X | X | R | | | | | +-------+-------+-------+ Now the Rat is at (2,2) this is the Destination cell, hence we will terminate the recursion Answer: DRRD For animated visulization:- https://github.com/karan236/Algorithm-Visualizer """ print_msg_box(message)
def selection_sort_hint(): message = """ selection Sort ------------------------------------ Purpose : sorting in increasing order Method : Pick Up minimum, swap with minimum Time Complexity: Worst Case - O(n^2) Hint : In every iteration the minimum element from the unsorted subarray is picked and moved to the sorted subarray. Pseudocode: --> for i in [0,length of array] minimum = i for j in [i+1,length of array] if arr[j] < arr[minimum] minimum = j swap arr[i] & arr[minimum] Visualization: Given Array : +-----+-----+-----+ | 5 | 4 | 3 | +-----+-----+-----+ We have two buckets, | | | | | Unsorted | | sorted | | | | | | 5,4,3 | | empty | -------------- -------------- Select the minimum from the unsorted bucket and put that in sorted bucket | | | | | Unsorted | | sorted | | | | | | 5,4 | | 3 | -------------- -------------- Again select the minimum from the unsorted bucket and put that in sorted bucket | | | | | Unsorted | | sorted | | | | | | 5 | | 3,4 | -------------- -------------- Repeat the same till the unsorted bucket is empty | | | | | Unsorted | | sorted | | | | | | | | 3,4,5 | -------------- -------------- Finally you have the sorted array. Learn More Here - https://en.wikipedia.org/wiki/Selection_sort """ print_msg_box(message)
def heap_sort_hint(): message = """ Heap Sort ------------------------------------ Purpose : sorting in increasing order Method : Create Max Heap, Remove max root element and swap with last element, Repeat process Time Complexity: Time complexity of heapify is O(Logn). Time complexity of createAndBuildHeap() is O(n) and overall time complexity of Heap Sort is O(nLogn). Hint : 1. Build a max heap from the input data. 2. At this point, the largest item is stored at the root of the heap. Replace it with the last item of the heap followed by reducing the size of heap by 1. Finally, heapify the root of the tree. 3. Repeat step 2 while size of heap is greater than 1. Visualization: Given Array : +-----+-----+-----+-----+-----+ | 4 | 10 | 3 | 5 | 1 | +-----+-----+-----+-----+-----+ ( 0 ) +------+ | 4 | +------+ / \\ ( 1 ) / \\ ( 2 ) +------+ +------+ | 10 | | 3 | +------+ +------+ / \\ / \\ +------+ +------+ ( 3 ) | 5 | | 1 | ( 4 ) +------+ +------+ -> The numbers in bracket represent the indices in the array representation of data. Applying heapify procedure to index 1 : ( 0 ) +------+ | 4 | +------+ / \\ ( 1 ) / \\ ( 2 ) +------+ +------+ | 10 | | 3 | +------+ +------+ / \\ / \\ +------+ +------+ ( 3 ) | 5 | | 1 | ( 4 ) +------+ +------+ Applying heapify procedure to index 0 : ( 0 ) +------+ | 10 | +------+ / \\ ( 1 ) / \\ ( 2 ) +------+ +------+ | 5 | | 3 | +------+ +------+ / \\ / \\ +------+ +------+ ( 3 ) | 4 | | 1 | ( 4 ) +------+ +------+ -> The heapify procedure calls itself recursively to build heap in top down manner. Learn More Here - https://en.wikipedia.org/wiki/Heapsort """ print_msg_box(message)
def quick_sort_hint(): message = """ Quick Sort ------------------------------------ Purpose : sorting in increasing order Method : Given an array and an element x of array as pivot, put x at its correct position in sorted array and put all smaller elements (smaller than x) before x, and put all greater elements (greater than x) after x. Time Complexity: Worst Case - O(n^2): The worst case occurs when the partition process always picks greatest or smallest element as pivot. Time Complexity: Average Case - O(n*log(n)) Hint : Take the last element as pivot, place the pivot element at its correct position in sorted array, and place all smaller (smaller than pivot) to left of pivot and all greater elements to right of pivot Pseudo Code: --> partition_index = partition(arr, low, high) quick_sort(arr, low, pi-1) # Before the partition index quick_sort(arr, pi+1, high) # After the partition index --> Partitioning Hint: for j in range(low, high): if arr[j] < pivot: i += 1 swap (arr[i], arr[j]) arr[i], arr[j] = arr[j], arr[i] swap (arr[i+1], arr[high]) return i+1 Visualization: (Illustrating the partition logic) Given Array : +-----+-----+-----+-----+-----+ | 5 | 3 | 2 | 7 | 4 | +-----+-----+-----+-----+-----+ Initializing i = -1, j = 0, pivot = arr[high] = 4. Traverse the array from low to high - 1 (ie 0 to 3). j = 0. i = -1. Since arr[j] > pivot, do nothing. No change in arr and i. +-----+-----+-----+-----+-----+ | 5 | 3 | 2 | 7 | 4 | +-----+-----+-----+-----+-----+ j = 1. i = -1. Since arr[j] <= pivot, do i++ and swap(arr[i], arr[j]) ie swap (5, 3). +-----+-----+-----+-----+-----+ | 3 | 5 | 2 | 7 | 4 | +-----+-----+-----+-----+-----+ j = 2. i = 0. Since arr[j] <= pivot, do i++ and swap(arr[i], arr[j]) ie swap (5, 2). +-----+-----+-----+-----+-----+ | 3 | 2 | 5 | 7 | 4 | +-----+-----+-----+-----+-----+ j = 3. i = 1. Since arr[j] > pivot, do nothing. No change in arr and i. +-----+-----+-----+-----+-----+ | 3 | 2 | 5 | 7 | 4 | +-----+-----+-----+-----+-----+ We come out of loop because j is now equal to high-1. Now we place pivot at correct position by swapping arr[i+1] and arr[high] (or pivot). +-----+-----+-----+-----+-----+ | 3 | 2 | 4 | 7 | 5 | +-----+-----+-----+-----+-----+ Now 4 is at its correct place. All elements smaller than 4 are before it and all elements greater than 4 are after it. Repeat this process for left and right side of partition index. Learn More Here - https://en.wikipedia.org/wiki/Quicksort """ print_msg_box(message)
def merge_sort_hint(): message = """ merge Sort ------------------------------------ Purpose : sorting in increasing order Method : Break into two halves and get these lists sorted, then merge these sorted halves into one sorted list. Time Complexity: Worst Case - O(n*log(n)) Hint : We break the list into halves until we have single element lists(since there is only one element they are sorted.) then we merge these lists pair by pair such that the merged list is sorted. Pseudocode: --> if len(arr) <= 1: return arr else: return merge(merge_sort(arr[:mid]), merge_sort(arr[mid:])) Visualization: Given Array : +-----+-----+-----+-----+-----+ | 5 | 4 | 3 | 7 | 2 | +-----+-----+-----+-----+-----+ Break list into halves until you have one list for each array +-----+-----+ +-----+-----+-----+ | 5 | 4 | | 3 | 7 | 2 | +-----+-----+ +-----+-----+-----+ +-----+ +-----+ +-----+ +-----+-----+ | 5 | | 4 | | 3 | | 7 | 2 | +-----+ +-----+ +-----+ +-----+-----+ +-----+ +-----+ +-----+ +-----+ +-----+ | 5 | | 4 | | 3 | | 7 | | 2 | +-----+ +-----+ +-----+ +-----+ +-----+ Now we merge pair and maintain the sorted order: +-----+ +-----+ +-----+ +-----+-----+ | 5 | | 4 | | 3 | | 2 | 7 | +-----+ +-----+ +-----+ +-----+-----+ +-----+-----+ +-----+ +-----+-----+ | 4 | 5 | | 3 | | 2 | 7 | +-----+-----+ +-----+ +-----+-----+ +-----+-----+ +-----+-----+-----+ | 4 | 5 | | 2 | 3 | 7 | +-----+-----+ +-----+-----+-----+ +-----+-----+-----+-----+-----+ | 2 | 3 | 4 | 5 | 7 | +-----+-----+-----+-----+-----+ Finally you have the sorted array. Learn More Here - https://en.wikipedia.org/wiki/Merge_sort """ print_msg_box(message)
def Sudoku_Solver_hint(): message= """ Sudoku ------------------------------------ Purpose : To solve Sudoku Grid of 9 X 9. Method : Recursive , Backtracking Time Complexity: Worst Case - O(9^(n*n)) Space Complexity: O(n*n) Hint: * Find the first empty cell in the Sudoku Grid by simply searhing through two for loops if no cell is empty i.e. this is the base condition return back and Complexity if any cell is present empty then store the row and Column of that cell and break the loops *Now one bye one check whether any digit ranging from [1-9] can be placed in that cell. For this checking we had used function CanPlaceNum this will inturn check for row, col and box. -> for Row the isRowSafe function will check whether any digit in this row is equal to the digit that we are going to place are equal if yes then return false else continue and return true at last. -> for Column the isColSafe function will check whether any digit in this Column is equal to the digit that we are going to place are equal if yes then return false else continue and return true at last. -> for Box that is the box that are of fixed size of 3 X 3, the function isBoxSafe is used and do the same as above But one thing the start of row and column are important that is calculated using : rs = row - row%3 cs = col - col%3 * Now if we are unable to place the digit then we try the next digit and continue the process... and if we are able to place the digit then we will Recursively call the Sudoku_Solver() function ,if this call return true then we will continue else we have to Backtrack by placing 0 to that cell and try another number in that place... Visualization: suppose we have a sudoku of 4 X 4 size as Below: INPUT: 0 1 2 3 +---------------+ 0 |1 3 |0 4| | | | 1 |2 0 |3 1| |-------+-------| 2 |0 1|0 2| | | | 3 |4 0|1 0| +---------------+ 0 -> Empty Cell we found the first Empty cell (0,2). Now we will check numbers from [1-9]. we find that we can place a 2. 0 1 2 3 +---------------+ 0 |1 3 |2 4| | | | 1 |2 0 |3 1| |-------+-------| 2 |0 1|0 2| | | | 3 |4 0|1 0| +---------------+ Now Recursively finding the Empty cell we found (1,1) and the Recursion continue 0 1 2 3 +---------------+ 0 |1 3 |2 4| | | | 1 |2 4 |3 1| |-------+-------| 2 |0 1|0 2| | | | 3 |4 0|1 0| +---------------+ 0 1 2 3 +---------------+ 0 |1 3 |2 4| | | | 1 |2 4 |3 1| |-------+-------| 2 |3 1|4 2| | | | 3 |4 2|1 0| +---------------+ FINAL RESULT: 0 1 2 3 +---------------+ 0 |1 3 |2 4| | | | 1 |2 4 |3 1| |-------+-------| 2 |3 1|4 2| | | | 3 |4 2|1 3| +---------------+ """ print_msg_box(message)
def n_queens_hint(): message = """ N-Queens problem via Backtracking ------------------------------------------------------------------------------------------ Purpose : To place n queens in an n X n Chessboard where no queen attack another queen in any form(such as diagonaly, through row or through Column), i.e. all the queen place are safe. Method : Backtracking Time Complexity : Worst Case - O(n!) Best Case - O(n^2) Hint : Since we can put only one queen in a particular row/column, we can assume the board to be a 1-d array where the index number of the array is the row number and the value in that position is the column of the placed Queen. We must also maintain seprate set for left and right diagonals. Then we can sequentially put the queen in every column of the given row and check wether the postition is right or not and recursively do so for the next row. If the recursion fails, we can backtrack and try a diffrent column and repeat the process. Pseudocode: --> Input: board_size 1) Maintain a set of occupied Diagonals and Columns diagonal1={};diagonal2={} Col={} ans=[] 2) Call the recursive function for row=0 3) In the Function,for a given row=R, try every Column C between 0 to n-1: Check if the Column is occupied and the respective diagonal (R-C) and (R+C) are occupied or not. If free, then add the column C and the Diagonals in their respective sets and call the recursive function for next row, i.r row=R+1 If not free, then try another column. 4) If in the recursive function, row= board_size. That means all the queens are placed and now we can stop the recursion. Visualization: --> board_size = 3 For R=0, placing queen at C=0 i.e at (0,0) +-------+-------+-------+ | | | | | X | | | | | | | +-------+-------+-------+ | | | | | | | | | | | | +-------+-------+-------+ | | | | | | | | | | | | +-------+-------+-------+ For R=1, Placing Queen at C=0, i.e at (1,0) But, column 0 is already occupied,so try next Col Placing Queen at C=1, i.e at (1,1) But, Diagonal2(R+C) i.e. 0 is already occupied Placing Queen at C=2, i.e. at (1,2) +-------+-------+-------+ | | | | | X | | | | | | | +-------+-------+-------+ | | | | | | | X | | | | | +-------+-------+-------+ | | | | | | | | | | | | +-------+-------+-------+ Queen is safe For R=2 Placing Queen at C=0, i.e at (2,0) +-------+-------+-------+ | | | | | X | | | | | | | +-------+-------+-------+ | | | | | | | X | | | | | +-------+-------+-------+ | | | | | X | | | | | | | +-------+-------+-------+ But, column 0 is already occupied,so try next Col Placing Queen at C=1, i.e.at (2,1) But, Diagonal1(R-C) i.e 1 already exist, Placing Queen at C=2, i.e at (2,2) But, column 2 is already occupied,so try next Col Placing Queen at C=3, but thats out of the board Hence recursion fails We back track to R=1, and place the queen in next Col And this will go On but there in no solution for a board of size 3, Hence the program will return -1. But for a board_size =4, the following will be one of the answer, [(0, 1), (1, 3), (2, 0), (3, 2)] +-------+-------+-------+-------+ | | | | | | | X | | | | | | | | +---------------+-------+-------+ | | | | | | | | | X | | | | | | +-------+-------+-------+-------+ | | | | | | X | | | | | | | | | +-------+-------+-------+-------+ | | | | | | | | X | | | | | | | +-------+-------+-------+-------+ """ print_msg_box(message) #print(n_queens(4))
def coin_change_hint(): message = """ Coin Change Problem ------------------------------------------------------------------------------------------ Purpose : In this problem, We are given coins of different denominations and a total amount of money amount. Calculate the fewest number of coins that we need to make up that amount. If no combination possible return -1. m = length of the coins array, n = amount Time Complexity : O(mn) Space Complexity : O(n) Hint : Apply Dynamic Programming on coins array to find the minimum required coins. Pseudocode: dp = [0]*(amount+1) LOOP (i = 1, amount+1): if i in coins: dp[i] = 1 continue min_coins = INFINITE LOOP coins array as coin: if i-coin >= 0: min_coins = min(dp[i-coin], min_coins) dp[i] = min_coins+1 return dp[-1] Visualization: coins = [1,2,5] amount = 6 dp = [0]*(amount+1) = [0]*7 index = [0,1,2,3,4,5,6] dp = [0,0,0,0,0,0,0] Applying recursive formula at each index we get min coins min_coins = min(dp[index-coin], min_coins) * At index 1: index = [0,1,2,3,4,5,6] dp = [0,1,0,0,0,0,0] * At index 2: index = [0,1,2,3,4,5,6] dp = [0,1,1,0,0,0,0] * At index 3: index = [0,1,2,3,4,5,6] dp = [0,1,1,2,0,0,0] * At index 4: index = [0,1,2,3,4,5,6] dp = [0,1,1,2,2,0,0] * At index 5: index = [0,1,2,3,4,5,6] dp = [0,1,1,2,2,1,0] * At index 6: index = [0,1,2,3,4,5,6] dp = [0,1,1,2,2,1,2] FINAL RESULT = dp[-1] = 2 Learn More: - Change Making - https://en.wikipedia.org/wiki/Change-making_problem """ print_msg_box(message) # print(coin_change([1,2,5],6,True))
def jump_search_hint(): message = """ Jump Search ------------------------------------ Purpose: Searching a required number Method: Iterating, Comparing Time Complexity: The optimal size of a block to be jumped is (√n). This makes the time complexity of Jump Search O(√n). Hint: Starting from 0th element of arr[], we comparing fewer elements (than linear search) by jumping ahead by fixed steps. Pseudocode: loc = -1 step = math.floor(math.sqrt(n)) for i in range[0,length of array,step to jump]: if(arr[i]==search) loc = i break else if(arr[i]<search) continue else if(arr[i]>search) j = i j -= (step-1) for k in range[j+1, i] if(arr[k]==search) loc = k break if(loc<0): return "Not found!" else: return loc Visualization: Number to Search => search = 233 Steps to be jumped = √n, where n = size of the array = √16 = 4 Given Array : +---+---+---+---+---+---+---+----+----+----+----+----+-----+-----+-----+-----+ | 0 | 1 | 1 | 2 | 3 | 5 | 8 | 13 | 21 | 34 | 55 | 89 | 144 | 233 | 377 | 610 | +---+---+---+---+---+---+---+----+----+----+----+----+-----+-----+-----+-----+ First Iteration (i = 0): +---+---+---+---+---+---+---+----+----+----+----+----+-----+-----+-----+-----+ | 0 | 1 | 1 | 2 | 3 | 5 | 8 | 13 | 21 | 34 | 55 | 89 | 144 | 233 | 377 | 610 | +---+---+---+---+---+---+---+----+----+----+----+----+-----+-----+-----+-----+ [checking if arr[0] > search, which is not true so going on the next iteration] Second Iteration (i = 3): +---+---+---+---+---+---+---+----+----+----+----+----+-----+-----+-----+-----+ | 0 | 1 | 1 | 2 | 3 | 5 | 8 | 13 | 21 | 34 | 55 | 89 | 144 | 233 | 377 | 610 | +---+---+---+---+---+---+---+----+----+----+----+----+-----+-----+-----+-----+ [checking if arr[3] > search, which is not true so going on the next iteration] Third Iteration (i = 6): +---+---+---+---+---+---+---+----+----+----+----+----+-----+-----+-----+-----+ | 0 | 1 | 1 | 2 | 3 | 5 | 8 | 13 | 21 | 34 | 55 | 89 | 144 | 233 | 377 | 610 | +---+---+---+---+---+---+---+----+----+----+----+----+-----+-----+-----+-----+ [checking if arr[6] > search, which is not true so going on the next iteration] Fourth Iteration (i = 9): +---+---+---+---+---+---+---+----+----+----+----+----+-----+-----+-----+-----+ | 0 | 1 | 1 | 2 | 3 | 5 | 8 | 13 | 21 | 34 | 55 | 89 | 144 | 233 | 377 | 610 | +---+---+---+---+---+---+---+----+----+----+----+----+-----+-----+-----+-----+ [checking if arr[9] > search, which is not true so going on the next iteration] Fifth Iteration (i = 12): +---+---+---+---+---+---+---+----+----+----+----+----+-----+-----+-----+-----+ | 0 | 1 | 1 | 2 | 3 | 5 | 8 | 13 | 21 | 34 | 55 | 89 | 144 | 233 | 377 | 610 | +---+---+---+---+---+---+---+----+----+----+----+----+-----+-----+-----+-----+ [checking if arr[12] > search, which is not true so going on the next iteration] Sixth Iteration (i = 15): +---+---+---+---+---+---+---+----+----+----+----+----+-----+-----+-----+-----+ | 0 | 1 | 1 | 2 | 3 | 5 | 8 | 13 | 21 | 34 | 55 | 89 | 144 | 233 | 377 | 610 | +---+---+---+---+---+---+---+----+----+----+----+----+-----+-----+-----+-----+ [checking if arr[15] > search, which is true so we go to for loop as linear search starts from i = 13 and ends at i = 14] First Sub - Iteration (j = 13) +-----+-----+-----+ | 233 | 377 | 610 | +-----+-----+-----+ [checking if arr[13] == search, which is true so the search returns location of search in the array.] If no element in the array matched search = 233 then it would return "Not found!" Learn More Here - https://en.wikipedia.org/wiki/Jump_search """ print_msg_box(message)
def min_product_subset_hint(): message = """ Minimum Product Subset of an Array ---------------------------------------------- Purpose : Finding the minimum product of the subset ,from the following subsets of an array Method : Time Complexity : Worst Case - 0(n) Hint : Finding the least product obtained among all the subsets of the given array Given an array a, we have to find minimum product possible with the subset of elements present in the array. The minimum product can be single element also. Pseudocode: -->Input: Set[], set_size 1. Get the size of power set power_set_size = pow(2, set_size) min_product = INT 2 Loop for counter from 0 to pow_set_size (a) Loop for i = 0 to set_size (i) Initialize product_temp to 1 (ii) If ith bit in counter is set Print ith element from set for this subset Update product_temp by multiplying with ith element (iii) Set max_product to min(min_product, product_temp) (b) Print separator for subsets i.e., newline Visualization: Input Array : +----+----+----+----+----+ | -1 | -1 | -2 | 4 | 3 | +----+----+----+----+----+ Step-1: Check if the length of the array = 1 Yes --> Minimum sum product = arr[0] No --> Push to Step-2 Step-2: Initializing int_neg = 0(No.of negative numbers in array) int_pos = 0(No.of positive numbers in array) int_zero = 0(No.of zeroes in array), max_neg = float('-inf'),min_pos = float('inf'), prod = 1(initial product of subset) Initializing i = 0 : int_neg = 1, max_neg = -1,product = 1*(-1) = -1 Initializing i = 1 : int_neg = 2, max_neg = -1,product = (-1)*(-1) = 1 Initializing i = 2 : int_neg = 3, max_neg = -2,product = 1*(-2) = -2 Initializing i = 3 : int_pos = 1, min_pos = 4,product = (-2)*4 = -8 Initializing i = 4 : int_pos = 2, min_pos = 3,product = (-8)*4 = -24 Step-3: a) Check If there are all zeros or no negative number present : Yes --> Minimum sum product = 0 No --> Go to (b) b) Check If there are all positive : Yes --> Minimum sum product = min_pos No --> Go to (c) c) Check If there are even number of negative numbers : Yes --> Minimum sum product = Product/max_neg No --> Minimum sum product = product Step-4: Prepare the output as "-24" """ print_msg_box(message)
def radix_sort_hint(): message = """ Radix Sort ------------------------------------ Purpose: Sorting a given array Method: Distributing, Non Comparing Time Complexity: Sorts in O(n+k) time when elements are in the range from 1 to k. Hint: From the given array, we sort the elements based on the i'th digit by performing counting sort on it until the unit value exceeds the maximum value in the array. Radix sort uses counting sort as a sub routine to sort elements. Pseudocode: Counting-Sort(A, n, unit) for j = 1 to d do int count[10] = {0}; for i = 0 to n do count[key of ((A[i]/unit)%10) in pass j]++ for k = 1 to 10 do count[k] = count[k] + count[k-1] for i = n-1 downto 0 do result[count[key of ((A[i]/unit)%10)] ] = A[j] count[key of(((A[i]/unit)%10)]-- for i= n-1 to 0 do A[i] = result[i] end for(j) end func Radix-Sort(A) unit = 1 while unit < max(A) Counting-Sort(A, unit) unit*=10 Visualization: Given Array : +-----+----+----+----+-----+----+---+----+ | 170 | 45 | 75 | 90 | 802 | 24 | 2 | 66 | +-----+----+----+----+-----+----+---+----+ First Iteration (unit = 1): +---+---+---+---+---+---+---+---+ | 0 | 5 | 5 | 0 | 2 | 4 | 2 | 6 | +---+---+---+---+---+---+---+---+ Count Array +---+---+---+---+---+---+---+---+---+---+ Count | 2 | 0 | 2 | 0 | 1 | 2 | 1 | 0 | 0 | 0 | +---+---+---+---+---+---+---+---+---+---+ Index | 0 | 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | Cumilative Count Array +---+---+---+---+---+---+---+---+---+---+ | 2 | 2 | 4 | 4 | 5 | 7 | 8 | 8 | 8 | 8 | +---+---+---+---+---+---+---+---+---+---+ From the first iteration array, we take each value as the index of the cumilative count array and that element provides the position in the result array. Once it is placed, the value in the cumilative count array, reduces by one. Example - First Iteration Array -> Value : 66 Pos = (66/1) Result[Count[int(Pos%10)]-1] = Result[Count[6]-1] = Result[7] = 66 Result Array +---+---+---+---+---+---+---+---+---+----+ | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 66 | +---+---+---+---+---+---+---+---+---+----+ Updated Cumilative Array +---+---+---+---+---+---+---+---+---+---+ | 2 | 2 | 4 | 4 | 5 | 7 | 8 | 8 | 8 | 7 | +---+---+---+---+---+---+---+---+---+---+ Final Result Array after First Iteration +-----+----+-----+---+----+----+----+----+ | 170 | 90 | 802 | 2 | 24 | 45 | 75 | 66 | +-----+----+-----+---+----+----+----+----+ Second Iteration (unit = 10): +---+---+---+---+---+---+---+---+ | 7 | 9 | 0 | 0 | 2 | 4 | 7 | 6 | +---+---+---+---+---+---+---+---+ Count Array +---+---+---+---+---+---+---+---+---+---+ Count | 2 | 0 | 1 | 0 | 1 | 0 | 1 | 2 | 0 | 1 | +---+---+---+---+---+---+---+---+---+---+ Index | 0 | 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | Cumilative Count Array +---+---+---+---+---+---+---+---+---+---+ | 2 | 2 | 3 | 3 | 4 | 4 | 5 | 7 | 7 | 8 | +---+---+---+---+---+---+---+---+---+---+ Final Result Array after Second Iteration +-----+---+----+----+----+-----+----+----+ | 802 | 2 | 24 | 45 | 66 | 170 | 75 | 90 | +-----+---+----+----+----+-----+----+----+ Third Iteration (unit = 100): +---+---+---+---+---+---+---+---+ | 8 | 0 | 0 | 0 | 0 | 1 | 0 | 6 | +---+---+---+---+---+---+---+---+ Count Array +---+---+---+---+---+---+---+---+---+---+ Count | 6 | 1 | 0 | 0 | 0 | 0 | 0 | 0 | 1 | 0 | +---+---+---+---+---+---+---+---+---+---+ Index | 0 | 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | Cumilative Count Array +---+---+---+---+---+---+---+---+---+---+ | 6 | 7 | 7 | 7 | 7 | 7 | 7 | 7 | 8 | 8 | +---+---+---+---+---+---+---+---+---+---+ Final Result Array after Third (And Final) Iteration +---+----+----+----+----+----+-----+-----+ | 2 | 24 | 45 | 66 | 75 | 90 | 170 | 802 | +---+----+----+----+----+----+-----+-----+ Learn More Here - https://en.wikipedia.org/wiki/Radix_sort """ print_msg_box(message)
def bogo_sort_hint(): message = """ Bogo Sort ------------------------------------ Purpose : Sorting in increasing/decreasing order as specified by asc argument Method : Choose the sorted one among all the permutations Time Complexity: Worst Case - O(n!) Hint : Generate all the possible permutations of the elements in the array and check which is sorted. Pseudocode: --> for sequence in all_permutations if is_sorted(array[sequence]) return array[sequence] Visualization: Given Array : +-----+-----+-----+ | 5 | 4 | 3 | +-----+-----+-----+ First Permutation : +-----+-----+-----+ | 5 | 4 | 3 | +-----+-----+-----+ Is it sorted? NO Second Permutation : +-----+-----+-----+ | 5 | 3 | 4 | +-----+-----+-----+ Is it sorted? NO Third Permutation : +-----+-----+-----+ | 4 | 5 | 3 | +-----+-----+-----+ Is it sorted? NO Third Permutation : +-----+-----+-----+ | 4 | 3 | 5 | +-----+-----+-----+ Is it sorted? NO Third Permutation : +-----+-----+-----+ | 3 | 4 | 5 | +-----+-----+-----+ Is it sorted? YES Learn More Here - https://en.wikipedia.org/wiki/Bogosort """ print_msg_box(message)