CS 302 SORTING ALGORITHMS FINAL EXAM

Pataasin ang iyong marka sa homework at exams ngayon gamit ang Quizwiz!

Insertion Sort

A simple sorting algorithm that builds the final sorted array (or list) one item at time. It is much less efficient on large lists than more advanced algorithms such as quicksort, heapsort, or merge sort. Time Complexity: O(n^2) Advantages of Insertion Sort: -Simple to Implement: Insertion Sort is a simple sorting algorithm to understand and implement, especially for small data sets. -Efficient for Small Data Sets: Insertion Sort has a linear time complexity of O(n) for small data sets, making it a good choice for sorting small arrays. -In-place Sorting: Insertion Sort operates directly on the data array without requiring additional memory, making it memory efficient. -Stable Sorting: Insertion Sort is a stable sorting algorithm, which means it maintains the relative order of equal elements. Online Sorting: Insertion Sort can sort the array as the elements are received, making it suitable for online sorting scenarios. Disadvantages of Insertion Sort: -Performance: The worst-case and average time complexity of Insertion Sort is O(n^2), which means it can be slow for large data sets. -Not Suitable for Large Data Sets: Due to its quadratic time complexity, Insertion Sort is not recommended for sorting large collections of data. -Not Cache-Friendly: Insertion Sort can be slow due to poor cache locality, especially for large data sets.

Selection Sort

A simple sorting algorithm that repeatedly scans for the smallest item in the list and swaps it with the element at the current index. The index is then incremented, and the process repeats until the last two elements are sorted. Time Complexity: O(n^2) Advantages of Selection Sort: -Easy to Implement: Selection Sort is a simple sorting algorithm to understand and implement, making it suitable for beginners. -In-place Sorting: Selection Sort operates directly on the data array without requiring additional memory, making it memory efficient. -Stable Sorting: Selection Sort is a stable sorting algorithm, which means it maintains the relative order of equal elements. Disadvantages of Selection Sort: -Performance: The worst-case and average time complexity of Selection Sort is O(n^2), which means it can be slow for large data sets. -Not Suitable for Large Data Sets: Due to its quadratic time complexity, Selection Sort is not recommended for sorting large collections of data. -Number of Writes: Selection Sort performs a large number of writes, which can lead to more cache misses and slower performance.

Bubble Sort

A simple sorting moving through a list repeatedly, swapping elements that are in the wrong order. Time Complexity: O(n^2) Advantages of Bubble Sort: -Easy to Implement: Bubble Sort is one of the simplest sorting algorithms to understand and implement. -In-place Sorting: It operates directly on the data array without requiring additional memory, making it memory efficient. Disadvantages of Bubble Sort: -Performance: The worst-case and average time complexity of Bubble Sort is O(n^2), which means it can be quite slow for large data sets. -Not Suitable for Large Data Sets: Due to its quadratic time complexity, it's not recommended for sorting large collections of data.

Merge Sort

A type of divide and conquer algorithm that was incited by John von Neumann. First the list is divided into the smallest unit (1 element), then each element is compared with the adjacent list to sort and merge the two adjacent lists. Finally all elements are sorted and merged. Time Complexity: O(nlog(n)) Pros: -Far more efficient and fast than Bubble Sort, Insertion Sort, and Selection Sort -Consistent running time of O(n log n) Stable sorting algorithm Cons: -Uses more memory than other sorting algorithms -More complex to implement than other sorting algorithms

Heap Sort

Pros: -Efficient: Heap sort has a time complexity of O(n log n), which makes it one of the most efficient sorting algorithms. -In-place sorting: Heap sort can be implemented as an in-place sorting algorithm, which means that it does not require any additional memory space. -Simplicity: Heap sort is relatively simple to understand and implement, making it a popular choice for sorting algorithms. -Consistency: Heap sort exhibits consistent performance, which means that it performs equally well in the best, average, and worst cases. Cons: -Not stable: Heap sort is not a stable sorting algorithm, which means that it does not preserve the relative order of equal elements. -Not adaptive: Heap sort does not take advantage of the fact that the input may already be partially sorted, which can make it less efficient than other sorting algorithms in some cases. -Not suitable for small arrays: Heap sort is not suitable for small arrays because the overhead of building the heap can be significant compared to the actual sorting time. -Not suitable for linked lists: Heap sort is not suitable for linked lists because it requires random access to the elements, which is not possible with linked lists.

Quick Sort

Unstable, O(n log n) for a good pivot ,O(n^2) for a bad pivot Ω(n log n) : Uses partitioning O(n), Pick a median of 1st, middle, and last element for pivot. Random selection is also good, but expensive. Algorithm can be slow because of many function calls. Time Complexity: O(nlog(n)) Pros of Quick Sort: -Quick Sort is an efficient algorithm with an average time complexity of O(n log n). -It is an in-place sorting algorithm, meaning it does not require any additional memory beyond what is needed to store the input array. -Quick Sort is a divide-and-conquer algorithm, which makes it highly parallelizable and efficient on modern CPUs with multiple cores. -It is a stable sorting algorithm, meaning it maintains the relative order of equal elements in the input array. Cons of Quick Sort: -Quick Sort has a worst-case time complexity of O(n^2) when the input array is already sorted or reverse sorted, or when all elements are equal. -The pivot selection strategy can significantly impact the performance of Quick Sort. A poorly chosen pivot can lead to worst-case performance. -Quick Sort is not a stable sorting algorithm when implemented using the traditional Lomuto partition scheme. However, it can be made stable by using a different partitioning scheme, such as the Hoare partition scheme.


Kaugnay na mga set ng pag-aaral

unit 8 vocabulary - true or false

View Set

Chapter 6 Life in the Industrial Age

View Set

Qu'est-ce que tu fais en général pendant les vacances?

View Set

Consilience: The Unity of Knowledge

View Set