Professional Documents
Culture Documents
Tazeen Muzammil
MSSE
Sorting
• 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.
Selection 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
• The operation in each pass is as follows:
1. First find the smallest element in the list and put it
in the first position.
2. On the second pass you scan through just the first
N-1 entries. Then find the second smallest element
in the list and put it in the second position.
3. This process is repeated, with one item being
placed in its correct location each time.
4. After N-1 passes, the entire collection of data is
sorted.
Example
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
• This procedure finds the location LOC of the
smallest element.
MIN (A,K, N,LOC)
1. [Initialize pointer]Set MIN=A[K] and LOC=K
2. Repeat for J=K+1 …..N
If MIN>A[J] then
Set MIN= A[J] LOC= A[J] and LOC=J
End of loop
3. Return
Selection Sort Algorithm
• This algorithm sorts the array A with N elements.
SELECTION (A,N)
1. Repeat Step 2and 3 for K=1 …..N-1
2. Call MIN(A,K,N,LOC)
3. [Interchange A[K] and A[LOC]
Set TEMP=A[K] , A[K]= A[LOC] and A[LOC] =TEMP
4. [End of Step 1 loop]
5. Exit
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
• The operation in each pass is as follows:
1. A[1 ] by itself is sorted.
2. A[2] is inserted either before A[1] or after A[1] so
that A[1] and A[2] are sorted.
3. A[N] is inserted into its proper place in A[1],A[2]
…A[N-1] so that A[1],A[2]…A[N] is sorted.
Example
• Suppose the following numbers are stored in an array A:
77,33,44,11,88,22,66,55
Pass 1:77,33,44,11,88,22,66,55
Pass 2:77,33,44,11,88,22,66,55
Pass 3:33,77,44,11,88,22,66,55
Pass 4:33,44,77,11,88,22,66,55
Pass 5:11,33,44,77,88,22,66,55
Pass 6:11,22,33,44,77,88,66,55
Pass 7:11,22,33,44,66,77,88,55
Pass 8:11,22,33,44,55,66,77,88
Insertion Sort Algorithm
This algorithm sorts the array A with N elements.
(Insertion Sort) INSERTION(A,N)
1. Set A[0]:=0.
2. Repeat Steps 3 to 5 for K = 2, 3,……,N:
3. Set TEMP := A[K] and PTR:= K – 1
4. Repeat while TEMP <A[PTR]:
(a) Set A[PTR + 1] :=A[PTR].[moves elements forward.]
(b) Set PTR := PTR – 1
[End of loop.
5. Set A[PTR + 1]:=TEMP.{Inserts element in proper place.]
[End of Step 2 loop.]
6. Return.
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.
first half
– Repeat until done.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 (divide-and-conquer)
Mergesort
– 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