Sorting Algorithms 1
Sorting Algorithms 1
Sorting refers to arranging data in a particular format. Sorting algorithm specifies the way to arrange
data in a particular order. Most common orders are in numerical or lexicographical order. The importance of
sorting lies in the fact that data searching can be optimized to a very high level, if data is stored in a sorted
manner.
Lesson Objectives:
At the end of this lesson, you will be able to:
• Learn what is sorting.
• Learn the algorithm of sorting.
• Learn different types of sorting techniques
WHAT IS SORTING?
• Sorting refers to the operation or technique of arranging and rearranging sets of data in some specific
order.
• A collection of records called a list where every record has one or more fields.
• The fields which contain a unique value for each record is termed as the key field.
EXAMPLE OF SORTING
Example A
➢ The telephone directory stores the telephone numbers of people sorted by their names, so that the names
can be searched easily.
Example B
➢ The dictionary stores words in an alphabetical order so that searching of any word becomes easy.
CATEGORIES OF SORTING
A. Internal Sorting
• If all the data that is to be sorted can be adjusted at a time in the main memory, the internal sorting
method is being performed.
B. External Sorting
• When the data that is to be sorted cannot be accommodated in the memory at the same time and
some has to be kept in auxiliary memory such as hard disk, floppy disk, magnetic tapes etc, then
external sorting methods are performed.
In-place
➢ Sorting algorithms may require some extra space for comparison and temporary storage of few data
elements.
➢ These algorithms do not require any extra space and sorting is said to happen in-place, or for example,
within the array itself.
➢ Bubble sort is an example of in-place sorting.
Not-in-place
➢ The program requires space which is more than or equal to the elements being sorted.
IMPORTANT TERMS
TYPES OF SORTING
TECHNIQUES
EXAMPLE
➢ Bubble sort starts with very first two elements, comparing them to check which one is greater.
A.
➢ In this case, value 33 is greater than 14, so it is already in sorted locations. Next, we compare 33 with
27.
B.
➢ We find that 27 is smaller than 33 and these two values must be swapped.
C.
D.
➢ We know then that 10 is smaller 35. Hence they are not sorted.
G.
➢ We swap these values. We find that we have reached the end of the array. After one iteration, the array
should look like this −
➢ To be precise, we are now showing how an array should look like after each iteration. After the second
iteration, it should look like this −
H.
➢ Notice that after each iteration, at least one value moves at the end.
I.
➢ And when there's no swap required, bubble sorts learns that an array is completely sorted.
J.
ALGORITHM
We assume list is an array of n elements. We further assume
that swap function swaps the values of the given array elements.
C++ IMPLEMENTATION
OUTPUT
INSERTION SORT
•This is an in-place comparison-based sorting algorithm.
•A sub-list is maintained which is always sorted. For example, the lower part of an array is maintained to
be sorted. An element which is to be 'insert'ed in this sorted sub-list, has to find its appropriate place and
then it has to be inserted there. Hence the name, insertion sort.
• The array is searched sequentially and unsorted items are moved and inserted into the sorted sub-list (in
the same array). This algorithm is not suitable for large data sets as its average and worst case
complexity are of Ο(n2), where n is the number of items.
How Insertion Sort Works?
We take an unsorted array for our example.
A.
➢ It finds that both 14 and 33 are already in ascending order. For now, 14 is in sorted sub-list.
B.
C.
D.
➢ It swaps 33 with 27. It also checks with all the elements of sorted sub-list. Here we see that the sorted
sub-list has only one element 14, and 27 is greater than 14. Hence, the sorted sub-list remains sorted
after swapping.
E.
➢ By now we have 14 and 27 in the sorted sub-list. Next, it compares 33 with 10.
F.
G.
➢ So we swap them.
H.
I.
J.
➢ Again we find 14 and 10 in an unsorted order.
K.
➢ We swap them again. By the end of third iteration, we have a sorted sub-list of 4 items.
L.
This process goes on until all the unsorted values are covered in a sorted sub-list. Now we shall see some
programming aspects of insertion sort.
ALGORITHM
Now we have a bigger picture of how this sorting technique works, so we can derive simple steps by which we
can achieve insertion sort.
OUTPUT
SELECTION SORT
In the selection sort technique, the list is divided into two parts.
1. The subarray which is already sorted.
2. Remaining subarray which is 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.
OUTPUT
MERGE SORT
• The merge sort technique is based on divide and conquer technique. We divide the while data set 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 worst case also.
The complexity of Merge Sort Technique
• Time Complexity: O(n log n) for all cases
• Space Complexity: O(n)
MergeSort(arr[], l, r)
If r > l
1. Find the middle point to divide the array into two halves:
middle m = (l+r)/2
2. Call mergeSort for first half:
Call mergeSort(arr, l, m)
3. Call mergeSort for second half:
Call mergeSort(arr, m+1, r)
4. Merge the two halves sorted in step 2 and 3:
Call merge(arr, l, m, r)
ALGORITHM
merge(array, left, middle, right)
Input: The data set array, left, middle and right index
Output: The merged list
mergeSort(array, left, right)
OUTPUT:
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.
• Quicksort 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
O(nLogn) and image.png(n2), respectively.
• QuickSort is a Divide and Conquer algorithm. It picks an element as pivot and partitions the given array
around the picked pivot. There are many different versions of quickSort that pick pivot in different
ways.
Example:
LEGEND COLOR
Using pivot algorithm recursively, we end up with smaller possible partitions. Each partition is then processed
for quick sort. We define recursive algorithm for quicksort as follows −
C++ Implementation
HEAP SORT
• Heap sort is a comparison based sorting technique based on Binary Heap data structure. It is similar to
selection sort where we first find the maximum element and place the maximum element at the end. We
repeat the same process for remaining element.
• A Binary Heap is a Complete Binary Tree where items are stored in a special order such that value in a
parent node is greater(or smaller) than the values in its two children nodes. The former is called as max
heap and the latter is called min heap. The heap can be represented by binary tree or array.
Given Array:
OUTPUT:
SUMMARY
• Sorting refers to the operation or technique of arranging and rearranging sets of data in some specific
order.
• A collection of records called a list where every record has one or more fields.
• The fields which contain a unique value for each record is termed as the key field.
• Sorting algorithms may require some extra space for comparison and temporary storage of few data
elements.
• Bubble sort is a simple sorting algorithm. This sorting algorithm is comparison-based algorithm in
which each pair of adjacent elements is compared and the elements are swapped if they are not in order.
• An element which is to be 'inserted in this sorted sub-list, has to find its appropriate place and then it has
to be inserted there. Hence the name, insertion sort.
• In the selection sort technique, the list is divided into two parts subarray which is sorted and subarray
which is unsorted
• The merge sort technique is based on divide and conquer technique
• Quick sort is a highly efficient sorting algorithm and is based on partitioning of array of data into
smaller arrays.
• Heap sort is a comparison based sorting technique based on Binary Heap data structure. It is similar to
selection sort where we first find the maximum element and place the maximum element at the end. We
repeat the same process for remaining element.