Algorithm hm 1 Group14 Main Problem Prove that

Algorithm hm 1 Group_14

Main Problem • Prove that each of the following sorting algorithms is stable or show that it is unstable by giving a counter example; moreover, determine whether it is in place: bubble sort, insertion sort, quick sort, merge sort and heap-sort. • The default of the solution will be from left to right, from less to more.

Problem – algorithms stable or not (concept) • When an sorting algorithm is stable, it means the order of the same values will be the same before and after the sorting. • Talking of keeping the original order, that means the correlation between each elements must be in order to keep the order of the same values. • So when an algorithm will change the original order BEFORE it sort, it will not be stable.

Problem – algorithms stable or not(1) •

Problem – algorithms stable or not(2) • Merge sort • After dividing it to the smallest part(1 value), every element’s left-right correlation didn’t change. As the same will happen when merging the lists if the list on the left is using left priority rule. So as a result, it is stable. • Heap sort • After filling the list into the tree structure(it is actually an array, we understand the concept by projecting it to a tree structure), the left hand side will always be prioritized. When ever there are more then 3 same value elements, and 2 or more elements among them are initial in the left hand side, they will be out of order. So it is a unstable sorting algorithm.

Problem – algorithms in-place or not(concept) • An in-place algorithm is an algorithm that only need the storage of input data and a small amount of extra auxiliary space. • If the original input data is not changeable during sorting, then additional storage will be needed in order to save the sorted output. • Input data is defaulted to be transform using no auxiliary data structure.

Problem – algorithms in-place or not(1) • Bubble sort • During the sorting steps, it only swap in the original list. No extra storage is needed, so it is in-place. • Insertion sort • During the sorting steps, the storage size that the unsorted part decrease is the same as the storage size that the sorted part increase. So it can be done in-place. • Quick sort • In average case : During the sorting steps, it only swap in the original list. (3 -way swap) Because it need to use the concept of recursive, so the extra space complexity is O(ln(n)). According to wiki’s broadly definition of in-place, it is an in-place algorithm. (out-of-place with wiki’s strictest definition) • But when considering it’s worst case, the space complexity will be O(n), totally out-of -place. So as a conclusion, it is an out-of-place sorting algorithm.

Problem – algorithms in-place or not(2) • Merge sort • It is known to be an out-of-place algorithm because people usually use another list to save the sorted list. • But because during the merging process, the decreased storage size in the unsorted lists is the same as the increased storage size in the sorted list, there is in-place version of merge sort. (The extra space complexity is O(ln(n)), and because additional steps are needed to save space, the time complexity become O(n^2). • Heap sort • During the sorting steps, it only swap in the original list, No extra storage is needed, so it is in-place.
- Slides: 8