Download presentation
Presentation is loading. Please wait.
1
Bubble, Selection & Insertion sort
Sorting Bubble, Selection & Insertion sort
2
Sorting ? Sorting is the process of placing elements from a collection in some kind of order. A list of words could be sorted alphabetically or by length. A list of cities could be sorted by population, by area, or by code Bubble Sort Insertion Sort Selection Sort
3
Bubble Sort Bubble sort, sometimes referred to as sinking sort, is a simple sorting algorithm that works by repeatedly stepping through the list to be sorted, comparing each pair of adjacent items and swapping them if they are in the wrong order. The pass through the list is repeated until no swaps are needed, which indicates that the list is sorted. When to use? Small amount of data What if array is reversely sorted?
4
Alg.: BUBBLESORT(A) for i 1 to length[A] do for j length[A] downto i + 1 do if A[j] < A[j -1] then exchange A[j] A[j-1] 1 3 2 9 6 4 8 i = 1 j
6
When bubble sort? Bubble sort is easy to implement and understand
Bubble sort is used at small number of elements. Best case: array already sorted Worst case: smallest element at the end or array is reversely sorted in that case maximum number of swapping as smallest element has to come at first position. Sorting will be done at every position
7
Selection sort The algorithm divides the input list into two parts: the sublist of items already sorted, which is built up from left to right at the front (left) of the list, and the sublist of items remaining to be sorted that occupy the rest of the list. Initially, the sorted sublist is empty and the unsorted sublist is the entire input list The algorithm proceeds by finding the smallest (or largest, depending on sorting order) element in the unsorted sublist, exchanging it with the leftmost unsorted element (putting it in sorted order), and moving the sublist boundaries one element to the right Simple and good in performance!
8
When SS? Time complexity is same for best, average or worst case in selection sort as it compares one smallest (largest) element with every other element The efficiency of Selection Sort does not depend on the initial arrangement of the data. That’s why it follows same methodology for all type of input Use it on smaller inputs
10
Insertion sort The algorithm consists of inserting one element at a time into the previously sorted part of the array, moving higher ranked elements up as necessary. To start off, the first (or smallest, or any arbitrary) element of the unsorted array is considered to be the sorted part Starting with an empty left hand and the cards face down on the table. One card at a time is then removed from the table and inserted into the correct position in the left hand. To find the correct position for a card, it is compared with each of the cards already in the hand, from right to left.
12
Best case : already sorted
Worst case: reverse sorted
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.