Download presentation
Presentation is loading. Please wait.
1
Sort Algorithm
2
Introduction Bubble Sort Selection Sort Insertion Sort Merge Sort
3
Bubble Sort Also known as exchange sort
Another simple, but inefficient, sorting algorithm It works by repeatedly stepping through the list to be sorted, comparing two items at a time and swapping them if they are in the wrong order. The pass through the list is repeated until no swaps are needed, which means the list is sorted.
4
Selection Sort a simple, but inefficient, sorting algorithm
The first iteration of the algorithm selects the smallest element in the array and swaps it with the first element. The second iteration selects the second-smallest item (which is the smallest item of the remaining elements) and swaps it with the second element. The algorithm continues until the last iteration selects the second-largest element and swaps it with the second-to-last index, leaving the largest element in the last index. After the ith iteration, the smallest i items of the array will be sorted into increasing order in the first i elements of the array.
5
An example of Selection Sort
[0] [1] [2] [3] [4] ……
6
Insertion Sort Another simple, but inefficient, sorting algorithm
The first iteration of this algorithm takes the second element in the array and, if it is less than the first element, swaps it with the first element. The second iteration looks at the third element and inserts it into the correct position with respect to the first two elements, so all three elements are in order. At the ith iteration of this algorithm, the first i elements in the original array will be sorted.
7
An example of Insertion Sort
[0] [1] [2] [3]
8
Merge Sort An efficient sorting algorithm, but is conceptually more complex than selection sort and insertion sort The merge sort algorithm sorts an array by splitting it into two equal-sized subarrays, sorting each subarray and then merging them into one larger array. With an odd number of elements, the algorithm creates the two subarrays such that one has one more element than the other.
9
An example of Merge Sort
27 10 12 13 15 22 1. Divide the array: 27 10 12 20 and 25 13 15 22 Sort each subarray: 10 12 20 27 and 13 15 22 25 Merge the subarrays: 10 12 13 15 20 22 25 27
11
An example of Merge Sort
Array A Array B
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.