Lecture Notes 2 On Analysis and Complexity of Algorithms
Lecture Notes 2 On Analysis and Complexity of Algorithms
LECTURE NOTES
BY
CHARLES OKONJI
Sorting Algorithm
A Sorting Algorithm is used to rearrange a given array or list of elements according to a comparison operator on the elements. The
comparison operator is used to decide the new order of elements in the respective data structure. For example, the list of characters
below is sorted in increasing order of their ASCII values, such that the character with a lesser ASCII value will be placed first than the
character with a higher ASCII value.
Selection Sort
This simple and efficient sorting algorithm works by repeatedly selecting the smallest (or largest) element from the unsorted portion of the
list and moving it to the sorted portion of the list.
Let us illustrate using, as an example, the array arr[] = {64, 25, 12, 22, 11}
First pass:
For the first position in the sorted array, the whole array is traversed from index 0 to 4 sequentially. The first position where 64 is
stored presently, after traversing whole array it is clear that 11 is the lowest value.
Thus, replace 64 with 11. After one iteration 11, which happens to be the least value in the array, tends to appear in the first position
of the sorted list.
Sorting Algorithm
Second Pass:
For the second position, where 25 is present, again traverse the rest of the array in a sequential manner.
After traversing, we found that 12 is the second lowest value in the array and it should appear at the second place in the array, thus
swap these values.
Third Pass:
Now, for third place, where 25 is present again traverse the rest of the array and find the third least value present in the array.
While traversing, 22 came out to be the third least value and it should appear at the third place in the array, thus swap 22 with
element present at third position.
Fourth pass:
Similarly, for fourth position traverse the rest of the array and find the fourth least element in the array
As 25 is the 4th lowest value hence, it will place at the fourth position.
Fifth Pass:
At last the largest value present in the array automatically get placed at the last position in the array
The resulted array is the sorted array.
Sorting Algorithm
procedure selection sort
list : array of items
n : size of list
for i = 1 to n - 1
/* set current element as minimum*/
min = i
/* check the element to be minimum */
for j = i+1 to n
if list[j] < list[min] then
min = j;
end if
end for
/* swap the minimum element with the current element*/
if indexMin != i then
swap list[min] and list[i]
end if
end for
end procedure
Sorting Algorithm
SELECTION SORT(arr, n)
// selection sort
#include <bits/stdc++.h>
int i, j, min_idx;
// unsorted subarray
// unsorted array
min_idx = i;
min_idx = j;
}
Sorting Algorithm
// Swap the found minimum element
if (min_idx != i)
swap(arr[min_idx], arr[i]);
int i;
// Driver program
int main()
{
Sorting Algorithm
int arr[] = { 64, 25, 12, 22, 11 };
// Function Call
selectionSort(arr, n);
printArray(arr, n);
return 0;
Output
Sorted array:
11 12 22 25 64
Bubble Sort
This algorithm works by repeatedly swapping the adjacent elements if they are in the wrong order. This algorithm is not suitable for large
data sets as its average and worst-case time complexity is quite high.
• In this algorithm,
o traverse from left and compare adjacent elements and the higher one is placed at right side.
o In this way, the largest element is moved to the rightmost end at first.
Let us illustrate using an array arr[] = {14, 33, 27, 35, 10}
Sorting Algorithm
Bubble sort starts with very first two elements, comparing them to check which one is greater. In this case, value 33 is greater than 14, so
it is already in sorted locations.
Next, we compare 33 with 27. We find that 27 is smaller than 33 and these two values must be swapped. The new array should look like
this −
Next we compare 33 and 35. We find that both are in already sorted positions.
Sorting Algorithm
Then we move to the next two values, 35 and 10. We know then that 10 is smaller 35. Hence they are not sorted, so we swap these
values.
Thus, after one iteration, the array should look like this −
Notice that after each iteration, at least one value moves at the end.
And when there's no swap required, bubble sorts learns that an array is completely sorted.
begin BubbleSort(arr)
for all array elements
if arr[i] > arr[i+1]
swap(arr[i], arr[i+1])
end if
end for
return arr
end BubbleSort
Sorting Algorithm
for (j = 0; j < n - i - 1; j++) {
if (arr[j] > arr[j + 1]) {
swap(&arr[j], &arr[j + 1]);
swapped = true;
}
}
Output
Sorted array: 10, 14, 27, 33, 35