












Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
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; }