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

15 DSA PPT Sorting Techniques-I

The document discusses various sorting techniques for arranging elements in a 1-D array in ascending order. It explains the algorithms and code implementations for bubble sort, insertion sort, and selection sort. It also covers the time complexity, advantages, and disadvantages of each algorithm. The learning outcomes are defined as understanding sorting concepts, analyzing algorithm complexity, and choosing the appropriate sorting technique to solve problems. The next session will cover quick sort, merge sort, and radix sort.

Uploaded by

Sayak Mallick
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
482 views

15 DSA PPT Sorting Techniques-I

The document discusses various sorting techniques for arranging elements in a 1-D array in ascending order. It explains the algorithms and code implementations for bubble sort, insertion sort, and selection sort. It also covers the time complexity, advantages, and disadvantages of each algorithm. The learning outcomes are defined as understanding sorting concepts, analyzing algorithm complexity, and choosing the appropriate sorting technique to solve problems. The next session will cover quick sort, merge sort, and radix sort.

Uploaded by

Sayak Mallick
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 23

Transforming Education

Ice Breaker (5 mins)

If you were stranded on a desert island,


what three items would you want to have
with you?

2
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

● Internal Sorting Techniques


○ Bubble Sort
○ Selection Sort
○ Insertion Sort
○ Quick Sort
○ Radix Sort
● External Sorting Techniques
○ Merge Sort

6
Bubble Sort

● 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

7
Algorithm/Pseudocode: Bubble Sort

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]
}
}
}

8
C Program: Bubble Sort
#include <stdio.h> void bubble_sort(int a[], int n)
void bubble_sort(int[], int); {
int main() { int i, j, t;
int a[100], n, i; for (i = 0 ; i < ( n - 1 ); i++)
printf("Enter the number of elements\n"); {
scanf("%d", &n); for (j = 0 ; j < (n - i - 1); j++)
printf("Enter the elements\n"); {
for (i = 0; i < n; i++) if (a[j] > a[j+1])
scanf("%d", &a[i]); {
bubble_sort(a, n); t = a[j];
printf("Sorted list in ascending order:\n"); a[j] = a[j+1];
for ( i = 0 ; i < n ; i++ ) a[j+1] = t;
printf("%d\n", a[i]); } } } }
return 0; } 9
Complexity, Advantages and Disadvantage: Bubble Sort

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

10
Insertion Sort

● 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

11
Algorithm/Pseudocode: Insertion Sort

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--;
}
}
12
Identify the solution: Insertion Sort (5 mins)

Can you quickly write the C program for


implementing Insertion Sort algorithm?

13
Solution: Insertion Sort: C Program
#include <stdio.h> void insertion_sort(int a[], int n)
void insertion_sort(int[], int); {
int main() { for (int i = 1 ; i <= n - 1; i++)
int a[100], n, i; {
printf("Enter the number of elements\n"); int j = i, t;
scanf("%d", &n); while ( j > 0)
printf("Enter the elements\n"); {
for (i = 0; i < n; i++) if(arr[j] < arr[j-1])
scanf("%d", &a[i]); {
insertion_sort(a, n); t = arr[j];
printf("Sorted list in ascending order:\n"); arr[j] = arr[j-1];
for ( i = 0 ; i < n ; i++ ) arr[j-1] = t;
printf("%d\n", a[i]); }
return 0; } j--; } } } 14
Complexity, Advantages and Disadvantages: Insertion Sort

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
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
Solution: C Program for Selection Sort
#include <stdio.h>
void selection_sort(int a[], int n) {
void selection_sort(int[], int);
int temp,t,j;
int main() {
for (int i = 0 ; i < ( n - 1 ) ; i++ ) {
int a[100], n, i;
temp = i;
printf("Enter the number of elements\n");
for (j = i + 1 ; j < n ; j++ ) {
scanf("%d", &n);
if ( arr[temp] > arr[j] )
printf("Enter the elements\n");
temp = j;
for (i = 0; i < n; i++)
}
scanf("%d", &a[i]);
if ( temp != i ) {
selection_sort(a, n);
t = arr[i];
printf("Sorted list in ascending order:\n");
arr[i] = arr[temp];
for ( i = 0 ; i < n ; i++ )
arr[temp] = t;
printf("%d\n", a[i]);
} } }
return
19 0; }
Solution: Complexity, Advantages and Disadvantage of Selection Sort

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

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

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