Sorting UNIT 5
Sorting UNIT 5
❖ Algorithms:
✓Step 1: Repeat Step 2 For i = 0 to N-1
✓Step 2: Repeat For J = i + 1 to N - I
✓Step 3: IF A[J] > A[i]
Swap A[J] and A[i]
[End of inner loop]
[end of outer loop]
✓Step 4: Exit
Working of Bubble Sort
❖1.Best-case: O(n)
✓ Array is already sorted in ascending order
❖2.Worst-case: O(n2)
✓ Worst case occurs when array is reverse sorted.
❖3.Average-case: O(n2)
✓ We have to look at all possible initial data organizations
Selection Sort
❖ We find the smallest element from the unsorted sub list and
swap it with the element at the beginning of the unsorted data.
❖After each selection and swapping, the imaginary wall between
the two sub lists move one element ahead, increasing the number
of sorted elements and decreasing the number of unsorted ones
❖Each time we move one element from the unsorted sub list to
the sorted sub list, we say that we have completed a sort pass.
❖ A list of n elements requires n-1 passes to completely rearrange
the data.
Algorithms of Selection Sort
❖Algorithms
SELECTION SORT(A[], N)
✓Step 1: Repeat Steps 2 and 3 for i = 1 to N-1
✓Step 2: Call Smallest (A[], i, N, pos)
✓Step 3:SwapA[i] with A[pos]
[End of Loop]
✓Step 4: Exit
✓Smallest (A[], i, N, POS)
✓Step 1: [Initialize] set Small= A[i]
✓Step 2: [Initialize] Set pos = i
✓Step 3: Repeat for J = i+1 to N -1
if small > A[J]
Set small = A[J]
Set pos = J
[End of if ]
[End of Loop]
✓Step 4: Return pos
Working of Selection Sort
❖In each pass, the first element of the unsorted part is picked up, transferred
to the sorted sub list, and inserted at the appropriate place.
❖A list of n elements will take at most n-1 passes to sort the data.
Algorithms of Insertion Sort
❖Algorithms
✓Step 1: Repeat Steps 2 to 5 for i = 1 to N-1
✓Step 2: Set Temp = A[i]
✓Step 3: Set J = i - 1
✓Step 4: Repeat while Temp <=A[J]
Set A[J + 1] = A[J]
Set J = J - 1
[End of inner Loop]
✓Step 5: Set A[J + 1] = Temp
[End of loop]
✓Step 6: Exit
Working of Insertion Sort
Insertion Sort – Analysis
❖Time Complexity
❖Best-case:➔ O(n)
❖Worst-case: ➔ O(n2)
❖Average-case: ➔ O(n2)
Merge Sort
Time Complexity
❖Best Case : Θ(nlog2n)
❖Average Case: Θ(nlog2n)
❖Worst Case : O(n2)
Heap Sort
– Look-Up Table
– Dictionary
– Cache
– Extended Array
Introduction
Example: A small phone book as a hash table.
Hash Function
• It is the mathematical function which takes unique key as an
argument and return a unique memory location which is used to
store and retrieve the data related to key.
• Ex. m = H(key)
– Where, m is memory location
– If the key is 3205 and if we are using mid-square method as hash function them
m=72. Which means, we are 72th location of memory to store and retrieve data
related to key 3205.
Hash Function
• Some of the hash functions are:
– Mid-Square method
– Division method
– Folding method
Hash Function - Mid-Square
• In this method, first we square the given key, then we
find out the mid of this squared key by first putting left &
then right, moving to mid.
• Ex 2: key = 2209
– Step 1: folding -> 22 and 09
– Step 2: adding -> 31 is the required m
Hash Function - Folding : Collision
• Ex 3: key = 3205
– Step 1: folding -> 32 and 05
– Step 2: adding -> 37 is the required m
• Ex 4: key = 3106
– Step 1: folding -> 31 and 06
– Step 2: adding -> 37 is the required m
1. Probing
a) Linear
b) Quadratic
2. Rehashing
3. Chaining
Probing - Linear
• In linear probing, we linearly probe for next slot.
• We find next empty location as:
– M = (p + i) % SIZE
– Ex. Let us consider a simple hash function as “key mod 7” and sequence of
keys as 50, 700, 76, 85, 92, 73, 101.
Probing - Linear
Probing - Quadratic
• In this method, we find next empty location as:
– M = (p + i^2) % SIZE