Elementary Searching and Sorting Algorithms - Prof. Gebremichael, Study notes of Data Structures and Algorithms

An overview of elementary searching and sorting algorithms, including sequential search, binary search, simple sort, bubble sort, selection sort, and insertion sort. It explains the basic principles, implementations, and big o notation for each algorithm. The document also includes examples and assignments to help understand the concepts. This material is suitable for students learning introductory data structures and algorithms, offering a clear and concise introduction to fundamental concepts.

Typology: Study notes

2024/2025

Uploaded on 05/17/2025

afendi-mohammed
afendi-mohammed 🇪🇹

8 documents

1 / 29

Toggle sidebar

This page cannot be seen from the preview

Don't miss anything!

bg1
Data Structure and
Algorithms
Chapter Two:
Elementary Searching and Sorting
Algorithms
pf3
pf4
pf5
pf8
pf9
pfa
pfd
pfe
pff
pf12
pf13
pf14
pf15
pf16
pf17
pf18
pf19
pf1a
pf1b
pf1c
pf1d

Partial preview of the text

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

Data Structure and

Algorithms

Chapter Two:

Elementary Searching and Sorting

Algorithms

Chapter Two: Elementary

Searching and Sorting Algorithms

This Chapter Covers:

Sequential search Binary search Simple sort Bubble sort Selection sort Insertion sort

Simple searching Algorithm

Searching is a process of finding an element from

a collection of elements.

Searching algorithms to be considered:

Sequential search Binary search

Sequential Searching

It is natural way of searching The algorithm does not assume that the data is sorted. Algorithm: Search list from the beginning until key is found or end of list reached. Implementation: int i; bool found =false; for(i=0;i<n;i++;) if (DataElement[i]==key) { found=true; break; } return (found);

Binary Searching

Algorithm:

  1. Divide the list into halve
  2. see which half of the list the key belongs
  3. repeat 1 and 2 until only one element remains
  4. if the remaining element is equal to search key then found =true else key not found. 5. But input data has to be sorted first.

Implementation: int Top =n-1, Bottom=0;middle; bool found=false; while (Top>bottom) { Middle=(Top+Bottom)/2; if (dataElement[middle] == key){ found =true; break; } if( dataElement[middle]<key) Bottom=middle+1; else Top=middle; } return found;

Simple (Elementary) Sorting

Algorithms

For searching, sorting is important

Take as an example a telephone directory

Efficiency of the algorithm should be

assessed

Sorting algorithms commonly consist of two

types of operation: comparisons and data

movements

A comparison is simply comparing one

value in a list with another, and a data

movement (swapping) is an assignment.

Sorting algorithms to be considered:

Simple Sort Algorithm

In simple sort algorithm, the first element is compared with the second, third, and all subsequent elements. If any of these is less than the current first element then the first element is swapped with that element. The above step is repeated with the second, third and all other subsequent elements.

Example

i j (pass) Data elements Remarks

0 1 2,4,3,1 The smallest element

is placed in its

proper position

1 2 1,3,4,2 The second smallest

element is placed

in its proper place.

2 3 1,2,3,4 All the data elements

are sorted at the n-

1 path

footer 13

Algorithm Analysis

Analysis involves number of comparisons

and number of swaps.

Iteration No. of comparison No. of swaps (compares) 1 st pass n-1 n- 2 nd pass n-2 n-

... ... ... (n-1) th 1 1 Total n(n-1)/2 n(n-1)/ Therefore, Big-O of simple sort algorithm is n(n-1)/2+n(n-1)/2=O(n 2 )

Example data element=

Selection Sort Algorithm

It is in many ways similar to both simple and

bubble algorithm.

Rather than swapping the neighbors continuously

as the algorithm traverses the sub array to be

sorted, as done in the bubble sort case, this

algorithm finds the minimum element of the sub

array and swaps it with the pivot elements.

Implementation

for (i=0; i<=n-2;i++) { min_index=i; for (j=(i+1); j<=n-1;j++) if (dataElement[j] <= dataElement[min_index]) min_index=j; swap (dataElement[i],dataElement[min_index]); }