Algorithm design, sorting and hashing Flashcards
Tehcniques
Good algorithm designers understand several fundamental al-
gorithm design techniques, including data structures, dynamic programming,
depth-first search, backtracking, and heuristics. Perhaps the single most im-
portant design technique is modeling, the art of abstracting a messy real-world
application into a clean problem suitable for algorithmic attack.
Resources
Good algorithm designers stand on the shoulders of giants.
Rather than laboring from scratch to produce a new algorithm for every task,
they can figure out what is known about a particular problem. Rather than
re-implementing popular algorithms from scratch, they seek existing imple-
mentations to serve as a starting point. They are familiar with many classic
algorithmic problems, which provide sufficient source material to model most
any application.
Characteristics of sorting
Time Complexity: Time complexity, a measure of how long it takes to run an algorithm, is used to categorize sorting algorithms. The worst-case, average-case, and best-case performance of a sorting algorithm can be used to quantify the time complexity of the process.
Space Complexity: Sorting algorithms also have space complexity, which is the amount of memory required to execute the algorithm.
Stability: A sorting algorithm is said to be stable if the relative order of equal elements is preserved after sorting. This is important in certain applications where the original order of equal elements must be maintained.
In-Place Sorting: An in-place sorting algorithm is one that does not require additional memory to sort the data. This is important when the available memory is limited or when the data cannot be moved.
Adaptivity: An adaptive sorting algorithm is one that takes advantage of pre-existing order in the data to improve performance.
Sorting
defined as the process of arranging a collection of data elements in a specific order, usually in ascending or descending order based on a specific attribute of the data elements.
Characteristics of sorting
Time Complexity: Time complexity, a measure of how long it takes to run an algorithm, is used to categorize sorting algorithms. The worst-case, average-case, and best-case performance of a sorting algorithm can be used to quantify the time complexity of the process.
Space Complexity: Sorting algorithms also have space complexity, which is the amount of memory required to execute the algorithm.
Stability: A sorting algorithm is said to be stable if the relative order of equal elements is preserved after sorting. This is important in certain applications where the original order of equal elements must be maintained.
In-Place Sorting: An in-place sorting algorithm is one that does not require additional memory to sort the data. This is important when the available memory is limited or when the data cannot be moved.
Adaptivity: An adaptive sorting algorithm is one that takes advantage of pre-existing order in the data to improve performance
Based on how they manage equal elements throughout the sorting process, sorting algorithms can be broadly categorized into two types:
Stable sorting algorithm
Unstable sorting algorithm.
The relative order of equal elements is preserved by stable sorting algorithms but is not ensured by unstable sorting algorithms.
Applications of sorting
Searching and retrieval of data
Data analysis and visualization
Database management and optimization
Sorting and organizing files and directories
Image and signal processing
Advantages of sorting
Improved search efficiency: It helps to organize data in a way that improves search efficiency.
Better data analysis: This algorithm can not only help in data analysis by identifying patterns, outliers, and trends but also help to summarize large datasets by grouping similar items together.
Improved database performance – This can improve the performance of databases by reducing the amount of time required to perform searches and data retrieval.
Easier data management – It can also make it easier to manage data by organizing it in a way that is easy to understand and work with.
Disadvantages of Sorting in DSA
Can be computationally expensive, especially for large datasets.
This may require additional memory to perform the sorting operation.
Maintaining the sort order of data can add complexity to data updates and insertions, which can be a disadvantage when dealing with dynamic datasets.
Bubble Sort algorithm
In this algorithm,
traverse from left and compare adjacent elements and the higher one is placed at right side.
In this way, the largest element is moved to the rightmost end at first.
This process is then continued to find the second largest and place it and so on until the data is sorted.
Selection Sort
The algorithm repeatedly selects the smallest (or largest) element from the unsorted portion of the list and swaps it with the first element of the unsorted part. This process is repeated for the remaining unsorted portion until the entire list is sorted.
Advantages of selection sort algorithm
Simple and easy to understand.
Works well with small datasets.
Disadvantages of selection sort algorithm
Selection sort has a time complexity of O(n^2) in the worst and average case.
Does not work well on large datasets.
Does not preserve the relative order of items with equal keys which means it is not stable.
Time Complexity Analysis of Selection Sort
The time complexity of Selection Sort is O(N2) as there are two nested loops:
One loop to select an element of Array one by one = O(N)
Another loop to compare that element with every other Array element = O(N)
Therefore overall complexity = O(N) * O(N) = O(N*N) = O(N2)
Auxiliary Space
O(1) as the only extra memory used is for temporary variables while swapping two values in Array. The selection sort never makes more than O(N) swaps and can be useful when memory writing is costly.
Insertion sort
simple sorting algorithm that works similar to the way you sort playing cards in your hands. The array is virtually split into a sorted and an unsorted part. Values from the unsorted part are picked and placed at the correct position in the sorted part.
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.
Time complexity of insertion sort
The worst-case time complexity of the Insertion sort is O(N^2)
The average case time complexity of the Insertion sort is O(N^2)
The time complexity of the best case is O(N).
The auxiliary space complexity of Insertion Sort is ____
O(1)