Escolar Documentos
Profissional Documentos
Cultura Documentos
2
Types of Sorting
• In-place Sorting
• Internal Sorting
• External Sorting
3
In-place Sorting
• A sorting algorithm is said to be in-place if it requires very little
additional space besides the initial array holding the elements that
are to be sorted
• “Very little” is taken to mean that for sorting n elements, O(log n)
extra space is required
• This is reasonable because in a purely mathematical analysis of the
algorithms, any sorting algorithm that operates on a contiguous array
requires O(log n) extra space, since this is the number of bytes
required to represent an index into the array
4
Cont’d
• Examples:
• Bubble Sort, Comb Sort, Selection Sort, Insertion Sort, Heap Sort, Shell Sort
and Quick Sort
5
Internal Sorting
• An internal sort is any data sorting process that takes place entirely
within the main memory of a computer
• This is possible whenever the data to be sorted is small enough and
can be held in the main memory
• For sorting larger datasets, it may be necessary to hold only a chunk
of data in memory at a time, since it won’t all fit
• The rest of the data is normally held on some larger, but slower
medium, like a hard-disk
6
Cont’d
• Examples:
• Bubble Sort, Comb Sort, Selection Sort, Insertion Sort, Heap Sort, Shell Sort,
Redix Sort and Quick Sort
7
External Sorting
• External sorting is a class of sorting algorithms that can handle
massive amounts of data
• External sorting is required when the data being sorted do not fit into
the main memory and instead they must reside in the slower external
memory (usually a hard drive)
• External sorting typically uses a hybrid sort-merge strategy
• In the sorting phase, chunks of data small enough to fit in main
memory are read, sorted, and then written to a temporary file
• In the merge phase, the sorted sub-files are combined into a single
larger file
8
Cont’d
• Examples:
• K-way External Merge Sort, Distributions Sort
9
Stable vs. Unstable Sorting
• A sorting algorithm is said to be stable if two objects with equal keys
appear in the same order in sorted output as they appear in the input
unsorted array
• Some sorting algorithms are stable by nature like Insertion sort, Merge
Sort, Bubble Sort, etc. And some sorting algorithms are not, like Heap Sort,
Quick Sort, etc.
• However, any given sorting algo which is not stable can be modified to be
stable
• There can be sorting algo specific ways to make it stable, but in general,
any comparison based sorting algorithm which is not stable by nature can
be modified to be stable by changing the key comparison operation so that
the comparison of two keys considers position as a factor for objects with
equal keys
10
The Insertion Sort
• Insertion Sort is a simple sorting algorithm: a comparison sort in which the
sorted array (or list) is built one entry at a time
• It is much less efficient on large lists than more advanced algorithms such
as quicksort, heapsort, or merge sort
• However, insertion sort provides several advantages
• Simple implementation
• Efficient for (quite) small data sets
• More efficient in practice than most other simple quadratic, i.e. O(n2) algorithms
such as selection sort or bubble sort; the best case (nearly sorted input) is O(n)
• Stable, i.e. does not change the relative order of elements with equal keys
• In-place, i.e. only requires a constant amount O(1) of additional memory space
11
Working of Insertion Sort
12
13
Algorithm
1. for j ←2 to length(A)
2. key ← A[ j ]
3. > A[ j ] is added in the sorted sequence A[1, .. j-1]
4. i←j-1
5. while i > 0 and A [ i ] > key
6. A[ i +1 ] ← A[ i ]
7. i ← i -1
8. A [i +1] ← key
14
Cont’d
15
Analysis of Insertion Sort
16
Cont’d
17
Loop Invariant
• Invariant: a function, quantity, or property which remains unchanged
when a specified transformation is applied
• Loop Invariant: a property of loop that is true before and after each
iteration of the loop, even after last iteration i.e., unchanged
• Helps us understand why an algorithm is correct
• Has three important things:
• Initialization: It is true prior to the first iteration of the loop
• Maintenance: If it is true before first iteration, it remains true before next
iteration
• Termination: When the loop terminates, the invariant gives us a useful
property that helps us show that the algorithm is correct
18
Loop Invariant and Correctness of Insertion
Sort
for j 2 to length(A)
19
Loop Invariant and Correctness of Insertion
Sort – Cont’d
for j 2 to length(A)
20
Loop Invariant and Correctness of Insertion
Sort – Cont’d
for j 2 to length(A)
21
Best Case Analysis
• The array is already sorted
22
Worst Case Analysis
23
Cont’d
25