Algorithm & Data Structure I – AAU – Instructor: Albohtori Mohammed
Algorithm & Data Structure I
Simple Sorting Algorithms
Selection, Bubble and Insertion
Albohtori Mohammed Salih
Alzaiem Alazhari University
Department of Electrical Engineering (Computer Engineering)
5th Lecture, March 4, 2020
1
Algorithm & Data Structure I – AAU – Instructor: Albohtori Mohammed
Overview
The Sorting Problem
Bubble Sort
Example of Bubble sort
Pseudo Code for Bubble sort
Bubble sort Implementation
Analysis of Bubble sort
Selection Sort
Example of Selection sort
Pseudo Code for Selection sort
Selection sort Implementation
Insertion Sort
Example of Insertion sort
Insertion sort Implementation
Analysis of Insertion sort
Summary
2
Algorithm & Data Structure I – AAU – Instructor: Albohtori Mohammed
The Sorting Problem
Sorting refers to arranging data in a particular format.
Sorting algorithm specifies the way to arrange data in a
particular order. Most common orders are in numerical or
alphabetical order.
Sorting is also used to represent data in more readable
formats. Following are some of the examples of sorting in
real-life scenarios:
Telephone Directory: The telephone directory stores
the telephone numbers of people sorted by their names,
so that the names can be searched easily.
Dictionary: The dictionary stores words in an
alphabetical order so that searching of any word
becomes easy.
3
Algorithm & Data Structure I – AAU – Instructor: Albohtori Mohammed
The Sorting Problem
The objective of the sorting problem: to
rearrange a given sequence of items.
It is one of the fundamental problems in computer
science, can be solved in many ways:
There are many sorting algorithms
Some are faster/slower than others
Some use more/less memory than others
Some work better with specific kinds of data
Some can utilize multiple computers / processors, ...
4
Algorithm & Data Structure I – AAU – Instructor: Albohtori Mohammed
The Sorting Problem
There are many sorting algorithms:
Bubble sort: swap adjacent pairs that are out of order.
Selection sort: look for the smallest element, move to front.
Insertion sort: build an increasingly large sorted front portion.
Shell sort: This algorithm is a simple extension of Insertion sort.
Heap sort: place the values into a sorted tree structure.
Merge sort: recursively divide the array in half and sort it.
Quick sort: recursively partition array based on a middle value.
5
Algorithm & Data Structure I – AAU – Instructor: Albohtori Mohammed
In-place Sorting and Not-in-place Sorting
Sorting algorithms may require some extra space for
comparison and temporary storage of few data
elements. These algorithms do not require any extra
space and sorting is said to happen in-place, or for
example, within the array itself. This is called in-place
sorting. Bubble sort is an example of in-place sorting.
However, in some sorting algorithms, the program
requires space which is more than or equal to the
elements being sorted. Sorting which uses equal or
more space is called not-in-place sorting. Merge-sort
is an example of not-in-place sorting.
6
Algorithm & Data Structure I – AAU – Instructor: Albohtori Mohammed
Stable and Not Stable Sorting
If a sorting algorithm, after sorting the contents,
does not change the sequence of similar content in
which they appear, it is called stable sorting.
7
Algorithm & Data Structure I – AAU – Instructor: Albohtori Mohammed
Stable and Not Stable Sorting
If a sorting algorithm, after sorting the contents,
changes the sequence of similar content in which
they appear, it is called unstable sorting.
Stability of an algorithm matters when we wish to maintain
the sequence of original elements, like in a tuple for
example.
8
Algorithm & Data Structure I – AAU – Instructor: Albohtori Mohammed
The Sorting Problem
To define an instance of a sorting problem, we must
specify:
The type of the sequence
The number of items to be sorted
The ordering relationship.
Consider the following instance of the sorting
problem: an array of integers A, containing N items
is to be sorted in ascending order.
We will review three algorithms for sorting known as
elementary sorts:
The Bubble sort algorithm.
The Selection sort algorithm.
The Insertion sort algorithm. 9
Algorithm & Data Structure I – AAU – Instructor: Albohtori Mohammed
Bubble Sort
Bubble sort is a simple sorting algorithm. This
sorting algorithm is comparison-based algorithm in
which each pair of adjacent elements is compared
and the elements are swapped if they are not in
order.
This algorithm is not suitable for large data sets as
its average and worst case complexity are of Ο(n2)
where n is the number of items.
10
Algorithm & Data Structure I – AAU – Instructor: Albohtori Mohammed
Bubble Sort
Compare each element (except the last one) with its
neighbor to the right
If they are out of order, swap them
This puts the largest element at the very end
The last element is now in the correct and final place
Compare each element (except the last two) with its
neighbor to the right
If they are out of order, swap them
This puts the second largest element next to last
The last two elements are now in their correct and final places
Compare each element (except the last three) with its
neighbor to the right
Continue as above until you have no unsorted elements on the left
11
Algorithm & Data Structure I – AAU – Instructor: Albohtori Mohammed
Example of Bubble sort
7 2 8 5 4 2 7 5 4 8 2 5 4 7 8 2 4 5 7 8
2 7 8 5 4 2 7 5 4 8 2 5 4 7 8 2 4 5 7 8
2 7 8 5 4 2 5 7 4 8 2 4 5 7 8 (done)
2 7 5 8 4 2 5 4 7 8
2 7 5 4 8
12
Algorithm & Data Structure I – AAU – Instructor: Albohtori Mohammed
Pseudo Code for Bubble sort
Algorithm BubbleSort(Data[0 .. N-1])
//Purpose: Sort a given array by Bubble sorting algorithm
//Input: An array Data[0 .. N-1]
//Output: Array Data [0..N-1] values, sorted in ascending order
Begin
For (i=N-1 to 0) Do
For (j=0 to i-1) Do
If (Data [j] > Data [j+1]) then
Swap (Data [j] with Data [j+1])
Endif
EndFor
EndFor
Return Data[0... N-1]
EndAlgorithm
13
Algorithm & Data Structure I – AAU – Instructor: Albohtori Mohammed
Bubble sort Implementation
public class BubbleSort {
public static int[] bubbleSort(int[] a) {
int outer, inner;
for (outer = a.length - 1; outer > 0; outer--) { // counting down
for (inner = 0; inner < outer; inner++) { // bubbling up
if (a[inner] > a[inner + 1]) { // if out of order...
int temp = a[inner]; // ...then swap
a[inner] = a[inner + 1];
a[inner + 1] = temp; } } }
return a;
}
public static void main(String[] args) {
int[] myArray = { 6,2,8,4,12,77,32,28,16};
int[] sorted = bubbleSort(myArray);
System.out.println(“Array after Sorted : ");
for (int i = 0; i < sorted.length; i++) {
System.out.print(sorted[i] + ", ");
}
}} 14
Algorithm & Data Structure I – AAU – Instructor: Albohtori Mohammed
Analysis of Bubble sort
for (outer = a.length - 1; outer > 0; outer--) {
for (inner = 0; inner < outer; inner++) {
if (a[inner] > a[inner + 1]) {
// code for swap omitted
} } }
Let n = a.length = size of the array
The outer loop is executed n-1 times (call it n, that’s close
enough)
Each time the outer loop is executed, the inner loop is
executed
Inner loop executes n-1 times at first, linearly dropping to just once
On average, inner loop executes about n/2 times for each
execution of the outer loop
In the inner loop, the comparison is always done (constant time),
the swap might be done (also constant time)
Result is n * n/2 * k, that is, O(n2/2 + k) = O(n2) 15
Algorithm & Data Structure I – AAU – Instructor: Albohtori Mohammed
Selection Sort
Selection sort is a simple sorting algorithm. This
sorting algorithm is an in-place comparison-based
algorithm
The smallest element is selected from the unsorted
array and swapped with the leftmost element, and
that element becomes a part of the sorted array.
This process continues moving unsorted array
boundary by one element to the right.
This algorithm is not suitable for large data sets as
its average and worst case complexities are of
Ο(n2), where n is the number of items.
16
Algorithm & Data Structure I – AAU – Instructor: Albohtori Mohammed
Selection Sort
Given an array of length n,
Search elements 0 through n-1 and select the smallest
Swap it with the element in location 0
Search elements 1 through n-1 and select the smallest
Swap it with the element in location 1
Search elements 2 through n-1 and select the smallest
Swap it with the element in location 2
Search elements 3 through n-1 and select the smallest
Swap it with the element in location 3
Continue in this fashion until there’s nothing left to
search.
17
Algorithm & Data Structure I – AAU – Instructor: Albohtori Mohammed
Example of Selection sort
7 2 8 5 4
2 7 8 5 4
2 4 8 5 7
2 4 5 8 7
2 4 5 7 8
18
Algorithm & Data Structure I – AAU – Instructor: Albohtori Mohammed
Pseudo Code for Selection sort
Algorithm SelectionSort(Data[0 .. N-1])
//Purpose: Sort a given array by Selection sorting algorithm
//Input: An array Data[0 .. N-1]
//Output: Array Data [0..N-1] values, sorted in ascending order
Begin
For (i=0 to N-1) Do
Min = i
For (j=i+1 to N-1) Do
If (Data [j] < Data [min]) then
Min = j
Endif
EndFor
Swap (Data [i] with Data [min])
EndFor
Return Data[0... N-1]
EndAlgorithm
19
Algorithm & Data Structure I – AAU – Instructor: Albohtori Mohammed
Selection sort Implementation
public static int[] swap(int[] a, int x, int y) {
int temp = a[x];
a[x] = a[y];
a[y] = temp;
return a;
}
public static int[] selectionSort(int[] a) {
int outer, inner, min;
for (outer = 0; outer < a.length; outer++){
min = outer;
for (inner = outer+1; inner < a.length; inner++){
if (a[inner]< a[min]){
min = inner;
}
}
a = swap(a, outer, min);
}
return a;
} 20
Algorithm & Data Structure I – AAU – Instructor: Albohtori Mohammed
Insertion Sort
This is an in-place comparison-based sorting
algorithm.
The array is partitioned into two parts, sorted (on
the left) and unsorted (on the right).
Initially the unsorted part has one element. In each
pass, the first element of the unsorted part is
inserted in the appropriate location in the sorted left
block.
This algorithm is not suitable for large data sets as
its average and worst case complexity are of Ο(n2),
where n is the number of items.
21
Algorithm & Data Structure I – AAU – Instructor: Albohtori Mohammed
Example of Insertion sort
sorted next to be inserted
3 4 7 12 14 15 20 21 33 38 10 55 9 23 28 16
temp
less than 10
10
3 4 7 10 12 14 15 20 21 33 38 55 9 23 28 16
sorted
22
Algorithm & Data Structure I – AAU – Instructor: Albohtori Mohammed
Insertion sort Implementation
public static int[] insertionSort(int[] array, int first, int last) {
int temp, k;
for (int i = first; i < last; i++) {
temp = array[i];
k = i;
while ((k > 0) && (array[k - 1] > temp)) {
array[k] = array[k - 1];
k--;
}
array[k] = temp;
}
return array;
}
23
Algorithm & Data Structure I – AAU – Instructor: Albohtori Mohammed
Analysis of Insertion sort
We run once through the outer loop, inserting each
of n elements; this is a factor of n
On average, there are n/2 elements already sorted
The inner loop looks at (and moves) half of these
This gives a second factor of n/4
Hence, the time required for an insertion sort of an
array of n elements is proportional to n2/4
Discarding constants, we find that insertion sort is
O(n2)
24
Algorithm & Data Structure I – AAU – Instructor: Albohtori Mohammed
Summary
Most of the sorting techniques we have discussed are
O(n2)
We can do much better than this with somewhat more
complicated sorting algorithms
Within O(n2),
Bubble sort is very slow, and should probably never be used for
anything
Selection sort is intermediate in speed
Insertion sort is usually faster than selection sort. In fact, for small
arrays (say, 10 or 20 elements), insertion sort is faster than more
complicated sorting algorithms
Selection sort and insertion sort are “good enough” for small
arrays
25
Algorithm & Data Structure I – AAU – Instructor: Albohtori Mohammed
Exercise
Rewrite the bubble sort, selection sort and insertion
sort algorithms for String data type
26