15 DSA PPT Sorting Techniques-I
15 DSA PPT Sorting Techniques-I
2
Sorting Techniques-I
Concepts
● Sorting Techniques
○ Internal Sorting
■ Bubble Sort
■ Selection Sort
■ Insertion Sort
○ External Sorting
4
Questions for this session
5
Sorting Techniques
6
Bubble Sort
7
Algorithm/Pseudocode: Bubble Sort
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
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
11
Algorithm/Pseudocode: Insertion Sort
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
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:
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
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:
3. Design and implement the appropriate sorting technique for developing solutions to real-world
problems and applications in C
5. Students will be able to sort a list of elements by deciding and choosing the appropriate sorting
algorithm
● Sorting Techniques-I
21
Q&A
22
What Next?
● Sorting Techniques
○ Quick Sort
○ Merge Sort
○ Radix Sort
• Sorting Techniques-II
23