6_Algorithms on Data Structures - part 1
6_Algorithms on Data Structures - part 1
Key concepts
• Concept of Sorting [CO04]
• Parameters [CO04]
• Bubble sort - concept, algorithm and illustration [CO02, CO04]
• Complexity and Stability [CO02, CO03]
• enumeration sorting: If we know that there are N items
which are smaller than the one we are currently considering,
then its final position will be at number N + 1.
Sorting
• exchange sorting: If two items are found to be out of order,
`Sorting' usually refers to bringing a exchange them. Repeat till all items are in order.
set of items into some well-defined
order.
• selection sorting: Find the smallest item, put it in the first
Sorting is important because having position, find the smallest of the remaining items, put it in the
the items in order makes it much second position .
easier to find a given item.
• insertion sorting: Take the items one at a time and insert
Internal sorting - takes place in the them into an initially empty data structure such that the data
main memory, where we can take structure continues to be sorted at each stage.
advantage of the random access
nature of the main memory;
• divide and conquer: Recursively split the problem into
External sorting - necessary when smaller sub-problems till you just have single items that are
the number and size of objects are trivial to sort. Then put the sorted `parts' back together in a
prohibitive to be accommodated way that preserves the sorting.
in the main memory.
Illustration
The outer loop is carried out n-1 times. The inner loop is carried out (n-1) - (i - 1) = n - i
times.
The worst case and average case number of comparisons are both proportional to n2, and hence the
average and worst case time complexities are O(n2).
Stability : This is stable because no item is swapped past another unless they are in the wrong
order. So items with identical keys will have their original order preserved.