0% found this document useful (0 votes)
6 views20 pages

Sorting Techniques

The document provides an overview of various sorting techniques, including Bubble Sort and Insertion Sort, along with their algorithms and complexities. Bubble Sort has a complexity of O(n^2) and involves repeatedly swapping adjacent elements if they are in the wrong order. Insertion Sort is also O(n^2) in the worst case and is efficient for small datasets, with a detailed algorithm provided for implementation.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
6 views20 pages

Sorting Techniques

The document provides an overview of various sorting techniques, including Bubble Sort and Insertion Sort, along with their algorithms and complexities. Bubble Sort has a complexity of O(n^2) and involves repeatedly swapping adjacent elements if they are in the wrong order. Insertion Sort is also O(n^2) in the worst case and is efficient for small datasets, with a detailed algorithm provided for implementation.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 20

Data Structures

Lecture: Sorting Techniques

By
Amit Sharma
Asst. Professor

Lovely Professional University, Punjab


Outlines
 Introduction
 Insertion Sort
 Merge Sort
 Bubble Sort
 Quick Sort
 Heap Sort
 Selection Sort
Sorting (Bubble Sort)
• Sorting refers to the operation of rearranging the elements of A
so they are in some particular order.

• Complexity of Bubble Sort Algorithm is: O(n2)

• Example of Bubble Sort


Bubble Sort Algorithm
Bubble (DATA, N)
1. Repeat Step 2 and 3 for K=1 to N-1.
2. [Initialize Pass Pointer P] Set P=1.
3. [Execute Pass] Repeat while P <= N-K.
(a) if DATA [P] > DATA [P+1], then:
Interchange DATA [P] and DATA[P+1]
[End of if Structure.]
(b) Set P = P+1.
[End of Inner Loop.]
[End of Step1 Outer Loop.]
4. Exit
Insertion Sort
Example of insertion sort
8 2 4 9 3 6
Example of insertion sort
8 2 4 9 3 6
Example of insertion sort
8 2 4 9 3 6

2 8 4 9 3 6
Example of insertion sort
8 2 4 9 3 6

2 8 4 9 3 6
Example of insertion sort
8 2 4 9 3 6

2 8 4 9 3 6

2 4 8 9 3 6
Example of insertion sort
8 2 4 9 3 6

2 8 4 9 3 6

2 4 8 9 3 6
Example of insertion sort
8 2 4 9 3 6

2 8 4 9 3 6

2 4 8 9 3 6

2 4 8 9 3 6
Example of insertion sort
8 2 4 9 3 6

2 8 4 9 3 6

2 4 8 9 3 6

2 4 8 9 3 6
Example of insertion sort
8 2 4 9 3 6

2 8 4 9 3 6

2 4 8 9 3 6

2 4 8 9 3 6

2 3 4 8 9 6
Example of insertion sort
8 2 4 9 3 6

2 8 4 9 3 6

2 4 8 9 3 6

2 4 8 9 3 6

2 3 4 8 9 6
Example of insertion sort
8 2 4 9 3 6

2 8 4 9 3 6

2 4 8 9 3 6

2 4 8 9 3 6

2 3 4 8 9 6

2 3 4 6 8 9 done
Insertion Sort
INSERTION_SORT (A, N)
1. Set A[0] = -∞.
2. Repeat Step 3 to 5 for K = 2, 3, …, N:
3. Set TEMP = A[K] and PTR = K – 1.
4. Repeat while TEMP < A[PTR]:
(a) Set A[PTR+1] = A[PTR]
(b) Set PTR = PTR – 1.
[End of Loop.]
5. Set A[PTR+1] = TEMP.
6. Return.
Insertion Sort Complexity
 This Sorting algorithm is frequently used when n is very
small.

 Worst case occurs when array is in reverse order. The inner


loop must use K – 1 comparisons.

f(n) = 1 + 2 + 3 + ….+ (n – 1) = n(n – 1)/2


= O(n2)

 In average case, there will be approximately (K – 1)/2


comparisons in the inner loop.
f(n) = 1 + 2 + 3 + ….+ (n – 1)/2 = n(n – 1)/4
= O(n2)
Animation Link
• Insertion Sort
• http://courses.cs.vt.edu/~csonline/Algorithms/Lessons/Insertio
nCardSort/insertioncardsort.swf

• https://www.hackerearth.com/practice/algorithms/sorting/bubb
le-sort/visualize/

• https://visualgo.net/en/sorting

You might also like