Sorting
-ByVarsha kumari
GLA University - CSE 4070
Sorting : Outline
Bubble Sort
Insertion Sort
Selection Sort
Quick Sort
Two-way Merge sort
Heap Sort
GLA University - CSE 4070
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
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
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
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
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
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
Insertion Sort
GLA University - CSE 4070
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
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
Selection Sort : 1st
pass
GLA University - CSE 4070
2nd
pass
GLA University - CSE 4070
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
Algorithm
quickSort(array, first, last)
{
pivot = partition(array, first,last)
quickSort(array,first,pivot-1)
quickSort(array,pivot+1,last)
}
GLA University - CSE 4070
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
quickSort(arr,0,5)
Partition Initialization...
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]
GLA University - CSE 4070
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
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
Recursive calls to quickSort()
GLA University - CSE 4070
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
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
Quicksort
GLA University - CSE 4070
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
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
Mergesort
GLA University - CSE 4070
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
Merge
array arr[ ]
GLA University - CSE 4070
Merge
GLA University - CSE 4070
GLA University - CSE 4070
Merge
GLA University - CSE 4070
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
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
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
Insert 17 in max-heap
maintain the heap property
GLA University - CSE 4070
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
Delete from heap
Swap 25
and 7
Swap 7 and 17
Swap 7 and 15
GLA University - CSE 4070
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
Algorithm
Heapsort(A,n)
{
   Buildheap(A)
   for i = 0 to n-1 
call Insert_heap(A,n,A[i])
End for
   Repeat while n>0
call Delete_heap(A,n,value)
set n = n – 1
end while loop
}
// heapify
// Get max value after
deletion (for
descending order)
GLA University - CSE 4070
Complexities
Sortings best case Average
case
Worst
case
Bubble sort O(n ) O(n2
) O(n2
)
Insertion sort O(n ) O(n2
) O(n2
)
Selection sort O(n2
) O(n2
) O(n2
)
Quick sort O(nlogn) O(nlogn) O(n2
)
Merge sort O(nlogn) O(nlogn) O(nlogn)
Heap sort O(nlogn) O(nlogn) O(nlogn)
GLA University - CSE 4070

Sorting.ppt read only

  • 1.
  • 2.
    Sorting : Outline BubbleSort Insertion Sort Selection Sort Quick Sort Two-way Merge sort Heap Sort GLA University - CSE 4070
  • 3.
    Bubble Sort It sortthe 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) { Repeatfor 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 > 25swap 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 worksby 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) { Repeatfor 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
  • 9.
  • 10.
    Selection Sort It takesn-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) { Repeatfor 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
  • 12.
    Selection Sort :1st pass GLA University - CSE 4070
  • 13.
  • 14.
    Quick Sort Quick sortworks 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
  • 15.
    Algorithm quickSort(array, first, last) { pivot= partition(array, first,last) quickSort(array,first,pivot-1) quickSort(array,pivot+1,last) } 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
  • 17.
    quickSort(arr,0,5) Partition Initialization... first movesto the right until Array element > pivot element last moves to the left until Array element < pivot element swap arr[first] and arr[last] GLA University - CSE 4070
  • 18.
    quickSort(arr,0,5) first moves tothe 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 UNTILleft & right cross first & last CROSS!!! Swap pivot and arr[last] Return new location of pivot to quicksort function = return 3 GLA University - CSE 4070
  • 20.
    Recursive calls toquickSort() GLA University - CSE 4070
  • 21.
    Recursive calls toquickSort() 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 toquickSort() 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
  • 23.
  • 24.
    Mergesort It focuses ondivide, 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
  • 26.
  • 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
  • 28.
    Merge array arr[ ] GLAUniversity - CSE 4070
  • 29.
  • 30.
  • 31.
  • 32.
    Heap A heap isa 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 inmax-heap maintain the heap property GLA University - CSE 4070
  • 36.
    Delete from heap Deletemax ◦ 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 Swap25 and 7 Swap 7 and 17 Swap 7 and 15 GLA University - CSE 4070
  • 38.
    Heap Sort  Heapifymethod 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
  • 39.
  • 40.
    Complexities Sortings best caseAverage case Worst case Bubble sort O(n ) O(n2 ) O(n2 ) Insertion sort O(n ) O(n2 ) O(n2 ) Selection sort O(n2 ) O(n2 ) O(n2 ) Quick sort O(nlogn) O(nlogn) O(n2 ) Merge sort O(nlogn) O(nlogn) O(nlogn) Heap sort O(nlogn) O(nlogn) O(nlogn) GLA University - CSE 4070