CCC121-07-Sorting Algorithms
CCC121-07-Sorting Algorithms
01 Searching Algorithms
02 Sorting Algorithms
Note
Several resources will be presented in the following sections. These resources
provide interactive examples where you can run it and modify it for practice
purposes. Some resources also provide videos in the bottom part of the webpage
to understand the topic easier.
Suggestion: Running some of the interactive examples might not work on some mobile browsers.
You may copy & paste it instead on a mobile IDE (Ex. Dcoder) and run the program.
Searching Algorithms
Searching Algorithms are designed to check for an element or retrieve an element from
any data structure where it is stored. Based on the type of search operation, these
algorithms are generally classified into two categories:
• Sequential Search: In this, the list or array is traversed sequentially and every element is
checked. For example: Linear Search.
• Interval Search: These algorithms are specifically designed for searching in sorted data-
structures. These type of searching algorithms are much more efficient than Linear
Search as they repeatedly target the center of the search structure and divide the search
space in half. For Example: Binary Search.
Searching Algorithms
Linear Search
Linear search is a very simple search algorithm. In this type of search, a sequential search
is made over all items one by one. Every item is checked and if a match is found then that
particular item is returned, otherwise the search continues till the end of the data collection.
Binary Search
Binary search is a fast search algorithm with run-time complexity of Ο(log n). This search
algorithm works on the principle of divide and conquer. For this algorithm to work properly,
the data collection should be in the sorted form.
Binary search looks for a particular item by comparing the middle most item of the
collection. If a match occurs, then the index of item is returned. If the middle item is
greater than the item, then the item is searched in the sub-array to the right of the middle
item. Otherwise, the item is searched for in the sub-array to the left of the middle item.
This process continues on the sub-array as well until the size of the subarray reduces to
zero.
Searching Algorithms
Binary Search
Selection Sort
Selection sort is conceptually the most simplest sorting algorithm. This algorithm will first
find the smallest element in the array and swap it with the element in the first position,
then it will find the second smallest element and swap it with the element in the second
position, and it will keep on doing this until the entire array is sorted.
It is called selection sort because it repeatedly selects the next-smallest element and
swaps it into the right place.
Insertion Sort
Insertion sort is the sorting mechanism where the sorted array is built having one item at a
time. The array elements are compared with each other sequentially and then arranged
simultaneously in some particular order. The analogy can be understood from the style we
arrange a deck of cards. This sort works on the principle of inserting an element at a
particular position, hence the name Insertion Sort.
Bubble Sort
Merge Sort
Merge sort is a sorting technique based on divide and conquer technique. With worst-case
time complexity being Ο(n log n), it is one of the most respected algorithms. Merge sort
first divides the array into equal halves and then combines them in a sorted manner.
Heap Sort
Heap sort can be understood as the improved version of the binary search tree. It does not
create a node as in case of binary search tree instead it builds the heap by adjusting the
position of elements within the array itself. In which method a tree structure called heap is
used where a heap is a type of binary tree.
Quick Sort
Quick sort is a highly efficient sorting algorithm and is based on partitioning of array of
data into smaller arrays. A large array is partitioned into two arrays one of which holds
values smaller than the specified value, say pivot, based on which the partition is made
and another array holds values greater than the pivot value.
Quick sort partitions an array and then calls itself recursively twice to sort the two resulting
subarrays. This algorithm is quite efficient for large-sized data sets as its average and
worst case complexity are of O(nlogn), where n is the number of items.