Sorting Final
Sorting Final
GROUP 20:
Do An Nam – 1624731
Ly Minh Hoang –
Nguyen Cong Bao -
Table of contents
01 Introduction to
Sorting Algorithms 04 Insertion Sort
02 Bubble Sort
05 Merge Sort
03 Selection Sort
06 Quick Sort
Introduction to Sorting
Sorting refers to arranging a set of data in some logical order.
For example, a telephone directory can be considered as a list where
each record has three fields – name, address and phone number.
Being unique, phone number can work as a key to locate any record
in the list.
Not
Stable
Stable
Time complexity
• Best case: O(n)
• If the array is all but sorted
• Inner Loop won’t execute so only some constant time the statement will run
• Worse case: O(n2)
• Array element in reverse sorted order
Space Complexity
• Since no extra space beside n variables is needed for sorting so
Space Complexity = O(n)
• The sorting algorithms we discussed so far
has the worst-case running time of Θ(n^2).
• When the array become longer and contain
Divide and more elements, the time it cost for sorting is
creasing.
conquer
• Two algorithms that have better running
algorithms time:
1. Merge sort.
2. Quick sort.
• Merge sort and quick sort share a similar idea.
Divide and conquer • Divide
algorithms • Conquer
• Combine
• Divide the array into two smaller array.
Merge sort
• The algorithms keep divide these sub-array by two until
there is only 1 element standing alone.
1. Find the middle point to divide the array into two halves:
2. middle m = l + (r – l)/2
3. Call the function recursively:
4. Call mergeSort function for first half:
5. Call mergeSort(arr, l, m)
6. Call mergeSort function for second half:
7. Call mergeSort(arr, m + 1, r)
8. Merge the two halves sorted in steps 2 and 3:
9. Call merge(arr, l, m, r)
Merge ( arr[] , l , m , r )
1. Set value of I and j to 0, k to l
2. Set value of n1 to ( m – l + 1 ) and n2 to ( r – m )
3. Create array L with n1 elements and array R with n2 element
4. Copy the first half to array L
5. Copy the second half to array R
6. While i is smaller than n1 and j is smaller than n2 do steps 7 through 13
7. If element at location i of array L is smaller than element at location j of array R do steps 8 through 12
8. Copy it to position k of the input array
9. Increase i by 1
10. Else ( If element at location i of array L is greater than element at location j of array R )
11. Copy it to position k of the input array
12. Increase j by 1
13. Increase k by 1
14. Stop
Time efficiency
Merge sort • Total step log( n + 1 ).
efficiency • Running time O(n).
Time complexity of O(n*log n).
Quick sort
Partioning the array to be sorted and
each partition in turn sorted
recursively.
main function
Time complexity
• Best case: O(n*logn).
• Average case: O(n*logn).
• Worse case: O(n2).
• Worst case in quick sort rarely occurs because by changing the choice
of pivot, it can be implemented in different ways.
Worst case in quicksort can be avoided by choosing the right pivot
element.
THANK YOU
Do you have any question ?