You are on page 1of 40

CSE-141: Structured Programming

 Lecture 18: Sorting using Arrays


Instructor
Md. Sabir Hossain
Lecturer
Dept. of CSE, CUET
Mobile: 01737143868/01882826575
Email: sabirndc08cuet10@gmail.com
Recap - Arrays
Today’s Target – Sorting using Arrays
Sorting
 Place array into some
order
Ascending or
descending
 Many types
Simple: Brute force
More intelligent: Bubble,
selection, insertion, shell,
comb, merge, heap, quick,
counting, bucket, radix,
distribution, timsort,
gnome, cocktail, library,
cycle, binary tree, bogo,
pigeonhole, spread, bead,
pancake, …

5
Brute Force Sort
 Compare element to all elements below and then
move to next element, swap when appropriate

6
Brute Force Example

7
Selection Sort (Similar to Brute Force Sort)
• Idea:
– Find the smallest element in the array
– Exchange it with the element in the first position
– Find the second smallest element and exchange it with
the element in the second position
– Continue until the array is sorted

8
Example
8 4 6 9 2 3 1 1 2 3 4 9 6 8

1 4 6 9 2 3 8 1 2 3 4 6 9 8

1 2 6 9 4 3 8 1 2 3 4 6 8 9

1 2 3 9 4 6 8 1 2 3 4 6 8 9

9
Step by step Selection Sort

Source: https://www.programiz.com/dsa/selection-sort

10
Animation

11
Source Code
#include <stdio.h> for(steps=0;steps<n;++steps)
int main() for(i=steps+1;i<n;++i)
{ {
int A[100],i,n,steps,temp; if(A[steps]>A[i])
printf("Enter the number of {
elements to be sorted: "); temp=A[steps];
scanf("%d",&n); A[steps]=A[i];
for(i=0;i<n;++i) A[i]=temp;
{ }
printf("%d. Enter element: ",i+1); }
scanf("%d",&A[i]); printf("In ascending order: ");
} for(i=0;i<n;++i)
printf("%d ",A[i]);
return 0;
}

12
Bubble/Sinking Sort
 Compare adjacent elements, swap when appropriate
 Stop if no swaps on a pass

13
Bubble/Sinking Sort
 Compare adjacent elements, swap when appropriate
 Stop if no swaps on a pass

14
After 1St pass
Next Iterations

Source: https://www.youtube.com/watch?v=xli_FI7CuzA
Pseudo Code

-i
Source Code
/*C Program To Sort A in ascending order using for(i=0;i<n-1;++i)
bubble sort.*/ for(j=0;j<n-i-1;++j)
#include <stdio.h> {
int main() if(A[j]>A[j+1]) /* To sort in descending
{ order, change > to < in this line. */
int A[100],i,j, n,temp; {
printf("Enter the number of elements to be temp=A[j];
sorted: "); A[j]=A[j+1];
scanf("%d",&n); A[j+1]=temp;
printf("Enter elements: "); }
for(i=0;i<n;++i) }
{ printf("In ascending order: ");
scanf("%d",&A[i]); for(i=0;i<n;++i)
} printf("%d ",A[i]);
return 0;
}
Insertion Sort
• Idea: like sorting a hand of playing cards
– Start with an empty left hand and the cards facing
down on the table.
– Remove one card at a time from the table, and insert
it into the correct position in the left hand
• compare it with each of the cards already in the hand, from
right to left
– The cards held in the left hand are sorted
• these cards were originally the top cards of the pile on the
table

19
Insertion Sort

To insert 12, we need to


make room for it by moving
first 36 and then 24.
6 10 24 36

12

20
6 10 24 36

12

21
Insertion Sort

6 10 24 3
6

12

22
Insertion Sort

6 10 24 3
6

12

23
Insertion Sort

10 12 24
6 36

24
How insertion sort Algorithm works?

1 3 5 8 12

25
Insertion Sort
input array

5 2 4 6 1
3
at each iteration, the array is divided in two sub-arrays:

left sub-array right sub-array

sorted unsorted

26
See the animation

27
Insertion Sort

28
Source Code
/*Sorting Elements of an array in for(i=1;i<n;i++)
ascending order using insertion sort {
algorithm*/ temp = A[i];
#include<stdio.h> j=i-1;
int main() while(temp<A[j] && j>=0)
{
{
A[j+1] = A[j];
int A[100],n,temp,i,j; - - j;
printf("Enter number of terms(should be }
less than 100): "); A[j+1]=temp;
scanf("%d",&n); }
printf("Enter elements: "); printf("In ascending order: ");
for(i=0;i<n;i++) for(i=0; i<n; i++)
{ printf("%d\t",A[i]);
return 0;
scanf("%d",&A[i]);
}
}
/*To sort elements in descending order, change
temp<A[j] to temp>A[j] in above line.*/

29
Comparisons
Sorting Nearly Sorted Random Reversed
Algorithms

Bubble Sort

Insertion Sort

Selection Sort

30
Comparisons
Sorting Nearly Sorted Random Reversed
Algorithms

Bubble Sort

Insertion Sort

Selection Sort

31
Comparisons
Sorting Nearly Sorted Random Reversed
Algorithms

Bubble Sort

Insertion Sort

Selection Sort

32
Comparisons
Sorting Nearly Sorted Random Reversed
Algorithms

Bubble Sort

Insertion Sort

Selection Sort

33
Comparisons
Sorting Nearly Sorted Random Reversed
Algorithms

Bubble Sort

Insertion Sort

Selection Sort

34
Comparisons
Sorting Nearly Sorted Random Reversed
Algorithms

Bubble Sort

Insertion Sort

Selection Sort

35
Comparisons
Sorting Nearly Sorted Random Reversed
Algorithms

Bubble Sort

Insertion Sort

Selection Sort

36
Comparisons
Sorting Nearly Sorted Random Reversed
Algorithms

Bubble Sort

Insertion Sort

Selection Sort

37
References
• Introduction to Algorithms, Third Edition
(International Edition), Thomas H. Cormen,
Charles E. Leiserson, Ronald L. Rivest, Clifford
Stein, ISBN-13: 978-0262033848. Page: 41-50
• Fundamentals of Computer Algorithms,
Second Edition, Ellis Horowitz, Sanguthevar
Rajasekaran, Sartaj Sahni, ISBN 10:
8173716129 / ISBN 13: 9788173716126,
Published by Universities Press/Orient
BlackSwan, 2008. Page: 29-49
References
• http://www.geeksforgeeks.org/sorting-algorithms/
• https://visualgo.net/en/sorting
• http://sorting.at/
• https://www.toptal.com/developers/sorting-algorit
hms

39
• Allah Hafez

You might also like