SlideShare a Scribd company logo
Transforming Education
Ice Breaker (5 mins)
2
If you were stranded on a desert island,
what three items would you want to have
with you?
Sorting Techniques-I
Concepts
● Sorting Techniques
○ Internal Sorting
■ Bubble Sort
■ Selection Sort
■ Insertion Sort
○ External Sorting
4
Questions for this session
We will answer the following questions in this session-
● How can we sort elements in a 1-D array?
5
Sorting Techniques
6
● Internal Sorting Techniques
○ Bubble Sort
○ Selection Sort
○ Insertion Sort
○ Quick Sort
○ Radix Sort
● External Sorting Techniques
○ Merge Sort
Bubble Sort
7
● Used for sorting small set of numbers
● Compares all the elements one by one and
sorts them based on their values
● n-1 iterations/ passes are required for
sorting n numbers
Algorithm/Pseudocode: Bubble Sort
8
Algorithm Bubble_Sort ( A, n) where A is a 1-D array of n elements to be sorted
for (i = 0 ; i < ( n - 1 ); i++)
{
for (j = 0 ; j < (n - i - 1); j++)
{
if (a[j] > a[j+1])
{
Swap a[j] and a[j+1]
}
}
}
C Program: Bubble Sort
9
#include <stdio.h>
void bubble_sort(int[], int);
int main() {
int a[100], n, i;
printf("Enter the number of elementsn");
scanf("%d", &n);
printf("Enter the elementsn");
for (i = 0; i < n; i++)
scanf("%d", &a[i]);
bubble_sort(a, n);
printf("Sorted list in ascending order:n");
for ( i = 0 ; i < n ; i++ )
printf("%dn", a[i]);
return 0; }
void bubble_sort(int a[], int n)
{
int i, j, t;
for (i = 0 ; i < ( n - 1 ); i++)
{
for (j = 0 ; j < (n - i - 1); j++)
{
if (a[j] > a[j+1])
{
t = a[j];
a[j] = a[j+1];
a[j+1] = t;
} } } }
Time Complexity of Bubble Sort-
● Best Case Complexity- O(n2
)
● Average Case Complexity- O(n2
)
● Worst Case Complexity- O(n2
)
Advantages-
● It is popular and easy to implement
● It sorts the numbers without using additional temporary storage
Disadvantage-
● This approach does not work for lists having large number of elements
Complexity, Advantages and Disadvantage: Bubble Sort
10
Insertion Sort
11
● Simple sorting algorithm
● Sorts the elements by shifting them one at a
time
● n-1 iterations/passes
● Sorting starts with the second element as the key
● Key compared with elements before it
● Key is then put in its appropriate location
Algorithm/Pseudocode: Insertion Sort
12
Algorithm Insertion_Sort ( A, n) where A is a 1-D array of n elements to be sorted
for (int i = 1 ; i <= n - 1; i++)
{
int j = i, t;
while ( j > 0)
{
if(arr[j] < arr[j-1])
{
Swap arr[j] and arr[j-1]
}
j--;
}
}
Identify the solution: Insertion Sort (5 mins)
13
Can you quickly write the C program for
implementing Insertion Sort algorithm?
Solution: Insertion Sort: C Program
14
#include <stdio.h>
void insertion_sort(int[], int);
int main() {
int a[100], n, i;
printf("Enter the number of elementsn");
scanf("%d", &n);
printf("Enter the elementsn");
for (i = 0; i < n; i++)
scanf("%d", &a[i]);
insertion_sort(a, n);
printf("Sorted list in ascending order:n");
for ( i = 0 ; i < n ; i++ )
printf("%dn", a[i]);
return 0; }
void insertion_sort(int a[], int n)
{
for (int i = 1 ; i <= n - 1; i++)
{
int j = i, t;
while ( j > 0)
{
if(arr[j] < arr[j-1])
{
t = arr[j];
arr[j] = arr[j-1];
arr[j-1] = t;
}
j--; } } }
Time Complexity of Bubble Sort-
● Best Case Complexity- O(n)
● Average Case Complexity- O(n2
)
● Worst Case Complexity- O(n2
)
Advantages-
● This algorithm has the simplest implementation
● It is stable and does not change the relative ordering of elements with equal values
● It is more efficient compared to Bubble and Selection Sort
Disadvantages-
● This algorithm works well for smaller data sets but is not suitable for larger data sets
● It requires additional memory space for sorting the elements
Complexity, Advantages and Disadvantages: Insertion Sort
15
InClass Activity: Selection Sort
Instructions:
Activity Type- Exploration
Students can divide themselves into groups of 2
Time Allotted for this activity is 20 minutes
Question:
Develop and Analyze the algorithm/ C program for implementing Selection Sort technique
16
Solution: Selection Sort
● Simple sorting technique
● Uses n-1 iterations/passes for sorting n elements
● Works as follows-
○ 1st iteration- Replaces smallest array element with the
element in 0th position/ index
○ 2nd iteration- Replaces second smallest array element
with the element in 1st index/ position and so on
○ This continues till all elements are sorted
17
Solution: Algorithm of Selection Sort
Algorithm Selection_Sort ( A, n) where A is a 1-D array of n elements to be sorted
for (int i = 0 ; i < ( n - 1 ) ; i++ )
{
temp = i;
for (j = i + 1 ; j < n ; j++ )
{
if ( arr[temp] > arr[j] )
temp = j;
}
if ( temp != i )
{
Swap a[i] and a[temp]
}
}
18
void selection_sort(int a[], int n) {
int temp,t,j;
for (int i = 0 ; i < ( n - 1 ) ; i++ ) {
temp = i;
for (j = i + 1 ; j < n ; j++ ) {
if ( arr[temp] > arr[j] )
temp = j;
}
if ( temp != i ) {
t = arr[i];
arr[i] = arr[temp];
arr[temp] = t;
} } }
Solution: C Program for Selection Sort
19
#include <stdio.h>
void selection_sort(int[], int);
int main() {
int a[100], n, i;
printf("Enter the number of elementsn");
scanf("%d", &n);
printf("Enter the elementsn");
for (i = 0; i < n; i++)
scanf("%d", &a[i]);
selection_sort(a, n);
printf("Sorted list in ascending order:n");
for ( i = 0 ; i < n ; i++ )
printf("%dn", a[i]);
return 0; }
Time Complexity of Selection Sort-
● Best Case Complexity- O(n2
)
● Average Case Complexity- O(n2
)
● Worst Case Complexity- O(n2
)
Advantages-
● It is popular and easy to implement
● It sorts the numbers without using additional temporary storage
Disadvantage-
● This approach does not work for lists having large number of elements
Solution: Complexity, Advantages and Disadvantage of Selection Sort
20
Learning Outcomes
In this session, you have learnt to:
1. Define the basic concepts of sorting the array elements
2. Explain the algorithms for internal sorting techniques
3. Design and implement the appropriate sorting technique for developing solutions to real-world
problems and applications in C
4. Students will be able to analyze the complexity of sorting algorithms
5. Students will be able to sort a list of elements by deciding and choosing the appropriate sorting
algorithm
Go through the following learning resources on the platform
● Sorting Techniques-I
21
Q & A
If you have more questions, please post them in the community on
the platform.
22
What Next?
In the next session the following concepts will be covered
● Sorting Techniques
○ Quick Sort
○ Merge Sort
○ Radix Sort
Go through the following learning resources on the platform
• Sorting Techniques-II
23

More Related Content

PPTX
my docoment
PPT
Lecture_4 (Sorting Algorithms) before mids - Copy.ppt
PPTX
searching in data structure.pptx
PPTX
Algorithms and Data Structures - Parahyangan Catholic University Credit Lionov
PPT
03_sorting123456789454545454545444543.ppt
PPT
03_sorting and it's types with example .ppt
PPT
quicksortnmsd cmz ,z m,zmm,mbfjjjjhjhfjsg
PDF
Sorting
my docoment
Lecture_4 (Sorting Algorithms) before mids - Copy.ppt
searching in data structure.pptx
Algorithms and Data Structures - Parahyangan Catholic University Credit Lionov
03_sorting123456789454545454545444543.ppt
03_sorting and it's types with example .ppt
quicksortnmsd cmz ,z m,zmm,mbfjjjjhjhfjsg
Sorting

Similar to 465659705-15-DSA-PPT-Sorting-Techniques-I.pdf (20)

PDF
Sorting
PPT
Sorting Algorithms for data struture prog
PDF
Analysis and design of algorithms part2
PPT
Data Structures 6
PPT
Unit6 C
PDF
Insertion sort
PPSX
Lecture 2 data structures & algorithms - sorting techniques
PPTX
Sorting algorithms
PPTX
LectureSlidData_sturcture_algorithm_v2.pptx
PPT
Insersion & Bubble Sort in Algoritm
PDF
Sorting
PPTX
Sorting pnk
PDF
Sorting algorithms bubble sort to merge sort.pdf
PPTX
Introduction to Algorithms
PDF
jn;lm;lkm';m';;lmppt of data structure.pdf
PDF
Iare ds ppt_3
PPTX
Chapter 8 Sorting in the context of DSA.pptx
PPT
07-sorting.ppt for insertion sort and bubble sorting technquies
Sorting
Sorting Algorithms for data struture prog
Analysis and design of algorithms part2
Data Structures 6
Unit6 C
Insertion sort
Lecture 2 data structures & algorithms - sorting techniques
Sorting algorithms
LectureSlidData_sturcture_algorithm_v2.pptx
Insersion & Bubble Sort in Algoritm
Sorting
Sorting pnk
Sorting algorithms bubble sort to merge sort.pdf
Introduction to Algorithms
jn;lm;lkm';m';;lmppt of data structure.pdf
Iare ds ppt_3
Chapter 8 Sorting in the context of DSA.pptx
07-sorting.ppt for insertion sort and bubble sorting technquies
Ad

Recently uploaded (20)

PPTX
IMMUNITY IMMUNITY refers to protection against infection, and the immune syst...
PPTX
Cell Types and Its function , kingdom of life
PDF
ANTIBIOTICS.pptx.pdf………………… xxxxxxxxxxxxx
PPTX
Lesson notes of climatology university.
PPTX
school management -TNTEU- B.Ed., Semester II Unit 1.pptx
PPTX
Pharmacology of Heart Failure /Pharmacotherapy of CHF
PPTX
Final Presentation General Medicine 03-08-2024.pptx
PDF
Saundersa Comprehensive Review for the NCLEX-RN Examination.pdf
PDF
Complications of Minimal Access Surgery at WLH
PPTX
Final Presentation General Medicine 03-08-2024.pptx
PDF
GENETICS IN BIOLOGY IN SECONDARY LEVEL FORM 3
PPTX
master seminar digital applications in india
PPTX
GDM (1) (1).pptx small presentation for students
PDF
102 student loan defaulters named and shamed – Is someone you know on the list?
PDF
Black Hat USA 2025 - Micro ICS Summit - ICS/OT Threat Landscape
PDF
Microbial disease of the cardiovascular and lymphatic systems
PDF
O5-L3 Freight Transport Ops (International) V1.pdf
PPTX
Microbial diseases, their pathogenesis and prophylaxis
PPTX
PPT- ENG7_QUARTER1_LESSON1_WEEK1. IMAGERY -DESCRIPTIONS pptx.pptx
PDF
FourierSeries-QuestionsWithAnswers(Part-A).pdf
IMMUNITY IMMUNITY refers to protection against infection, and the immune syst...
Cell Types and Its function , kingdom of life
ANTIBIOTICS.pptx.pdf………………… xxxxxxxxxxxxx
Lesson notes of climatology university.
school management -TNTEU- B.Ed., Semester II Unit 1.pptx
Pharmacology of Heart Failure /Pharmacotherapy of CHF
Final Presentation General Medicine 03-08-2024.pptx
Saundersa Comprehensive Review for the NCLEX-RN Examination.pdf
Complications of Minimal Access Surgery at WLH
Final Presentation General Medicine 03-08-2024.pptx
GENETICS IN BIOLOGY IN SECONDARY LEVEL FORM 3
master seminar digital applications in india
GDM (1) (1).pptx small presentation for students
102 student loan defaulters named and shamed – Is someone you know on the list?
Black Hat USA 2025 - Micro ICS Summit - ICS/OT Threat Landscape
Microbial disease of the cardiovascular and lymphatic systems
O5-L3 Freight Transport Ops (International) V1.pdf
Microbial diseases, their pathogenesis and prophylaxis
PPT- ENG7_QUARTER1_LESSON1_WEEK1. IMAGERY -DESCRIPTIONS pptx.pptx
FourierSeries-QuestionsWithAnswers(Part-A).pdf
Ad

465659705-15-DSA-PPT-Sorting-Techniques-I.pdf

  • 2. Ice Breaker (5 mins) 2 If you were stranded on a desert island, what three items would you want to have with you?
  • 4. Concepts ● Sorting Techniques ○ Internal Sorting ■ Bubble Sort ■ Selection Sort ■ Insertion Sort ○ External Sorting 4
  • 5. Questions for this session We will answer the following questions in this session- ● How can we sort elements in a 1-D array? 5
  • 6. Sorting Techniques 6 ● Internal Sorting Techniques ○ Bubble Sort ○ Selection Sort ○ Insertion Sort ○ Quick Sort ○ Radix Sort ● External Sorting Techniques ○ Merge Sort
  • 7. Bubble Sort 7 ● Used for sorting small set of numbers ● Compares all the elements one by one and sorts them based on their values ● n-1 iterations/ passes are required for sorting n numbers
  • 8. Algorithm/Pseudocode: Bubble Sort 8 Algorithm Bubble_Sort ( A, n) where A is a 1-D array of n elements to be sorted for (i = 0 ; i < ( n - 1 ); i++) { for (j = 0 ; j < (n - i - 1); j++) { if (a[j] > a[j+1]) { Swap a[j] and a[j+1] } } }
  • 9. C Program: Bubble Sort 9 #include <stdio.h> void bubble_sort(int[], int); int main() { int a[100], n, i; printf("Enter the number of elementsn"); scanf("%d", &n); printf("Enter the elementsn"); for (i = 0; i < n; i++) scanf("%d", &a[i]); bubble_sort(a, n); printf("Sorted list in ascending order:n"); for ( i = 0 ; i < n ; i++ ) printf("%dn", a[i]); return 0; } void bubble_sort(int a[], int n) { int i, j, t; for (i = 0 ; i < ( n - 1 ); i++) { for (j = 0 ; j < (n - i - 1); j++) { if (a[j] > a[j+1]) { t = a[j]; a[j] = a[j+1]; a[j+1] = t; } } } }
  • 10. Time Complexity of Bubble Sort- ● Best Case Complexity- O(n2 ) ● Average Case Complexity- O(n2 ) ● Worst Case Complexity- O(n2 ) Advantages- ● It is popular and easy to implement ● It sorts the numbers without using additional temporary storage Disadvantage- ● This approach does not work for lists having large number of elements Complexity, Advantages and Disadvantage: Bubble Sort 10
  • 11. Insertion Sort 11 ● Simple sorting algorithm ● Sorts the elements by shifting them one at a time ● n-1 iterations/passes ● Sorting starts with the second element as the key ● Key compared with elements before it ● Key is then put in its appropriate location
  • 12. Algorithm/Pseudocode: Insertion Sort 12 Algorithm Insertion_Sort ( A, n) where A is a 1-D array of n elements to be sorted for (int i = 1 ; i <= n - 1; i++) { int j = i, t; while ( j > 0) { if(arr[j] < arr[j-1]) { Swap arr[j] and arr[j-1] } j--; } }
  • 13. Identify the solution: Insertion Sort (5 mins) 13 Can you quickly write the C program for implementing Insertion Sort algorithm?
  • 14. Solution: Insertion Sort: C Program 14 #include <stdio.h> void insertion_sort(int[], int); int main() { int a[100], n, i; printf("Enter the number of elementsn"); scanf("%d", &n); printf("Enter the elementsn"); for (i = 0; i < n; i++) scanf("%d", &a[i]); insertion_sort(a, n); printf("Sorted list in ascending order:n"); for ( i = 0 ; i < n ; i++ ) printf("%dn", a[i]); return 0; } void insertion_sort(int a[], int n) { for (int i = 1 ; i <= n - 1; i++) { int j = i, t; while ( j > 0) { if(arr[j] < arr[j-1]) { t = arr[j]; arr[j] = arr[j-1]; arr[j-1] = t; } j--; } } }
  • 15. Time Complexity of Bubble Sort- ● Best Case Complexity- O(n) ● Average Case Complexity- O(n2 ) ● Worst Case Complexity- O(n2 ) Advantages- ● This algorithm has the simplest implementation ● It is stable and does not change the relative ordering of elements with equal values ● It is more efficient compared to Bubble and Selection Sort Disadvantages- ● This algorithm works well for smaller data sets but is not suitable for larger data sets ● It requires additional memory space for sorting the elements Complexity, Advantages and Disadvantages: Insertion Sort 15
  • 16. InClass Activity: Selection Sort Instructions: Activity Type- Exploration Students can divide themselves into groups of 2 Time Allotted for this activity is 20 minutes Question: Develop and Analyze the algorithm/ C program for implementing Selection Sort technique 16
  • 17. Solution: Selection Sort ● Simple sorting technique ● Uses n-1 iterations/passes for sorting n elements ● Works as follows- ○ 1st iteration- Replaces smallest array element with the element in 0th position/ index ○ 2nd iteration- Replaces second smallest array element with the element in 1st index/ position and so on ○ This continues till all elements are sorted 17
  • 18. Solution: Algorithm of Selection Sort Algorithm Selection_Sort ( A, n) where A is a 1-D array of n elements to be sorted for (int i = 0 ; i < ( n - 1 ) ; i++ ) { temp = i; for (j = i + 1 ; j < n ; j++ ) { if ( arr[temp] > arr[j] ) temp = j; } if ( temp != i ) { Swap a[i] and a[temp] } } 18
  • 19. void selection_sort(int a[], int n) { int temp,t,j; for (int i = 0 ; i < ( n - 1 ) ; i++ ) { temp = i; for (j = i + 1 ; j < n ; j++ ) { if ( arr[temp] > arr[j] ) temp = j; } if ( temp != i ) { t = arr[i]; arr[i] = arr[temp]; arr[temp] = t; } } } Solution: C Program for Selection Sort 19 #include <stdio.h> void selection_sort(int[], int); int main() { int a[100], n, i; printf("Enter the number of elementsn"); scanf("%d", &n); printf("Enter the elementsn"); for (i = 0; i < n; i++) scanf("%d", &a[i]); selection_sort(a, n); printf("Sorted list in ascending order:n"); for ( i = 0 ; i < n ; i++ ) printf("%dn", a[i]); return 0; }
  • 20. Time Complexity of Selection Sort- ● Best Case Complexity- O(n2 ) ● Average Case Complexity- O(n2 ) ● Worst Case Complexity- O(n2 ) Advantages- ● It is popular and easy to implement ● It sorts the numbers without using additional temporary storage Disadvantage- ● This approach does not work for lists having large number of elements Solution: Complexity, Advantages and Disadvantage of Selection Sort 20
  • 21. Learning Outcomes In this session, you have learnt to: 1. Define the basic concepts of sorting the array elements 2. Explain the algorithms for internal sorting techniques 3. Design and implement the appropriate sorting technique for developing solutions to real-world problems and applications in C 4. Students will be able to analyze the complexity of sorting algorithms 5. Students will be able to sort a list of elements by deciding and choosing the appropriate sorting algorithm Go through the following learning resources on the platform ● Sorting Techniques-I 21
  • 22. Q & A If you have more questions, please post them in the community on the platform. 22
  • 23. What Next? In the next session the following concepts will be covered ● Sorting Techniques ○ Quick Sort ○ Merge Sort ○ Radix Sort Go through the following learning resources on the platform • Sorting Techniques-II 23