Chapter-2 - Array Searching and Sorting
Chapter-2 - Array Searching and Sorting
ENGINEERING
DEPARTMENT OF COMPUTER
SCIENCE AND ENGG.
Bachelor of Engineering (Computer Science & Engineering)
DATA STRUCTURES
• Searching
• Linear Search
• Binary Search
•Sorting
•Bubble Sort
•Insertion Sort
•Selection Sort
2
Searching
3
Search Element using Linear Search
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
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
13
Algorithm
14
Selection Sort
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