0% found this document useful (0 votes)
598 views21 pages

Chapter-2 - Array Searching and Sorting

This document contains information about various data structure algorithms taught as part of a Bachelor of Engineering degree in Computer Science and Engineering. It discusses searching algorithms like linear search and binary search. It also discusses sorting algorithms like bubble sort, insertion sort, selection sort, quick sort, and merge sort. For each algorithm, it provides details on the approach, pseudocode, examples, and references for further reading.

Uploaded by

Kartik Tyagi
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
Download as pptx, pdf, or txt
0% found this document useful (0 votes)
598 views21 pages

Chapter-2 - Array Searching and Sorting

This document contains information about various data structure algorithms taught as part of a Bachelor of Engineering degree in Computer Science and Engineering. It discusses searching algorithms like linear search and binary search. It also discusses sorting algorithms like bubble sort, insertion sort, selection sort, quick sort, and merge sort. For each algorithm, it provides details on the approach, pseudocode, examples, and references for further reading.

Uploaded by

Kartik Tyagi
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
Download as pptx, pdf, or txt
Download as pptx, pdf, or txt
You are on page 1/ 21

UNIVERSITY INSTITUTE OF

ENGINEERING
DEPARTMENT OF COMPUTER
SCIENCE AND ENGG.
Bachelor of Engineering (Computer Science & Engineering)
DATA STRUCTURES

ARRAY IN DATA STRUCTURE DISCOVER . LEARN . EMPOWER


Agenda

• Searching
• Linear Search
• Binary Search
•Sorting
•Bubble Sort
•Insertion Sort
•Selection Sort

2
Searching

Searching Algorithms are designed to check for an element or retrieve


an element from any data structure where it is stored. Based on the type
of search operation, these algorithms are generally classified into two
categories:
Sequential Search: In this, the list or array is traversed sequentially and
every element is checked. For example: Linear Search.
Interval Search: These algorithms are specifically designed for
searching in sorted data-structures. These type of searching algorithms
are much more efficient than Linear Search as they repeatedly target the
center of the search structure and divide the search space in half. For
Example: Binary Search.

3
Search Element using Linear Search

A simple approach is to do linear search, i.e


• Start from the leftmost element of arr[] and one by one compare x with
each element of arr[]
• If x matches with an element, return the index.
• If x doesn’t match with any of elements, return -1.

4
Linear Search
LINEAR(DATA, N, ITEM, LOC)
Here DATA is a linear array with N elements, and ITEM is a given item of information.
This algorithm finds the location LOC of ITEM in DATA, or sets LOC = 0 if the search
is unsuccessful.
1. [Insert ITEM at the end of DATA] Set DATA[N+1] = ITEM
2. [Initialize counter] Set LOC = 1
3. [Search for ITEM]
Repeat while DATA[LOC] ≠ ITEM
Set LOC = LOC + 1
[End of loop]
4. [Successful?] If LOC = N+1, then: Set Loc = 0
5. Exit

5
Binary Search

•Search a sorted array by repeatedly dividing the search interval in half.


•Begin with an interval covering the whole array.
•If the value of the search key is less than the item in the middle of the
interval, narrow the interval to the lower half.
•Otherwise narrow it to the upper half. Repeatedly check until the value
is found or the interval is empty.

6
Binary Search Algorithm
• Binary search algorithm is efficient if the array is sorted.
• A binary search is used whenever the list starts to become large.
• Consider to use binary searches whenever the list contains more than 16
elements.
• The binary search starts by testing the data in the element at the middle of
the array to determine if the target is in the first or second half of the list.
• If it is in the first half, we do not need to check the second half. If it is in
the second half, we do not need to test the first half. In other words we
eliminate half the list from further consideration. We repeat this process
until we find the target or determine that it is not in the list.

7
Example

8
Binary Search Algorithm
1. [Define variables]
ST = LB, LAST= UB;
MID = (ST+LAST)/2;
Repeat 3 and 4 DO ST <= LAST & DATA[MID] != ITEM
3. If ITEM < DATA[MID] then
LAST = MID-1
If not
ST = MID+1
4. Set MID = INT((ST + LAST)/2)
[LAST repeat to 2]
5. If DATA[MID] = ITEM then
LOK = MID
If not,
LOK = NULL
6. Stop
9
Sorting
The process of arranging items systematically
Bubble Sort : Exchange two adjacent elements if they are out of order. Repeat
until array is sorted.
Insertion sort: Scan successive elements for an out-of-order item, then insert the
item in the proper place.
Selection sort: Find the smallest (or biggest) element in the array, and put it in
the proper place. Swap it with the value in the first position. Repeat until array is
sorted.
Quick sort: Partition the array into two segments. In the first segment, all
elements are less than or equal to the pivot value. In the second segment, all
elements are greater than or equal to the pivot value. Finally, sort the two
segments recursively.
Merge sort: Divide the list of elements in two parts, sort the two parts
individually and then merge it.
10
Step-by-step example
Take an array of numbers " 5 1 4 2 8", and sort the array from lowest number to greatest number using
bubble sort. In each step, elements written in bold are being compared. Three passes will be required;

11
Bubble Sort
BUBBLE(DATA, N)
Here DATA is an array with N elements. This algorithm sorts the elements in DATA.
1. Repeat steps 2 and 3 for K = 1 to N – 1
2. Set PTR = 1 [Initialize pass pointer PTR]
3. Repeat while PTR ≤ N – K [Executes pass]
a) If DATA[PTR] > DATA[PTR + 1], then
Interchange DATA[PTR] and DATA[PTR + 1]
[End of If structure]
b) Set PTR = PTR + 1
[End of inner loop]
[End of step 1 outer loop]
4. Exit

12
Insertion sort

Insertion sort is a simple sorting algorithm that works the way


we sort playing cards in our hands

13
Algorithm

14
Selection Sort

• Selection sort is a simple sorting algorithm. This sorting algorithm is


an in-place comparison-based algorithm in which the list is divided
into two parts, the sorted part at the left end and the unsorted part at
the right end. Initially, the sorted part is empty and the unsorted part is
the entire list.
• The smallest element is selected from the unsorted array and swapped
with the leftmost element, and that element becomes a part of the
sorted array. This process continues moving unsorted array boundary
by one element to the right.
• This algorithm is not suitable for large data sets as its average and
worst case complexities are of Ο(n2), where n is the number of items.

15
Example

16
Algorithm

17
Merge Sort
• Merge sort is a sorting technique based on divide and conquer technique.
With worst-case time complexity being Ο(n log n), it is one of the most
respected algorithms.
• Merge sort first divides the array into equal halves and then combines them
in a sorted manner.
Algorithm
Merge sort keeps on dividing the list into equal halves until it can no more be
divided. By definition, if it is only one element in the list, it is sorted. Then,
merge sort combines the smaller sorted lists keeping the new list sorted too.
Step 1 − if it is only one element in the list it is already sorted, return.
Step 2 − divide the list recursively into two halves until it can no more be
divided.
Step 3 − merge the smaller lists into new list in sorted order.
18
Example

19
References

• Lipschutz, Seymour, “Data Structures”, Schaum's Outline Series, Tata McGraw Hill.
• Goodrich, Michael T., Tamassia, Roberto, and Mount, David M., “Data Structures and Algorithms in C++”, Wiley Student
Edition.
• https://www.tutorialspoint.com/data_structures_algorithms/algorithms_basics.htm
• https://www.cs.utexas.edu/users/djimenez/utsa/cs1723/lecturehtml
• https://www.w3resource.com/php-exercises/searching-and-sorting-algorithm/searching-and-sorting-algorithm-exercise-4.
php
• https://www.programiz.com/dsa/merge-sort
• https://www.geeksforgeeks.org/binary-search/

20
THANK YOU

You might also like