120 likes | 135 Views
Learn about Bubble Sort, Selection Sort, Insertion Sort, Sequential Search, and Binary Search algorithms. Understand their functions, implementation, and efficiency for data structures. Dive into efficient sorting and searching techniques.
E N D
Algorithms and Data Structuresالخوارزميات و تراكيب البياناتCS 211 Lecture 6: Searching & Sorting Algorithms
Sorting Algorithms • Bubble Sort Algorithm • Selection Sort Algorithm • Insertion Sort Algorithm
Bubble Sort AlgorithmItis a sorting algorithm that repeatedly steps through the list, compares adjacent pairs and swaps them if they are in the wrong order. The pass through the list is repeated until the list is sorted.
Selection Sort Algorithm • The list is divided into two parts, the sorted part at the left and the unsorted part at the right. • Initially, the sorted part is empty and the unsorted part is the entire list. • In every iteration of selection sort, the smallest element (considering ascending order) is selected from the unsorted array and swapped with the leftmost element, and that element becomes a part of the sorted array.
Insertion Sort Algorithm At each iteration, insertion sort algorithm removes one element from the input data, finds the location it belongs within the sorted list, and inserts it there. It repeats until no input elements remain.
Searching Algorithms • Sequential (Linear) Search Algorithm • Binary Search Algorithm
Sequential Search Algorithm • In this algorithm, a sequential search is made over items one by one. • It sequentially checks each element of the list until a match is found or the whole list has been searched.
Sequential Search Algorithm Wanted = 80 == ؟ == ؟ == ؟ ...... ....... 0 1 2 3 4 5 7 8 9 10 11 13 6 12 array 66 29 34 3 56 8 19 80 20 5 18 10 90 47
Binary Search Algorithm • Search a SORTED list by repeatedly dividing the search interval in half. • Begin with an interval covering the whole list . • Binary search compares the target value to the middle element of the interval. If they are equal, this element is returned. • If the target value is less than the middle element of the interval, narrow the interval to the lower half. Otherwise narrow it to the upper half. • Repeatedly check until the target value is found or the interval is empty.