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

4. Sorting

The document provides an overview of sorting algorithms, including definitions, examples, and detailed explanations of Bubble Sort, Selection Sort, and Insertion Sort. It describes how sorting arranges values in arrays in specific orders and outlines the steps and pseudocode for each algorithm. Additionally, it discusses when to use and when not to use these sorting methods based on data size and efficiency.

Uploaded by

meminebd
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)
8 views

4. Sorting

The document provides an overview of sorting algorithms, including definitions, examples, and detailed explanations of Bubble Sort, Selection Sort, and Insertion Sort. It describes how sorting arranges values in arrays in specific orders and outlines the steps and pseudocode for each algorithm. Additionally, it discusses when to use and when not to use these sorting methods based on data size and efficiency.

Uploaded by

meminebd
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/ 100

CSE-2101

Data Structures &


Algorithms I
Rumana Yasmin
Lecturer
DCSE, FST, BUP
Sorting
Definition and example

Definition: Sorting arranges values in an array in an specific


order. It may be alphabetical, increasing numerical and
decreasing numerical.

Example:
An12unsorted
45 array
9 of numbers
55 10 7

Sorted
7 List
9 (increasing
10 12or ascending
45 55order)

Aada
sorteddavi
array deb
of names
jack neil
m d
Sorting
● Sorting an array is the ordering the array elements in
● ascending (increasing: from min to max), or
● descending (decreasing: from max to min) order.

● Example:
● {2, 1, 5, 3, 2} → {1, 2, 2, 3, 5} ascending order
● {2, 1, 5, 3, 2} → {5, 3, 2, 2, 1} descending order
Sorting Algorithms
 Bubble Sort
 Insertion Sort
 Selection Sort
Bubble Sort
Bubble Sort
How it works:

1. Go through the array, one value at a time.


2. For each value, compare the value with
the next value.
3. If the value is higher than the next one,
swap the values so that the highest value
comes last.
4. Go through the array as many times as
there are values in the array.
Bubble Sort
Bubble Sort
Bubble Sort
• Can you see what happened to the highest value 12? It
has bubbled up to the end of the array, where it
belongs. But the rest of the array remains unsorted.

• So the Bubble Sort algorithm must run through the


array again, and again, and again, each time the next
highest value bubbles up to its correct position. The
sorting continues until the lowest value 3 is left at the
start of the array. This means that we need to run
through the array 4 times, to sort the array of 5 values.

• And each time the algorithm runs through the array, the
remaining unsorted part of the array becomes shorter.
Bubble Sort
Algorithm: BUBBLESORT(DATA, N)
Here DATA is an array with N elements. This
algorithm sorts the elements in DATA.
1. Repeat steps 2 and 3 for K=1 to N-1
2. set PTR := 1 [initializes pass pointer PTR]
3. repeat while PTR<= N-K:
a) If DATA[PTR] > DATA[PTR + 1] then:
interchange DATA[PTR] and DATA[PTR + 1]
[end of if structure]
b) Set PTR:=PTR + 1
[end of inner loop]
[end of step 1 outer loop]
4. Exit
Bubble Sort
Bubble Sort

● Smaller values in the list gradually “bubble” their way upward


to the top of the array.

● The technique makes several passes through the array.

● On each pass successive pairs of elements are compared.

● If the pair is in increasing order (or equal) the pair is


unchanged.

● If a pair is in descending order, their values are swapped in the


array.
Bubble Sort
When not to use

 When dealing with a large set of data.

 When you are looking for a quick algorithm.


Compared to other sorting algorithm,
bubble sort is really slow.
Bubble Sort
When to use

 Not much use in the real world, but it’s easy to


understand and fast to implement.

 It is used when a fast algorithm is needed to sort:

1) an extremely small set of data (Ex. Trying to get


the books on a library shelf back in order.) or

2) a nearly sorted set of data. (Ex. Trying to decide


which laptop to buy, because it is easier to
compare pairs of laptops one at a time and
decide which you prefer, than to look at them all
at once and decide which was best.)
Bubble Sort

Increasing order
PASS 1
Correc wrong
t order! Simulation
order!
62 62
43 43 45 34
76 76 6 90
34 9
45 76 66
6
0
Bubble Sort

Increasing order PASS 2


Correctwrong
order! order! Simulation
43 45
62 34
45 62
62 34 66 7 9
76 66
6 0
Bubble Sort

Increasing order PASS 3


Correctwrong
order! order!
Simulation
34 62 66 76 90
34 45
43 45
Bubble Sort

Increasing order
PASS 4
Correct wrong
order! order!
Simulation
43
34 43
34 45 62 66 76 90
Bubble Sort
Increasing order PASS 5
Correct
order! Simulation
34 43 45 62 66 76 90

There was no exchange or swap in pass 5. So we can


stop here otherwise it could go to pass 6
Bubble Sort

Pass = 1 Pass = 2 Pass = 3 Pass = 4


21532 12325 12235 12235
12532 12325 12235 12235
12532 12325 12235 12235
12352 12235 12235 12235
12325 12235 12235 12235

⮚ Underlined pairs show the comparisons. For each pass there are
size-1 comparisons.
⮚ Total number of comparisons = (size-1)2
Bubble Sort

Pass = 1 Pass = 2 Pass = 3 Pass = 4


54321 43215 32145 21345
45321 34215 23145 12345
43521 32415 21345 12345
43251 32145 21345 12345
43215 32145 22345 12345

⮚ Underlined pairs show the comparisons. For each pass there are
size-1 comparisons.
⮚ Total number of comparisons = (size-1)2
Bubble Sort
/* This program sorts the array elements in the ascending order*/

#include <stdio.h>
#define SIZE 5
void BubbleSort(int [ ]);
int main() {
int a[SIZE]= {2, 1, 5, 3, 2};
int i;
printf(“The elements of the array before sorting\n”);
for (i=0; i < SIZE; i++)
printf(“%4d”, a[i]);
BubbleSort(a);
printf(“\n\nThe elements of the array after sorting\n”);
for (i=0; i< SIZE; i++)
printf(“%4d”, a[i]);
return 0;
}
Bubble Sort : PseudoCode

void BubbleSort(int A[ ]) {
int i, pass, temp;
for (pass=0; pass < SIZE-1; pass++)
for (i=0; i < SIZE-pass-1; i++)
if(A[i] > A[i+1]){
temp = A[i];
A[i] = A[i+1];
A[i+1] = temp;
}
}
Selection Sort
Sorting
Selection Sort

Selection sort:
 Locate smallest element in array.
 Interchange it with element in position 0.
 Locate next smallest element in array.
 Interchange it with element in position 1.
 Continue until all elements are arranged in order.
Sorting
Selection Sort

Algorithm:
Input: A (array), N (#elements),

Step 1: Find the smallest element from the


positions starting at index to . Swap the smallest
element with the element at index .

Step 2: . If go to step 1
Selection Sort
Simulation and Pseudocode

SelectionSort(A, N)
Input: A (array), N (#elements)
Output: A (sorted array)

1. for i = 0 to N-2 do
2. minIndex = i
3. for j = i+1 to N-1 do i i i i i i i i i
4. if A[j] < A[minIndex] then j j j j j j j j j
5. minIndex = j
6. end if
7. end for
11 22
33 66 22
33
66 44
99 55
77 33
66
11 77
91 77
88
91
55 91
88 99
44
8. if minIndex ≠ i then
9. Swap A[i] and A[minIndex]
k k k k k k k k k k
10. end if
11. end for
Selection Sort : PseudoCode

void selectionSort(int array[], int size) {


for (int step = 0; step < size - 1; step++) {
int min_idx = step;
for (int i = step + 1; i < size; i++) {

if (array[i] < array[min_idx])


min_idx = i;
}

temp = array[min_idx];
array[min_idx] = array[step];
array[step] = temp;
}
}
Insertion Sort
Insertion Sort
It is a simple Sorting algorithm which sorts the array by shifting
elements one by one.

Following are some of the important characteristics of Insertion Sort.


 It has one of the simplest implementation
 It is efficient for smaller data sets, but very inefficient
for larger lists.
 Insertion Sort is adaptive, that means it reduces its total
number of steps if given a partially sorted list, hence it
increases its efficiency.
 It is Stable, as it does not change the relative order of
elements with equal keys
An Example: Insertion Sort

The idea of
insertion sort is
similar to the
idea of sorting
playing cards
To insert 12, we need to
make room for it by moving
first 36 and then 24.
An Example: Insertion Sort

A = {5, 2, 4, 6, 1, 3}
8 2 4 9 3 6
8 2 4 9 3 6
8 2 4 9 3 6

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

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

2 8 4 9 3 6

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

2 8 4 9 3 6

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

2 8 4 9 3 6

2 4 8 9 3 6

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

2 8 4 9 3 6

2 4 8 9 3 6

2 4 8 9 3 6
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
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
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
Example
:

9 2 7 5 1 4 3 6
Sorte
d
sectio
n

9 2 7 5 1 4 3 6

We start by dividing the array in two sections: a


sorted section and an unsorted section. We put the
first element as the only element in the sorted
section, and the rest of the array is the unsorted section.
Sorte Item
d to
sectio positio
n n

9 2 7 5 1 4 3 6

The first element in the unsorted section is


the next element to be put into the correct
position.
Item
to
positio
n

9 2 7 5 1 4 3 6

We copy the element to be placed into


another variable so it doesn’t get overwritten.
9 2 7 5 1 4 3 6

2
compar
e

If the previous position is more than


the item being placed, copy the value into the
next position
belongs
here

9 9 7 5 1 4 3 6

If there are no more items in the sorted section


to compare with, the item to be placed must go at
the front.
2 9 7 5 1 4 3 6
2 9 7 5 1 4 3 6
Item
to
positio
n

2 9 7 5 1 4 3 6
2 9 7 5 1 4 3 6

7
compar
e
Copied from
previous
position
belongs here

2 9 9 5 1 4 3 6

7
compar
e

If the item in the sorted section is less than the


item to place, the item to place goes after it in
the array.
2 7 9 5 1 4 3 6
2 7 9 5 1 4 3 6
Item
to
positio
n

2 7 9 5 1 4 3 6
2 7 9 5 1 4 3 6

5
compar
e
2 7 9 9 1 4 3 6

5
compar
e
belongs
here

2 7 7 9 1 4 3 6

5
compar
e
2 5 7 9 1 4 3 6
2 5 7 9 1 4 3 6
Item
to
positio
n

2 5 7 9 1 4 3 6
2 5 7 9 1 4 3 6

1
compar
e
2 5 7 9 9 4 3 6

1
compar
e
2 5 7 7 9 4 3 6

1
compar
e
2 5 5 7 9 4 3 6

1
compar
e
belongs
here

2 2 5 7 9 4 3 6

1
1 2 5 7 9 4 3 6
1 2 5 7 9 4 3 6
Item
to
positio
n

1 2 5 7 9 4 3 6
1 2 5 7 9 4 3 6

4
compar
e
1 2 5 7 9 9 3 6

4
compar
e
1 2 5 7 7 9 3 6

4
compar
e
belongs
here

1 2 5 5 7 9 3 6

4
compar
e
1 2 4 5 7 9 3 6
1 2 4 5 7 9 3 6
Item
to
positio
n

1 2 4 5 7 9 3 6
1 2 4 5 7 9 3 6

3
compar
e
1 2 4 5 7 9 9 6

3
compar
e
1 2 4 5 7 7 9 6

3
compar
e
1 2 4 5 5 7 9 6

3
compar
e
belongs
here

1 2 4 4 5 7 9 6

3
compar
e
1 2 3 4 5 7 9 6
1 2 3 4 5 7 9 6
Item
to
positio
n

1 2 3 4 5 7 9 6
1 2 3 4 5 7 9 6

6
compar
e
1 2 3 4 5 7 9 9

6
compar
e
belongs
here

1 2 3 4 5 7 7 9

6
compar
e
1 2 3 4 5 6 7 9
1 2 3 4 5 6 7 9

SORTED!
Insertion Sort Pseudocode
InsertionSort(A, n) {

for i = 1 to n-1 { //i is STEP, n is SIZE


key = A[i] // Select the current element to insert
j = i - 1; // Start comparing with the previous elements
while (j >= 0 and A[j] > key) {
A[j+1] = A[j] // Shift element one position to
the right
j = j - 1
}
A[j+1] = key // Place the key in its correct position
}
}
Complexity Analysis

Bubble Sort Selection Sort Insertion Sort

Best Case: Best/Average/Worst Best Case:


Occurs when the array is Case: Occurs when the array is
already sorted, requiring Always requires scanning already sorted, requiring
only one pass to verify the unsorted part of the no swaps and only N−1
order. array to find the minimum comparisons.
Worst/Average Case: element, irrespective of Average/Worst Case:
Involves nested loops, the initial order. Requires shifting
where every pair is elements to insert an
compared even if already element into the correct
sorted. position.
Sorting
Brainstorming Question

Apply different sorting algorithms (Bubble, Selection and


Insertion) to sort the below array. Show step by step simulation.

32 12 67 33 22 88 44 25

Also find the number of comparisons, shifting or swapping for


each case.
Reference
• Bubble Sort (With Code in Python/C++/Java/C)
• Selection Sort (With Code in Python/C++/Java/C)
• Insertion Sort (With Code in Python/C++/Java/C)

• https://www.cs.usfca.edu/~galles/visualization/ComparisonSort.html
• https://sortvisualizer.com/bubblesort/

Rumana Yasmin, Lecturer, CSE Dept,


BUP

You might also like