You are on page 1of 28

• 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.

• Then it working on these element and try to put them in


right order.
Merge sort pseudo code
Main function
1. Declair the unsorted array
2. Call the merge sort array and send into it the array , 0 ,
size_of_array – 1
3. Print the sorted array
4. End program
 MergeSort function receive arr[] ; l ; r from the main function as
argument

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).

 Time complexity of Merge Sort is O(n*Log n) in all the 3


cases (worst, average and best).
• Worst Case Time Complexity [ Big-O ]: O(n*log n)
• Best Case Time Complexity [Big-omega]: O(n*log n)
• Average Time Complexity [Big-theta]: O(n*log n)

 Space Complexity: O(n)


Quick sort is the fastest known sorting
algorithm in practice.

Best time performance.

Quick sort
Partioning the array to be sorted and
each partition in turn sorted
recursively.

Hence also called partition exchange


sort.
Pseudo code

 main function

1. Declair the unsorted array


2. Set n to hold the number of total element of the array
3. Call the quickSort function
4. Print the sorted array
5. Stop
 quickSort ( arr[] , low, high )

1. If low is less than high, do


2. Set value of index to the returning value of function partition
3. (Index is the location of the element that divided the array in to two sub array)
4. Call the function recursively ( to sort the sub array ):
5. quickSort ( arr[] , low, index – 1 )
6. quickSort ( arr[] , index – 1, high )
7. Stop
 partition ( arr[] , low , high )
1. Set value of p to element at position high of the array
2. Set value if i to low – 1 and j to low
3. While j is less than high
4. If element at position j of the array less than p,
5. Increase i by 1
6. Swap the element at position i with position j of the array
7. Increase j by 1
8. Swap the element at position ( i + 1 ) with position high of the array
9. The function return value of ( i + 1 )
10. Stop
Quick sort efficiency

 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.
• The bubble sort is an easy way to
arrange data in ascending or
descending order.
• Operating principle:
Bubble sort  Compare 2 elements that are side by
side and swap if necessary
 Repeat that step to the end of the list
 Go through the list again and again
until there is no swap to do left
Bubble sort pseudo code
Do
Set swap flag to false.
For count is set to each subscript in array from 0
through the next-to-last subscript
If array[count] is greater than array[count + 1]
Swap the contents of array[count] and
array[count + 1].
Set swap flag to true.
End If.
End For.
While any elements have been swapped
Bubble sort efficiency

Time complexity: Space complexity: O(1)


Best case: O(n)
Worst case: O(n^2)
Average time complexity: O(n^2)
• When it comes to large array, the
selection sort is better than
bubble sort because it moves
elements into its right place
immediately.
Selection sort • Operating principle: repeatedly
finding the minimum/maximum
element from the unsorted part
and putting it at the beginning.
Selection
sort pseudo
code
• Time complexity:
 Time complexity of selection sort is O(n^2) in
Selection best and worst case
sort  Therefore, average time is also 0(n^2)
 Best case does O(1) swap
efficiency  Worst case does O(n) swaps
• Space complexity: O(1)
• Insertion sort is a sorting algorithm that
places an unsorted element at its suitable
place in each iteration .
• Operating principle:
 Insertion sort works similarly as we sort
cards in our hand in a card game.
Insertion sort
We assume that the first card is already
sorted then, we select an unsorted card. If the
unsorted card is greater than the card in hand,
it is placed on the right otherwise, to the left.
In the same way, other unsorted cards are
taken and put in their right place.
Insertion sort algorithm

Step 1 − If it is the first element, it is already sorted. return 1;


Step 2 − Pick next element
Step 3 − Compare with all elements in the sorted sub-list
Step 4 − Shift all the elements in the sorted sub-list that is greater
than the value to be sorted
Step 5 − Insert the value
Step 6 − Repeat until list is sorted
Insertion sort efficiency

 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)

You might also like