4. Sorting
4. Sorting
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:
• 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
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 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
⮚ Underlined pairs show the comparisons. For each pass there are
size-1 comparisons.
⮚ Total number of comparisons = (size-1)2
Bubble Sort
⮚ 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 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
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.
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
9 2 7 5 1 4 3 6
9 2 7 5 1 4 3 6
2
compar
e
9 9 7 5 1 4 3 6
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
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) {
32 12 67 33 22 88 44 25
• https://www.cs.usfca.edu/~galles/visualization/ComparisonSort.html
• https://sortvisualizer.com/bubblesort/