Sorting definition
a process in which data in a list are organized in certain order
Advantages of sorted list
• Easier to understand and analyze data collection.• Searching process will be much faster
Internal sort
entire data fit in computer's main memory, small size of data
external sort
collection of data will not fit in the computer's main memory and will reside in secondary storage, large size of data
Sorting Algorithms Categories
Internal Sort, External Sort
List of simple data types
integers, char or strings
List of Records
the list contain more than one element or record.
main activity in sorting process
Compare,swap
Compare
compare between two elements. If they are not in correct order, then
Swap
Change the position of the elements in order to get the right order.
efficiency of sorting algorithm is measured based on
• Number of comparisons
• Number of swapping between elements
Worst-case analysis considers
the maximum amount of work an
algorithm will require on a problem of a given size. (Data is totally
unsorted)
Best-case analysis considers
the minimum amount of work an algorithmwill require on a problem of a given size. (Data is almost sorted)
Average-case analysis considers
the expected amount of work that analgorithm will require on a problem of a given size.
Bubble sort step
• Compare adjacent elements in the list• Exchange the elements if they are out of order• Each pass moves the largest (or smallest) elements to the end ofthe array• Repeating this process eventually sorts the array into ascending (ordescending) order.• Bubble sort is a quadratic algorithm O(n 2 ), which only suitable to sortarray with small size of data.
what determine the efficiency of Bubble Sort Algorithm
number of comparison between elements and the number ofexchange between elements
number of comparisons between elements in Bubble
O(n2)
In bubble sort, best has no swapping since all data already
in right position
In bubble sort, if there is no exchange of data
the list is already sorted
Bubble Sort: Algorithm Complexity, Number of Swaps, Best Case
O(1)
Bubble Sort: Algorithm Complexity, Number of Compares, Best Case
O(n)
Bubble Sort: Algorithm Complexity, Number of Compares, Worst Case
O(n2 )
Bubble Sort: Algorithm Complexity, Number of Swaps, Worst Case
O(n2 )
Selection sort steps
• Choose the largest / smallest item in the array and place the itemin its correct place• Choose the next largest / next smallest item in the array and placethe item in its correct place.• Repeat the process until all items are sorted.• Does not depend on the initial arrangement of the data• Only appropriate for small n - O(n2 ) algorithm
Selection Sort Analysis, the external loop will iterate
from n-1 to 1.
for (int last = n-1; last>=1; --last)
Selection Sort Analysis, find the largest number in subarray, the number of comparison inside the internal loop must equal to the value of last.
for (int p=1;p <=last; ++p)
number of comparisons between elements in Selection Sort
O(n2)
Number of comparison between elements of worst case, best case or average case?
Same
efficiency of Selection Sort
does not depend on the initialarrangement of the data.
Insertion Sort
• Take multiple passes over the array• Partition the array into two regions: sorted and unsorted• Take each item from the unsorted region and insert it into itscorrect order in the sorted region• Find next unsorted element and insert it in correct place,relative to the ones already sorted• Analysis• Appropriate for small arrays due to its simplicity
Insertion Sort Analysis – Best Case
O(n)
Insertion Sort Analysis – Worse Case
O(n2)
Divide
• Break into sub-problems that are themselves smaller instances ofthe same type of problem• Recursively solving this problem
Conquer (overcome)
• The solution to the original problem is then formed from the solutions to the sub-problems.
Merge sort
• Applies divide and conquer strategy.• Three main steps in Merge Sort algorithm:• Divide an array into halves• Sort each half• Merge the sorted halves into one sorted array• A recursive sorting algorithm• Performance is independent of the initial order of the array items
MergeSort() function
• A Recursive function that divide the array into pieces until eachpiece contain only one item.• The small pieces is merge into larger sorted pieces until one sortedarray is achieved.
Merge() function
• Compares an item into one half of the array with item in the otherhalf of the array and moves the smaller item into temporary array.Then, the remaining items are simply moved to the temporaryarray. The temporary array is copied back into the original array.
Merge Sort Analysis
The list is always divided into two balanced list (or almost balanced forodd size of list)• The number of calls to repeatedly divide the list until there is one itemleft in the list is:• If the left segment and the right segment of the list have the equal size(or almost equal size), then x ≈ lg n. The number of iteration isapproximately n lg n.• The same number of repetition is needed to sort and merge the list.• Thus, as a whole number of steps needed to sort data using merge sortis 2n lg n, which is O(n lg n).
MergeSort Analysis
• Worst case: O(n * log 2 n)• Average case: O(n * log 2 n)• Performance is independent of the initial order of the arrayitems
Mergesort advantage
extremely fast algorithm
Disadvantage
requires a second array ( large array ) as large as the original array
Quick sort steps
• Choose a pivot (first element in the array)• Partition the array about the pivot• items < pivot• items >= pivot• Pivot is now in correct sorted position• Sort the left section again until there is one item left• Sort the right section again until there is one item left
quickSort() function
• a recursive function that willpartition the list into several sublists until there is one item left inthe sub list.
• partition() function
• organize the data so that the itemswith values less than pivot will beon the left of the pivot, while thevalues at the right pivot containsitems that are greater or equal topivot.
Quick Sort Analysis
• The efficiency of quick sort depends on the pivot value.• This class chose the first element in the array as pivot value.• However, pivot can also be chosen at random, or from the last elementin the array.• The worse case for quick sort occur when the smallest item or thelargest item always be chosen as pivot value causing the left partitionand the right partition not balance.24Example of worse case quick sort: sortedarray [1 2 5 4] causing imbalance partition.
• The best case for quick sort happen when the list is partition into balancesegment.• Must chose the right pivot that can put other items in balance situation.• The number of comparisons in partition process for base case situation is asfollows:25The best case for quick sort happen when theleft segment and the right segment is balanced(have the same size) with value x ≈ lg n
Quick Sort
• Average case: O(n * log 2 n)• Worst case: O(n 2 )• When the array is already sorted, and the smallest item ischosen as the pivot• Quicksort is usually extremely fast in practice• Even if the worst case occurs, quicksort’s performance is acceptablefor moderately large arrays