0% found this document useful (0 votes)
16 views19 pages

Sorting Techniques

The document discusses various sorting techniques including bubble sort, insertion sort, merge sort, quick sort, heap sort, and selection sort. It provides details on the bubble sort and insertion sort algorithms, including pseudocode for the algorithms and examples. It analyzes the time complexity of insertion sort, noting that it is O(n^2) in the worst and average cases due to the number of comparisons required in the inner loop.

Uploaded by

Amar Thakur
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)
16 views19 pages

Sorting Techniques

The document discusses various sorting techniques including bubble sort, insertion sort, merge sort, quick sort, heap sort, and selection sort. It provides details on the bubble sort and insertion sort algorithms, including pseudocode for the algorithms and examples. It analyzes the time complexity of insertion sort, noting that it is O(n^2) in the worst and average cases due to the number of comparisons required in the inner loop.

Uploaded by

Amar Thakur
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/ 19

Lovely Professional University, Punjab

Data Structures
Lecture: Sorting Techniques
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)

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy