












Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
Prepare for your exams
Study with the several resources on Docsity
Earn points to download
Earn points by helping other students or get them with a premium plan
An overview of simple sorting and searching algorithms, including linear sort, bubble sort, selection sort, insertion sort, linear search, and binary search. It explains the basic ideas behind each algorithm, presents pseudocode implementations, and discusses their time complexity. The document also includes c++ code examples for linear and binary search, illustrating how these algorithms can be implemented in practice. It is designed to help students understand the fundamental concepts of sorting and searching and their applications in software development. The document emphasizes the importance of these algorithms in various applications, such as databases, web services, and ai, where sorting and searching operations account for a significant portion of total execution time. It also highlights how faster sorting and better search techniques can improve application performance and reduce lookup time.
Typology: Lecture notes
1 / 20
This page cannot be seen from the preview
Don't miss anything!













C H A P T E R T W O : S I M PL E S O R T I N G A N D S E A R C H IN G A L G OR I T H M S DATA STRUCTURES AND ALGORITHM BY: KIBRU G.
▪Sorting is a process of reordering a list of items in either increasing or decreasing order. ▪Sorting is used to arrange names or numbers in meaning full ways. ▪Sorting algorithms commonly consist of two types of operation: comparisons and data movements.
▪Let us consider “A” is an array with n elements:
▪ Bubble Sort is the simplest algorithm to implement and the slowest algorithm on very large inputs. ▪ Bubble sort: Makes a number of passes through array: ▪ First bubble the largest element putted in last position, by iteratively comparing & swapping adjacent elements starting with the start of the list. ▪Next bubble next largest element putted to last- 1 position ▪etc. until all apart from first element placed ▪ Basic Idea: Loop through array from i= 0 to n and swap adjacent elements if they are out of order.
▪ Basic Idea:
= 0 ) Array[pos+ 1 ]=array[pos] pos=pos- 1
▪ Searching involves checking a collection of items to find a particular element or confirming its absence. ▪ There are two elementary searching algorithms: ▪Sequential Search, and ▪Binary Search
Linear Search (Sequential Search)
#include
Linear Search (Sequential Search)
Binary Search
#include
Binary Search int main() { int arr[5]; int target; cout<<"Enter Elements of the array\n"; for(int i = 0; i < 5; i++) { cout<<"Enter Element at "<<i<<": "; cin>>arr[i]; } cout<<"Enter Elements of the Element to be Searched: "; cin>> target; int result = binarySearch(arr, target); if (result != - 1) { cout << "Element found at index: " << result << endl; } else { cout << "Element not found in the array." << endl; } return 0; }