Chapter 10 Analysis of Sorting Algorithms
Chapter 10 Analysis of Sorting Algorithms
Steps
Check if the first element in the input array is greater than the
next element in the array.
If it is greater, swap the two elements; otherwise move the
pointer forward in the array.
Repeat Step 2 until we reach the end of the array.
Check if the elements are sorted; if not, repeat the same
process (Step 1 to Step 3) from the last element of the array to
the first.
The final output achieved is the sorted array.
Bubble Sort Algorithm
Pseudocode Memory Requirement
From the algorithm stated
above, it is clear that
bubble sort does not
require extra memory.
Bubble Sort Algorithm
Example
1 4 7
End result after first iteration
2 5
3 6
SAMPLE CODE INCLUDED
Insertion Sort Algorithm
Insertion sort is a very simple method to sort numbers in
an ascending or descending order.
This method follows the incremental method.
This is an in-place comparison-based sorting algorithm.
Here, a sub-list is maintained which is always sorted.
For example, the lower part of an array is maintained to
be sorted. An element which is to be 'inserted' in this
sorted sub-list, has to find its appropriate place and then
it has to be inserted there.
This algorithm is not suitable for large data sets
Insertion Sort Algorithm
Steps
If it is the first element, it is already sorted. return 1;
Pick next element
Compare with all elements in the sorted sub-list
Shift all the elements in the sorted sub-list that is
greater than the value to be sorted
Insert the value
Repeat until list is sorted
Insertion Sort Algorithm
Pseudocode
Insertion Sort Algorithm
Example
an unsorted array
For the first position in the sorted list, the whole list
is scanned sequentially. we search the whole list
and find that 10 is the lowest value.
3. The elements of each bucket are sorted using any of the stable
sorting algorithms.
4. The elements from each bucket are gathered.
Counting Sort Algorithm
Counting sort is a sorting algorithm that sorts the
elements of an array by counting the number of
occurrences of each unique element in the array.
The count is stored in an auxiliary (separate) array and
the sorting is done by mapping the count as an index of
the auxiliary array.
How Counting Sort Works
Find out the maximum element (let it
be max) from the given array.
4. Since,pivot is at right, so
algorithm starts from left and
moves to right.
How Quicksort works
5. Since a[pivot] > a[left], so algorithm moves
one position to right as –
10. Elements that are right side of element 24 are greater than
it, and the elements that are left side of element 24 are smaller
than it.