Common Sorting Algorithms
Common Sorting Algorithms
Insertion sort
Insertion sort divides the list into two sub-list, sorted and unsorted. It takes one element at time and finds it appropriate
location in sorted sub-list and insert there. The output after insertion is a sorted sub-list. It iteratively works on all the
elements of unsorted sub-list and inserts them to sorted sub-list in order.
Both sorting techniques maintains two sub-lists, sorted and unsorted and both take one element at a time and places it
into sorted sub-list. Insertion sort works on the current element in hand and places it in the sorted array at appropriate
location maintaining the properties of insertion sort. Whereas, selection sort searches the minimum from the unsorted
sub-list and replaces it with the current element in hand.
Heapsort is a comparison-based sorting algorithm Binary Heap data structure. You can think of it as improved version f
selection sort, where it divides its input into a sorted and an unsorted region, and it iteratively shrinks the unsorted
region by extracting the largest element and moving that to the sorted region.
Merge Sort I Runtime: 0 ( n log (n)) average and worst case. Memory: Depends.
Merge sort divides the array in half, sorts each of those halves, and then merges them back together. Each
of those halves has the same sorting algorithm applied to it. Eventually, you are merging just two single element arrays.
It is the "merge" part that does all the heavy lifting.
Quick Sort I Runtime: O(n log(n)) average, O(n2 ) worst case. Memory: 0( log(n)).
In quick sort we pick a random element and partition the array, such that all numbers that are less than the
partitioning element come before all elements that are greater than it. The partitioning can be performed
efficiently through a series of swaps (see below).
If we repeatedly partition the array (and its sub-arrays) around an element, the array will eventually become
sorted. However, as the partitioned element is not guaranteed to be the median (or anywhere near the
median), our sorting could be very slow. This is the reason for the 0(n2) worst case runtime.
Linear Data Structures (elements are sequential and ordered in a way so that: there is only one first element and has
only one next element, there is only one last element and has only one previous element, while all other elements have
a next and a previous element.)
Arrays (An array is a linear data structure representing a group of similar elements, accessed by index. Size of an
array must be provided before storing data.)
Linked List (A linked list is a linear data structure with the collection of multiple nodes, where each element
stores its own data and a pointer to the location of the next element. The last link in a linked list points to null,
indicating the end of the chain. An element in a linked list is called a node. The first node is called the head. The
last node is called the tail.)
Stacks (Stack, an abstract data structure, is a collection of objects that are inserted and removed according to
the last-in-first-out (LIFO) principle. Objects can be inserted into a stack at any point of time, but only the most
recently inserted (that is, “last”) object can be removed at any time.)
Queues (Unlike a stack, the queue is a collection of objects that are inserted and removed according to the first-
in-first-out (FIFO) principle. That is, elements can be inserted at any point of time, but only the element that has
been in the queue the longest can be removed at any time.)
Hierarchical (Non-Linear) Data Structures (A data structure is said to be non-linear if traversal of nodes is nonlinear in
nature.)
Binary Trees (Binary Tree is a hierarchical tree data structures in which each node has at most two children,
which are referred to as the left child and the right child. Each binary tree has the following groups of nodes:
Root Node: It is the topmost node and often referred to as the main node because all other nodes can be
reached from the root Left Sub-Tree, which is also a binary tree Right Sub-Tree, which is also a binary tree)
Binary Search Tree (stores data in such a way that they can be retrieved very efficiently. The left subtree
contains nodes whose keys are less than the node’s key value, while the right subtree contains nodes whose
keys are greater than or equal to the node’s key value. Moreover, both subtrees are also binary search trees.)
Trie (Trie which is also known as "Prefix Trees", is a tree-like data structure which proves to be quite efficient for
solving problems related to strings. It provides fast retrieval, and mostly used for searching words in a dictionary,
providing auto suggestions in a search engine, and even for IP routing.)
Hash Tables (Hashing is a process used to uniquely identify objects and store each object at some pre-calculated
unique index called its "key." So, the object is stored in the form of a "key-value" pair, and the collection of such
items is called a "dictionary." Each object can be searched using that key. There are different data structures
based on hashing, but the most commonly used data structure is the hash table.)
Graphs (A graph is a set of nodes that are connected to each other in the form of a network. Nodes are also
called vertices. A pair(x,y) is called an edge, which indicates that vertex x is connected to vertex y. An edge may
contain weight/cost, showing how much cost is required to traverse from vertex x to y.)
Searching Algorithms
Linear search or sequential search is the simplest search algorithm. It involves sequential searching for an element in the
given data structure until either the element is found or the end of the structure is reached. If the element is found, then
the location of the item is returned otherwise the algorithm returns NULL.
What are the various operations that can be performed on different Data Structures?
Insertion ? Add a new data item in the given collection of data items.
Deletion ? Delete an existing data item from the given collection of data items.
Traversal ? Access each data item exactly once so that it can be processed. accessing and/or printing all data items
Searching ? Find out the location of the data item if it exists in the given collection of data items.
Sorting ? Arranging the data items in some order i.e. in ascending or descending order in case of numerical data
and in dictionary order in case of alphanumeric data.