Sorting and Searching Algorithms Explained - Prof. Gebremichael, Lecture notes of Data Structures and Algorithms

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

2024/2025

Uploaded on 05/17/2025

afendi-mohammed
afendi-mohammed 🇪🇹

8 documents

1 / 20

Toggle sidebar

This page cannot be seen from the preview

Don't miss anything!

bg1
Data
Structures
and
Algorithms
CHAPTER TWO:
SIMPLE SORTING A ND
SEARCHING ALGORITHMS
DATA STRUCTURES AND ALGORITHM BY: KIBRU G.
pf3
pf4
pf5
pf8
pf9
pfa
pfd
pfe
pff
pf12
pf13
pf14

Partial preview of the text

Download Sorting and Searching Algorithms Explained - Prof. Gebremichael and more Lecture notes Data Structures and Algorithms in PDF only on Docsity!

Data

Structures

and

Algorithms

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.

Outline

▪Sorting Algorithms

▪ Ordinary/Linear Sort

▪ Bubble Sort

▪ Sélection Sort

▪ Insertion Sort

▪Searching Algorithms

▪ Linear/Sequential Searching

▪ Binary Searching

Sorting Algorithms

▪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.

  1. Comparison is simply comparing one value in a list with another. These involve checking the relative order of two elements (e.g., determining if one value is greater than, less than, or equal to another).
  2. Data movement includes swapping, shifting, or copying elements within the array or data structure to achieve the correct order. ▪By default, sorting is performed in ascending order.

▪The following are simple sorting algorithms used to sort

small-sized lists:

1.Ordinary/Linear Sort

2. Bubble Sort

3.Selection Sort

4.Insertion Sort

Sorting Algorithms

▪Let us consider “A” is an array with n elements:

  1. input “n” elements of the array ;
  2. initialize i= 0 and repeat through step 4 if (i<n)
  3. initialize j=i+ 1 and repeat through step 4 if (j<n)
  4. if(array[i]> array[j]) { temp = array[i] array[i]=array[j] array[j]=temp }
  5. Display the sorted element of array
  6. exit Sorting Algorithms: Ordinary/Linear Sort

▪ 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.

Sorting Algorithms: Bubble Sort

▪Each element is swapped directly with the element that

occupies its correct position.

▪ Basic Idea:

▪Scan array to find smallest element: swap it with element

in first position

▪Scan remainder of array to find smallest element: swap it

with element in second position

▪etc. until all elements placed in correct position

Sorting Algorithms: Selection Sort

  1. input “n” elements of the array ;
  2. initialize i= 0 and repeat through step 5 if (i<n) min=array[i] loc=i
  3. initialize j=i+ 1 and repeat through step 4 if (j<n)
  4. if(array[j]<min) { min = array[j] loc=j }
  5. if(loc!=i) { temp = array[i] array[i] = array[loc] array[loc]=temp }
  6. Display the sorted element of array
  7. exit

Sorting Algorithms: Selection Sort

  1. input “n” elements of the array ;
  2. initialize i= 1 and repeat through step 4 if (i<n) temp=array[i] pos=i- 1
  3. repeat through step 3 if ( temp < array[pos] and pos

    = 0 ) Array[pos+ 1 ]=array[pos] pos=pos- 1

  4. array[pos+ 1 ]=temp
  5. Display the sorted element of array
  6. exit

Sorting Algorithms: Insertion Sort

▪ 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

Searching Algorithms

  1. input “n” elements of the array and elements going to be searched “x”.
  2. initialize i= 0 and repeat through step 3 if (i<n)
  3. if( array[i]==x) flag= 1 return i
  4. if(flag== 0 ) return - 1
  5. if flag == 0 index=- 1 otherwise index=i
  6. return index
  7. exit

Searching Algorithms:

Linear Search (Sequential Search)

#include using namespace std; // Function to perform linear search int linearSearch(int arr[],int target) { for (int i = 0 ; i < 5 ; i++) { if (arr[i] == target) { return i; // Return the index if target is found } } return - 1 ; // Return - 1 if target is not found } 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 = linearSearch(arr, target); if (result != - 1 ) { cout << "Element found at index: " << result << endl; } else { cout << "Element not found in the array." << endl; } return 0 ; }

Searching Algorithms:

Linear Search (Sequential Search)

  1. input “n” elements of the array and the element going to be searched “x” ;
  2. initialize i= 0 and repeat through step 3 if (i<n)
  3. do step 4 while left <= right and flag == 0
  4. mid= left + (right - left)/ 2 ; if(array[mid]==x) { flag= 1 return mid } if(array[mid]<x) left=mid+ 1 otherwise right=mid- 1
  5. if flag = 0 index=- 1 otherwise index=mid
  6. return index

Searching Algorithms:

Binary Search

#include using namespace std; // Function to perform binary search (iterative) int binarySearch(int arr[], int target) { int left = 0; int right = 5 - 1; while (left <= right) { int mid = left + (right - left) / 2; // Avoids overflow if (arr[mid] == target) { return mid; // Target found } else if (arr[mid] < target) { left = mid + 1; // Search the right half } else { right = mid - 1; } } // Search the left half return - 1; } // Target not found

Searching Algorithms:

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; }