0% found this document useful (0 votes)
23 views

Types of Sorting

Copyright
© © All Rights Reserved
Available Formats
Download as PDF or read online on Scribd
0% found this document useful (0 votes)
23 views

Types of Sorting

Copyright
© © All Rights Reserved
Available Formats
Download as PDF or read online on Scribd
You are on page 1/ 4
Types of Sorting Algorithm Example of Sorted Algorithm + Library Book Arrangement Product Listing + School Report Card + Hospital Record Management “Types of Sorting Algorithm There ave diferent mainy used pes of sorting algorithms, but here we wil discuss 6 sorting algorithms that are + Bubble Sort Algorithm + Setoction Sort Algorithm + Insertion Sort Algorithm + Merge Sort Algorithm + Quick Sort Algorithm ‘+ Heap Sort Algorithm Now, we wil discuss them individually in complete detail wth tner advantages and disadvantages, Bubble Sort Algorithm [Risa simple sorting technique to arrange a Ist of elements {Ike rumbers or leters) in a specific order, usually ascending or descending. The process involves repeatedly stepping through the ist, comparing adjacent elements, and swapping them if they are in the wrong order Let's understand with the helo of an example Imagine an array of numbers: (5, 3,8, 4,1 First Pass Through the Array: ‘+ Compare the ist two elements (5 and 3). Since 5 is greater than 3, swap them. The array becomes (3, 5,8,4,7 + Next, compare § and 8, No swap is needed as Sis less than 8. + Then compare 8 and 4, Swap them since Bis greater. The array becomes (3,5, 4, 8,1 + Finally, compare 8 and 1, Swap them, The arrays now [3, 5, 4,18] Second Pass: + Repeat the process, ignoring the last element (as it's already ints correct postion). + Aftor this pass, the next highest number, 5, willbe ints correct position, ‘Subsequent Passes: + Continue this process, with each pass requiring one less comparison than the previous. + The array is sorted ina complete pass when no swaps are needed. ‘Advantages and Disadvantages of Bubble Sort Algorithm ‘Advantages of Bubble Sort + Simplicity: Itis easy to understand, making ft 8 good educational tool for beginners in programming “+ No Extra Space Needed: Bubble sorts an in-place sorting algorithm, meaning it doesn require extra space for sorting + Detection of Sorted List It can detect if the lst is already sorted, making it arrays. ffilent for nearly sorted Disadvantages of Bubble Sort + Inefficient for Large Lists: thas atime complexity of O(n2), making i inefficient for large datasets, + Slow and Less Efficient: Compared te more advanced sorting algorithms, nubble sort is generally slower ‘and ess efficient + Too Many Swaps: It performs poorly because it makes too many swaps, potential leading o high processing time Types of Sorting Algorithm ino two parts: ine sortea part atsne stars oF he ust ana tne unsortea part at ine rest oF te Intl, the sorted part is empty, and the unsorted parts the entie lis. The algorithm repeatedly selects the smallest (or largest, depending on the desired order) element from the unsorted part and moves it to the end of the sorted part. Let's understand with the help of an example Consider an array of numbers: (28, 10, 14,37, 1]. First Pass: + Find the smallest element the aray, which is 10 ‘+ Swap t withthe ist element. The array becomes (10, 28, 14, 37,13) Second Pass: + Find the next smallest element inthe remaining unsorted part, wich is 13. + Swap it withthe fist element of the unsorted part (28). The array is now {10, 13, 14,27, 29) Subsequent Passes: + Continue this process for each element, exclusing the last ane (as it will already be ints correct place) + After each pass, the next smallest element is placed in its correct position. ‘Advantages and Disadvantages of Selection Sort Algorithm [Advantages of Selection Sort Algorithm + Simplilty: Like Bubble Sort, tis easy to understand and implement, making it sultable for educatlonal purposes. ‘+ Momory ficiency: Its an in-place sort (doesn't require additional temporary array). + Performance on Small Lists: Fo smal ys it can perform reasonably well + Minimized Swaps: Unlike Bubble Sort, it minimizes the number of swaps, as it only swaps once per pass. Disadvantages of Selection Sort + Inefficient on Large Lists: With a time complexity of 772), it becomes inefficient for sorting large datasets ‘+ Not Adaptive: t does not adapt to the existing order af elements; hence, Its performance is constant Irrespective of te intial order of elements + No Early Termination: passes are complete, Unlike Bubble Sort, t doesnt stop early if the array becomes sorted before all tion Sort Algorithm itis a simple and intuitive sorting algorithm that bulds the final sorted lst one item at atime, It's much like the way you might sort playing cards in your hands. Lets take an example jet a better understanding Imagine you have an array of numbers: (12, 11,13, 5, First Element (Initial Step): ‘The fist elament (12) is considered sorted by itsett Second Element: + Look at the second element (1), Compare I with elements in the sorted part (12), + Since 11s smaler than 12, move 12 one position to the rghit and place 11 before 12. The aay becomes: (01, 72,13,5,61 “Third Element: Look at the made. rd element (13). 1's already in the correct postion relative to the frst two. No changes are Fourth Element: Now, consider the fou ‘one position to the rg 12,13,61 «h element (5). Compare it with each element inthe serted part and shift them fom intl the correct position for 5 Is found, Insert § there. The array becomes [5,1 Fifth Element: Finally, place the last element (6) in its correct position. Types of Sorting Algorithm + Simple implementation ts straightforward and easy to implement «+ Efficient for Small Data Sets: It works well for small or nearly sorted ists + Adaptive: It can adapt tothe existing order of elements, performing well when the ists partially sorted + Stable: it does not change the relative order of elements with equa keys + In-Place: Requires only a constant amount f Ot) of aditional memory space. Disadvantages of insertion Sort + Inefficient for Large Lists: Wit atime complexity of O(0%2), it becomes inefficient and stow for arger datasets + More Complex Than Other Simple Sorts: Though straightforward, i's more complex to understand and Implement than Bubble Sort ‘+ Many Movements: I the unsorted part has a smaller element towards the end, it requires many shits, ‘Merge Sort Algorithm Ik is highly efficient, comparison-based, dvide-and-conquer sorting algorithm. It civides the input array into two halves, cals itself the two halves, anc then merges the two sorted halves. Let's take an example to better understand now it works. CConsider an array of numbers: (38, 27,43, 3,8, 82,10) Divide the Array: 1, vide the array Into two halves. In our example: (38,27, 42] and [3, 8, 82, 10] + Keop dividing each haf until you have arrays with single elements. Single elements are considered sorted, Merge Halves: + Start merging these element arrays, thor. Merge each pair of singlo-oloment arrays into sorted two- + Continue merging these sorted arrays, maintaining the sorted order, until you get back tothe full length of the array. Instance, merge [38] and [27] into (27, 38], and so on Final Sorted Array After the final merge, you wil have a completely sorted array (3, 8,10, 27, 38, 43, 82) ‘Advantages and Disadvantages of Merge Sort Algorithm ‘Advantages of Merge Sort + Efficient and Consistent: has a time complexity of O(n fog minal cases, making it more efficent than algorithms tke Bubble, Selection, o Insertion Sort, especial for large datasets. + Stable: Doos not change the relative order of elements with equal keys. + Paral izable: Merge Sort can be easily paralized duc tits divide. nd-conquer nature + Good for Large Data Sets: Performs well wth large datasets or datasets that do not fit entirely in RAM (External Merge Sort) Disadvantages of Merge Sort + Space Complexity: Requires adsitional space (0 be a drawback for very large arrays. forthe temporary array used for merging, This can + Slower for Small Tasks: For smaller tasks, simpler algorithms Ike Insertion Sort may be faster due to lower overnead. ‘+ Complex Implementation: More complex to understand and implement compared to more straightforward sorting algorithms Ike Bubble or Selection Sort. Quick Sort Algorithm itis a highly efficient sorting algorithm based on the divie-and-conque’ principle. works by selecting @ ‘pivot element from the array and partitioning the other elements into two sub-arrays, according to whether they are les than or greater than the pivot Let's take an example to get a better understanding of how it works. Consider an array of numbers: [10, 80, 30, 90, 4, 80,70 Types of Sorting Algorithm + Rearrange the array s that elements smaller than the pivot come before it, while elements greater come ater. Partitioning: + After the first pass, the array might look lke this: [10, 30, 40, 50, 70, 90, 601 ‘+ Hote, elements less than 70 are positioned before it, and tnose greater are a Recursive Sort + Apply the same process to the sub-arrays formed (olements bofore and after the pivot + Continue this recursively, resulting ina sortes array. [Advantages and Disadvantages of Quick Sort Algorithm [Advantages of Quick Sort + Time Efficiency: On average, it has atime complexity of O(n log n), making it algorithms for large datasets. ne ofthe fastest sorting + Space Efficiency: Being an in-place sort, t does not requir adtional storage, which is advantageous for arge arrays. + Highly optimi ble: Many variations of Quick Sort can be optimized for specific types of dats Disadvantages of Quick Sort + Worst-Case Performance: In the worst-case scenario, such as when the smallest or largest element is always chosen asthe pivot, its time complexity degrades to O(n*2), ‘+ Unstable: The relative positioning of aqua olements might change, which isnot ideal for certain applications. + Pivot Selection: The cholee of pivot can greatly affect performance, Gad pivot selection can lead to poor performance. Heap Sort Algorithm It is comparison-based sorting algorithm that uses a binary heap data structure. It works by building @ heap from the input data, then repeatedly extracting the maximum element from the heap and rebuilding the heap untt all elements are sorte. Lets take an example to get a better understanding of how it works. Consider an array of numbers: [12, 1,13, 5, 6,71 Building a Max Heap: + First transform the array into a max heap (2 complete binary tee where each parent node i greater than its chile nodes) + For our array, after building the max heap t may look tke ths: [13, 11,12, 5,6, 7. Sorting the Array: + Remove tne oot element (13) and swap it withthe last element inthe heap (7) since it's the largest. Now, the heap is (7, 1,12, 6], and the sorted part is [131 + Rebull the heap withthe remaining elements. After rebuilding, the heap might be [12, 11,7, 5,6 Repeat this process until all elements ae removed from the heap and placed in the sorted section. The final sorted aray is [5,6 7,11, 12,13. ‘Advantages and Disadvantages of Heap Sort Algorithm Advantages of Heap Sort + Time Complexity: Heap Sort has atime complexity of O(n log n) for allcases, whichis better than algorithms like Bubble Sort or Insertion Sort, especially for large datasets, + Space Efficiency: I's an in-place sorting algorithm requiring no additonal storage space + No Stack Overflow Risk: Unike Quick Sort or Merge Sort, Heap Sort does not use recursion, so it doesrit risk stack overflow errors. Disadvantages of Heap Sort + Slower than Quick Sort: While it has the same average time complexity as Quick Sor, i's generally slower in practice due to more complex heap operations.

You might also like