Divide And Conquere Flashcards
What are the principles of Divide and Conquer
Divide − We divide the original problem into multiple sub-problems until they cannot be divided further.
Conquer − Then these subproblems are solved separately with the help of recursion
Combine − Once solved, all the subproblems are merged/combined together to form the final solution of the original problem.
How does merge sort work?
Step 1 − if it is only one element in the list it is already sorted, return.
Step 2 − divide the list recursively into two halves until it can no more be divided.
Step 3 − merge the smaller lists into new list in sorted order.
https://www.geeksforgeeks.org/merge-sort/
What is the complexity of Merge Sort?
+ On time O(N Log N)
+ + On Space O(N) as auxilarty array to sort the elements.
How do you create a seudo code of these steps:
Step 1 − if it is only one element in the list it is already sorted, return.
Step 2 − divide the list recursively into two halves until it can no more be divided.
Step 3 − merge the smaller lists into new list in sorted order.
procedure mergesort( a[]) if ( n == 1 ) return a al[] = a[0] ... a[n/2] ar[] = a[n/2+1] ... a[n] al = mergesort( al ) ar = mergesort( ar ) return merge( al, ar ) end procedure
What is the steps of Quicksot Algorithm?
Creating a recursive Algorithm that:
Step 1 − Make the right-most index value pivot
Step 2 − partition the array using pivot value
Step 3 − quicksort left partition recursively
Step 4 − quicksort right partition recursively
Being Step 2 (Partition) the key of the algorith, finding the pivot.
What is the complexity of QuickSort?
+ On time, Best case O(n log n) ; Worse Case O(n^2)
+ O(1) on space, the space required for swaping.
What is the key steo of QuikSort algorith and how does it work?
The partion.
We start from the leftmost element and keep track of the index of smaller (or equal) elements as i. While traversing, if we find a smaller element, we swap the current element with arr[i]. Otherwise, we ignore the current element
https://www.geeksforgeeks.org/quick-sort/
Ho
How Does Insertion Algotih work?
To sort an array of size N in ascending order iterate over the array and compare the current element (key) to its predecessor, if the key element is smaller than its predecessor, compare it to the elements before. Move the greater elements one position up to make space for the swapped element.
This Algorithm is not considered into the Divided And Conquere category.
https://www.geeksforgeeks.org/insertion-sort/
What is the complexity of Insertion Sort?
+ On Time: O(N^2) but O(N) in the best case, all the elements are sorted.
+ On space: O(1), it requieres 1 space for swaping.
How Does Buble Sort work?
1.Traverse from left and compare adjacent elements and the higher one is placed at right side.
2. In this way, the largest element is moved to the rightmost end at first.
3. This process is then continued to find the second largest and place it and so on until the data is sorted.
This Algorithm is not considered into the Divided And Conquere category.
https://www.geeksforgeeks.org/bubble-sort/
What is the complexity of Bubble Sort?
+ On Time: O(N^2)
+ On space: O(1), it requieres 1 space for swaping.
How does selection sort work?
election sort is a simple and efficient sorting algorithm that works by repeatedly selecting the smallest (or largest) element from the unsorted portion of the list and moving it to the sorted portion of the list.
This Algorithm is not considered into the Divided And Conquere category.
https://www.geeksforgeeks.org/sorting-algorithms/
What is the complexity of Selection Sort?
+ On Time: O(N^2)
+ On Space: O(1), it requieres 1 space for swaping
How does Heap Sort work?
Heap sort is a comparison-based sorting technique based on Binary Heap data structure. It is similar to the selection sort where we first find the minimum element and place the minimum element at the beginning. Repeat the same process for the remaining elements.
It is not considered an Divided And Conquere Algorith.
https://www.geeksforgeeks.org/heap-sort/
What is the node that replace the top node in a Heap Tree and what happends in that moment?
In this case, is the last item of the array that represents the Heap.
One you replace the last node as the new root, you need to compare its children and swap the hieghst, repeate the comaration until it becames a leaf or there is no greatest children.