3. Bubble Sort
It sort the array elements by repeatedly moving
the largest element to the highest index position
of the array[for ascending order].
Consecutive adjacent pair of elements in the
array are compared with each other.
Two elements are interchanged if lower index
value is greater than upper index value.
Process is continued till list of unsorted elements
exhausted.
It is called bubble sort because larger elements
‘bubble’ to the end of the list.
GLA University - CSE 4070
4. Algorithm
Bubble_sort(a[ ], n)
{
Repeat for i = 0 to n-1
Repeat for j = 0 to n-i-2
if(a[j]>a[j+1])
swap a[j] and a[j+1]
end if
end for
end for
}
GLA University - CSE 4070
5. Bubble Sort : 1st
pass
30 < 52 no swapping
52 >25 swap 52 and 25
52 < 84 no swapping
84 > 65 swap 84 and 65
Max value 84 moved to
last index
GLA University - CSE 4070
6. 2nd
pass
30 > 25 swap 30 and 25
30 < 52 no swapping
52 < 65 no swapping
2nd
Max value 65 moved
to 2nd
last index
GLA University - CSE 4070
7. Insertion Sort
Algorithm works by moving the current
data element past the already sorted
values and repeatedly interchanging it
with preceding value until it is it’s correct
place.
less efficient when compared with other
algorithms such as quick sort, heap sort
and merge sort.
GLA University - CSE 4070
8. Algorithm
Insertion_sort(a[ ], n)
{
Repeat for i = 0 to n-1
set temp = a[i]
set j=i-1
Repeat while temp< a[j] and j>=0
set a[j+1]= a[j]
set j=j-1
end while
set a[j+1] = temp
end for
}
GLA University - CSE 4070
10. Selection Sort
It takes n-1 passes to sort the entire
array and works as follows:
◦ Find smallest value in the array and place it in
it’s position.
◦ Repeat this process untill array is sorted.
GLA University - CSE 4070
11. Algorithm
Selection_sort(a[ ], n)
{
Repeat for i = 0 to n-1
Repeat for j = i+1 to n-1
if(a[i]>a[j])
swap a[i] and a[j]
end if
end for
end for
}
GLA University - CSE 4070
14. Quick Sort
Quick sort works s follows:
◦ Select a pivot element from the array
elements.
◦ Rearrange the array elements int the array
such that all the elements less than the pivot
element appears before the pivot and all the
elements greater than the pivot element
comes after the pivot.(this process is called
partition)
◦ Recursively sort two sub arrays thus
obtained.
GLA University - CSE 4070
16. Partition algorith
partition (arr[], first, last)
{
set pivot = first, i = first , j=last
Repeat while i < j
Repeat while arr[i] < arr[pivot]
set i= i+1
end while
Repeat while arr[j] > arr[pivot]
set j= j-1
end while
if i<j
swap arr[i] and arr[j]
end if
End while
Swap arr[pivot] and arr[j]
Return pivot }
GLA University - CSE 4070
18. quickSort(arr,0,5)
first moves to the right
until
Array element > pivot
element
last moves to the left
until
Array element < pivot
element
swap arr[first] and arr[last]
pivot=6
partition(arr,0,5)
GLA University - CSE 4070
19. quickSort(arr,0,5)
repeat right/left scan
UNTIL left & right cross
first & last CROSS!!!
Swap pivot and arr[last]
Return new
location of pivot
to quicksort
function
= return 3
GLA University - CSE 4070
21. Recursive calls to quickSort()
first & llast CROSS!!!
1 - Swap pivot element and arr[last]
2 - Return new location of pivot element
to quicksort function
Return 0 GLA University - CSE 4070
22. Recursive calls to quickSort()
first & llast CROSS!!!
1 - Swap pivot element and arr[last]
2 - Return new location of pivot element
to quicksort function
Return 0 GLA University - CSE 4070
24. Mergesort
It focuses on divide, conquer and
combine
Divide means partitioning the array to be
sorted into two equal sub-arrays
Conquer means sorting two sub-array
recursively
Combine means merge two sorted sub-
arrays.
GLA University - CSE 4070
25. Algorithm
MergeSort(arr[], l, r)
{ if r>l
set mid = (l+r)/2
Call mergeSort(arr, l, mid)
Call mergeSort(arr, mid+1, r)
Call merge(arr, l, m, r)
end if
}
GLA University - CSE 4070
27. Algorithm
Merge(arr[], l, mid, r)
{ compute n1 and n2
copy n1 elements in left[]
copy n2 elements in right[]
set left[n1]= 9999, rightt[n1]= 9999
set i=0,j=0,k=0
for k = l to r
if left[i]<right[j]
set arr[k]= left[i]
i=i+1
else arr[k]=right[j]
j=j+1
end if
end for
}
GLA University - CSE 4070
32. Heap
A heap is a data structure that stores a
collection of keys , and has the following
properties:
◦ Complete Binary tree
◦ Satisfies Heap property which states that
child key value >= parent key value for min - heap
child key value <= parent key value for max - heap
Each node of the binary tree corresponds to an
element of the array. The array is completely
filled on all levels except possibly lowest.
GLA University - CSE 4070
33. Heap
Max-heap :
A[Parent(i)] ≥ A[i]
Min-heap : A[Parent(i)] ≤ A[i]
PARENT (i) where i is index of any key
return floor(i/2)
LEFT (i)
return 2i
RIGHT (i)
return 2i + 1
GLA University - CSE 4070
34. Insertion in heap
1. Add the new element to the next available position at the
lowest level
2. Restore the max-heap property if violated
Let the new rise to its appropriate place, if the
parent of the element is smaller than the
element, then interchange the parent and child.
OR
Restore the min-heap property if violated
Sink down the new element, if the parent of the
element is larger than the element, then
interchange the parent and child.
GLA University - CSE 4070
35. Insert 17 in max-heap
maintain the heap property
GLA University - CSE 4070
36. Delete from heap
Delete max
◦ Replace the last element with root value (delete
last element).
◦ Restore the max heap property by sinking down
the new root value.
Delete min
◦ Replace the last element with root value (delete
last element).
◦ Restore the min heap property by sinking down
the new root value.
GLA University - CSE 4070
37. Delete from heap
Swap 25
and 7
Swap 7 and 17
Swap 7 and 15
GLA University - CSE 4070
38. Heap Sort
Heapify method picks the largest child key and compare
it to the parent key. If parent key is larger than heapify
quits, otherwise it swaps the parent key with the
largest child key. So that the parent is now becomes
larger than its children.
The heap algorithm works as:
◦ Build a heap from the given array using heapify
method
◦ Repeatedly delete the root element using max
heap(for descending order)
◦ Repeatedly delete the root element using min
heap(for ascending order)
GLA University - CSE 4070