Dijkstra’s Algorithm for Adjacency List Representation

Updated on 15-Jun-2020 16:25:50
There is a given graph G(V, E) with its adjacency list representation, and a source vertex is also provided. Dijkstra’s algorithm to find the minimum shortest path between source vertex to any other vertex of the graph G.To Solve this problem, we will use two lists. One is to store vertices which have been considered as the shortest path tree, and another will hold the vertices which are not considered yet. In each phase of the algorithm, we find the unconsidered vertex and which has the minimum distance from the source.Another list is used to hold the predecessor node. Using ... Read More

Activity Selection Problem

Updated on 15-Jun-2020 16:28:54
There are n different activities are given with their starting time and ending time. Select the maximum number of activities to solve by a single person. We will use the greedy approach to find the next activity whose finish time is minimum among rest activities, and the start time is more than or equal with the finish time of the last selected activity.The complexity of this problem is O(n log n) when the list is not sorted.When the sorted list is provided the complexity will be O(n).Input and OutputInput: A list of different activities with starting and ending times. {(5, ... Read More

Shell Sort

Updated on 15-Jun-2020 14:58:31
The shell sorting technique is based on the insertion sort. In the insertion sort sometimes we need to shift large block to insert an item in the correct location. Using shell sort, we can avoid a large number of shifting. The sorting is done with a specific interval. After each pass, the interval is reduced to make the smaller interval.The complexity of the Shell Sort TechniqueTime Complexity: O(n log n) for best case, and for other cases, it depends on the gap sequence.Space Complexity: O(1)Input and OutputInput: The unsorted list: 23 56 97 21 35 689 854 12 47 66 ... Read More

Selection Sort

Updated on 15-Jun-2020 15:02:02
In the selection sort technique, the list is divided into two parts. In one part all elements are sorted and in another part the items are unsorted. At first, we take the maximum or minimum data from the array. After getting the data (say minimum) we place it at the beginning of the list by replacing the data of first place with the minimum data. After performing the array is getting smaller. Thus this sorting technique is done.The complexity of Selection Sort TechniqueTime Complexity: O(n^2)Space Complexity: O(1)Input and OutputInput: The unsorted list: 5 9 7 23 78 20 Output: Array ... Read More

Updated on 15-Jun-2020 15:20:04
Radix sort is a non-comparative sorting algorithm. This sorting algorithm works on the integer keys by grouping digits which share the same position and value. The radix is the base of a number system. As we know that in the decimal system the radix or base is 10. So for sorting some decimal numbers, we need 10 positional boxes to store numbers.The complexity of Radix Sort TechniqueTime Complexity: O(nk)Space Complexity: O(n+k)Input and OutputInput: The unsorted list: 802 630 20 745 52 300 612 932 78 187 Output: Data before Sorting: 802 630 20 745 52 300 612 932 78 187 ... Read More

Quick Sort

Updated on 15-Jun-2020 15:24:00
The quicksort technique is done by separating the list into two parts. Initially, a pivot element is chosen by partitioning algorithm. The left part of the pivot holds the smaller values than the pivot, and right part holds the larger value. After partitioning, each separate lists are partitioned using the same procedure.The complexity of Quicksort TechniqueTime Complexity: O(n log n) for best case and average case, O(n^2) for the worst case.Space Complexity: O(log n)Input and OutputInput: The unsorted list: 90 45 22 11 22 50 Output: Array before Sorting: 90 45 22 11 22 50 Array after Sorting: 11 22 ... Read More

Pigeonhole Sort

Updated on 15-Jun-2020 15:31:17
This is an example of the non-comparison sorting technique. It is used where the number of items and the range of possible key values is approximately the same.To perform this sort, we need to make some holes. The number of holes needed is decided by the range of numbers. In each hole, items are inserted. Finally deleted from the hole and stored into an array for sorted order.The complexity of Pigeon-Hole Sort TechniqueTime Complexity: O(n+2^k)Space Complexity: O(2^k)Input and OutputInput: The unsorted list: 802 630 20 745 52 300 612 932 78 187 Output: Data before Sorting: 802 630 20 745 ... Read More

Merge Sort

Updated on 15-Jun-2020 15:27:06
The merge sort technique is based on divide and conquers technique. We divide the whole dataset into smaller parts and merge them into a larger piece in sorted order. It is also very effective for worst cases because this algorithm has lower time complexity for the worst case also.The complexity of Merge Sort TechniqueTime Complexity: O(n log n) for all casesSpace Complexity: O(n)Input and OutputInput: The unsorted list: 14 20 78 98 20 45 Output: Array before Sorting: 14 20 78 98 20 45 Array after Sorting: 14 20 20 45 78 98Algorithmmerge(array, left, middle, right)Input − The data set array, left, middle ... Read More

Insertion Sort

Updated on 15-Jun-2020 15:35:20
This sorting technique is similar with the card sorting technique, in other words, we sort cards using insertion sort mechanism. For this technique, we pick up one element from the data set and shift the data elements to make a place to insert back the picked up an element into the data set.The complexity of the Insertion Sort TechniqueTime Complexity: O(n) for best case, O(n^2) for average and worst caseSpace Complexity: O(1)Input and OutputInput: The unsorted list: 9 45 23 71 80 55 Output: Array before Sorting: 9 45 23 71 80 55 Array after Sorting: 9 23 45 55 ... Read More

Heap Sort

Updated on 15-Jun-2020 15:39:25
Heap sort is performed on the heap data structure. We know that heap is a complete binary tree. Heap tree can be of two types. Min-heap or max heap. For min heap the root element is minimum and for max heap the root is maximum. After forming a heap, we can delete an element from the root and send the last element to the root. After these swapping procedure, we need to re-heap the whole array. By deleting elements from root we can sort the whole array.The complexity of Heap Sort TechniqueTime Complexity: O(n log n)Space Complexity: O(1)Input and OutputInput: A ... Read More