0% found this document useful (0 votes)
14 views

Sorting Algorithms 1

The document discusses different sorting algorithms: - It defines sorting as arranging data in a particular order like numerical or alphabetical. This allows for optimized searching. - It describes common sorting algorithms like bubble sort, insertion sort, and selection sort. Bubble sort compares adjacent elements and swaps them if out of order until sorted. Insertion sort maintains a sorted sub-list and inserts elements into their correct position within it. Selection sort divides the array into sorted and unsorted parts, selecting the minimum element and placing it at the front. - The time complexity of these algorithms is O(n2), making them inefficient for large data sets. The document provides examples and code implementations of each algorithm.
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
14 views

Sorting Algorithms 1

The document discusses different sorting algorithms: - It defines sorting as arranging data in a particular order like numerical or alphabetical. This allows for optimized searching. - It describes common sorting algorithms like bubble sort, insertion sort, and selection sort. Bubble sort compares adjacent elements and swaps them if out of order until sorted. Insertion sort maintains a sorted sub-list and inserts elements into their correct position within it. Selection sort divides the array into sorted and unsorted parts, selecting the minimum element and placing it at the front. - The time complexity of these algorithms is O(n2), making them inefficient for large data sets. The document provides examples and code implementations of each algorithm.
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 28

69

Lesson1. SORTING ALGORITHM

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.

Learning Module on PROG 201


70

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.

COMPLEXITY OF SORTING ALGORITHMS


• The complexity of sorting algorithm calculates the running time of a function in which 'n' number of
items are to be sorted. The choice for which sorting method is suitable for a problem depends on several
dependency configurations for different problems. The most noteworthy of these considerations are:

In-place VS Not-in-place Sorting

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.

➢ Sorting which uses equal or more space

➢ Merge-sort is an example of not-in-place sorting.

Learning Module on PROG 201


71

➢ Stable VS Not-Stable Sorting

Adaptive VS Non-Adaptive Sorting Algorithm

Learning Module on PROG 201


72

IMPORTANT TERMS

TYPES OF SORTING
TECHNIQUES

Learning Module on PROG 201


73

BUBBLE SORT ALGORITHM


• 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. 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 Bubble Sort Works?


We take an unsorted array for our example. Bubble sort takes Ο(n2) time so we're keeping it short and precise.

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.

Learning Module on PROG 201


74

➢ The new array should look like this −

D.

➢ Next we compare 33 and 35. We find that both are in already


sorted positions.
E.

➢ Then we move to the next two values, 35 and 10.


F.

➢ 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.

Learning Module on PROG 201


75

➢ Now we should look into some practical aspects of bubble sort.

ALGORITHM
We assume list is an array of n elements. We further assume
that swap function swaps the values of the given array elements.

Learning Module on PROG 201


76

C++ IMPLEMENTATION

OUTPUT

Learning Module on PROG 201


77

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.

➢ Insertion sort compares the first two elements.

A.

➢ It finds that both 14 and 33 are already in ascending order. For now, 14 is in sorted sub-list.

B.

➢ Insertion sort moves ahead and compares 33 with 27.

C.

➢ And finds that 33 is not in the correct position.

D.

Learning Module on PROG 201


78

➢ 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.

➢ These values are not in a sorted order.

G.
➢ So we swap them.

H.

➢ However, swapping makes 27 and 10 unsorted.

I.

➢ Hence, we swap them too.

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.

Learning Module on PROG 201


79

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.

Learning Module on PROG 201


80

C++ Implementation Code

OUTPUT

Learning Module on PROG 201


81

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.

THE COMPLEXITY OF SELECTION SORT TECHNIQUE


• Time Complexity: O(n2)
• Space Complexity: O(1)
Example
arr[] = 64 25 12 22 11
Example
➢ Find the minimum element in arr[0...4] and place it at beginning
A. 11 25 12 22 64
➢ Find the minimum element in arr[1...4] and place it at beginning of arr[1...4]
B. 11 12 25 22 64
➢ Find the minimum element in arr[2...4] and place it at beginning of arr[2...4]
C. 11 12 22 25 64
➢ Find the minimum element in arr[3...4] and place it at beginning of arr[3...4]
D. 11 12 22 25 64
ALGORITHM

Learning Module on PROG 201


82

C++ IMPLEMENTATION CODE

OUTPUT

Learning Module on PROG 201


83

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)

Learning Module on PROG 201


84

ALGORITHM
merge(array, left, middle, right)
Input: The data set array, left, middle and right index
Output: The merged list
mergeSort(array, left, right)

Learning Module on PROG 201


85

C++ IMPLEMENTATION CODE

Learning Module on PROG 201


86

OUTPUT:

Learning Module on PROG 201


87

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.

QUICK SORT PIVOT ALGORITHM


Based on our understanding of partitioning in quick sort, we will now try to write an algorithm for it, which is
as follows

Example:

LEGEND COLOR

LEFT =LO & RIGHT= HI PIVOT SORTED ELEMENT

Learning Module on PROG 201


88

Learning Module on PROG 201


89

QUICK SORT ALGORITHM

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

Learning Module on PROG 201


90

Learning Module on PROG 201


91

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.

Learning Module on PROG 201


92

Why array based representation for Binary Heap?


Since a Binary Heap is a Complete Binary Tree, it can be easily represented as array and array based
representation is space efficient. If the parent node is stored at index I, the left child can be calculated by 2
* I + 1 and right child by 2 * I + 2 (assuming the indexing starts at 0).

Heap Sort Algorithm for sorting in increasing order:


1. Build a max heap from the input data.
2. At this point, the largest
item is stored at the root of
the heap. Replace it with
the last item of the heap
followed by reducing the
size of heap by 1. Finally,
heapify the root of tree.
3. Repeat above steps
while size of heap is
greater than 1.

How to build the heap?


Heapify procedure can be
applied to a node only if
its children nodes are
heapified. So the
heapification must be
performed in the bottom
up order.

Learning Module on PROG 201


93

Learning Module on PROG 201


94

C++ Implementation Code

Given Array:

Learning Module on PROG 201


95

OUTPUT:

SUMMARY

Learning Module on PROG 201


96

• 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.

M2:L1 - ENRICHMENT ACTIVITY

Learning Module on PROG 201

You might also like