Professional Documents
Culture Documents
Lecture 2: Sorting Techniques
Lecture 2: Sorting Techniques
Lecture 2: Sorting Techniques
Techniques
Sorting
• The general idea of the bubble sort is that each element is compared
to all the elements in the data.
• The bubble sort algorithm requires N-1 passes to sort N items of
data.
• The bubble sort is notoriously slow, but it is conceptually the simplest
of the sorting algorithms.
1. The result of the first pass is that the largest item is in the last location
of the array.
2. The result of the second pass is that the second largest item is in the
second last location of the array.
3. etc.
4. After N passes, the entire array is sorted.
Bubble Sort
• Worst Case:
The worst case occurs when we have to search through the entire array of
data and the item does not appear in data. In this case the algorithm requires
f(n) = n-1 comparisons.
• Average Case:
The average number of comparisons required to find the location item is
approximately equal to half number of elements in the array that is :
f(n) = (n-1)/2 comparisons.
Complexity of Bubble Sort
• The general idea of the selection sort is that for each slot, find the
element that belongs there.
• The selection sort improves on the bubble sort by reducing the
number of swaps.
• The number of comparisons remains the same that is to sort N items
you make N-1 passes through them.
Selection Sort
Pass 1: 77,33,44,11,88,22,66,55
Pass 2: 11,33,44,77,88,22,66,55
Pass 3: 11,22,44,77,88,33,66,55
Pass 4: 11,22,33,77,88,44,66,55
Pass 5: 11,22,33,44,88,77,66,55
Pass 6: 11,22,33,44,55,77,66,88
Pass 7: 11,22,33,44,55,66,77,88
Selection Sort Algorithm
O(n2) O(n2)
Insertion Sort
• The general idea of the insertion sort method is that for each
element, find the slot where it belongs.
• This sorting algorithm is frequently used when N is small.
• Suppose A is an array with N elements.
• The insertion sort scans A from A[1] to A[N] inserting each element
A[k] into its proper position
Insertion Sort
O(n2) O(n2)
Merging
•Merge.
• Keep track of smallest element in each sorted half.
• Insert smallest of two elements into auxiliary array.
• Repeat until done.
smallest smallest
A G L O R H I M S T
A auxiliary array
Merging
•Merge.
• Keep track of smallest element in each sorted half.
• Insert smallest of two elements into auxiliary array.
• Repeat until done.
smallest smallest
A G L O R H I M S T
A G auxiliary array
Merging
•Merge.
• Keep track of smallest element in each sorted half.
• Insert smallest of two elements into auxiliary array.
• Repeat until done.
smallest smallest
A G L O R H I M S T
A G H auxiliary array
Merging
•Merge.
• Keep track of smallest element in each sorted half.
• Insert smallest of two elements into auxiliary array.
• Repeat until done.
smallest smallest
A G L O R H I M S T
A G H I auxiliary array
Merging
•Merge.
• Keep track of smallest element in each sorted half.
• Insert smallest of two elements into auxiliary array.
• Repeat until done.
smallest smallest
A G L O R H I M S T
A G H I L auxiliary array
Merging
•Merge.
• Keep track of smallest element in each sorted half.
• Insert smallest of two elements into auxiliary array.
• Repeat until done.
smallest smallest
A G L O R H I M S T
A G H I L M auxiliary array
Merging
•Merge.
• Keep track of smallest element in each sorted half.
• Insert smallest of two elements into auxiliary array.
• Repeat until done.
smallest smallest
A G L O R H I M S T
A G H I L M O auxiliary array
Merging
•Merge.
• Keep track of smallest element in each sorted half.
• Insert smallest of two elements into auxiliary array.
• Repeat until done.
smallest smallest
A G L O R H I M S T
A G H I L M O R auxiliary array
Merging
•Merge.
• Keep track of smallest element in each sorted half.
• Insert smallest of two elements into auxiliary array.
• Repeat until done.
first half
exhausted smallest
A G L O R H I M S T
A G H I L M O R S auxiliary array
Mergesort
•Mergesort (divide-and-conquer)
• Divide array into two halves.
A L G O R I T H M S
A L G O R I T H M S divide
Mergesort
•Mergesort (divide-and-conquer)
• Divide array into two halves.
• Recursively sort each half.
A L G O R I T H M S
A L G O R I T H M S divide
A G L O R H I M S T sort
Mergesort
•Mergesort (divide-and-conquer)
• Divide array into two halves.
• Recursively sort each half.
• Merge two halves to make sorted whole.
A L G O R I T H M S
A L G O R I T H M S divide
A G L O R H I M S T sort
A G H I L M O R S T merge
Merge Sort Algorithm
Mergesort(A)
{
for( i=1;i<size,i=2i)
{
for( j=0,j<size-i;j=j+2i)
Merge(&A[j], i, min(2i,size-j))
}
}
Merge Function
Merge(A, end1,end2)
{
i=0, j=end1,k=0
{
while i<end1 and j<end2
{ if(a[i]<A[j])
temp[k]=A[i]
i=i+1,k=K+1
else
temp[k]=A[j]
J=j+1, k=k+1 }
}
while i<end1
temp[k]=A[i]
i=i+1, k=k+1
while i<end2
temp[k]=A[i]
j=j+1, k=k+1
For(i=0;i<end2.i++)
A[i]=temp[i]