0% found this document useful (0 votes)
0 views

Chapter 5 searching and sorting

Uploaded by

prashantkori
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
0 views

Chapter 5 searching and sorting

Uploaded by

prashantkori
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 23

CHAPTER 5:- SEARCHING AND SORTING

Introduction:
● searching is an operation which finds the place of a given element in the
list/array.

● Search can be a process of locating a record with particular key value.


Types of searching:

● Internal searching: in which all elements remain constantly in main memory

is called as “Internal search”.

● External Searching: in which elements are kept on secondary storage is

called as “External search”


Linera Search / Sequential Search:

● Linear search is a type of sequential searching algorithm. In this method,

every element within the input array is traversed and compared with the key

element to be found. If a match is found in the array the search is said to be

successful; if there is no match found the search is said to be unsuccessful


Algorithm for Linera Search / Sequential Search:

Step 1: start

Step 2: set i to 0

step 3: if i > n then go step 8

step 4: if A[i] = x then go to step 7

step 5: set i to i+1

step 6: go to step 3

step 7: print element x found at

index i and go to step 9


step 8: print element not found

step 9: exit
Binary Search:
● Binary search is a search algorithm used to find the position of a target
value within a sorted array.
● It works by repeatedly dividing the search interval in half until the target
value is found or the interval is empty.
● The search interval is halved by comparing the target element with the
middle value of the search space.
Algorithm for Binary Search:
Step 1: Initialize low = 0, high=n-1
step 2: while low < = high
step 3: mid = low + high /2
step 4: if a[mid] = = item
step 5: set pos = mid
step 6: break and jump to step 10
step 7: else if item < a[mid]
step 8: high = mid - 1
step 9: else low = mid + 1
step 10: if pos < 0
step 11: print “ element is not found”
step 12: else print pos
Interpolation Search:
● Interpolation Search algorithm is used to search for a value in an ordered,
uniformly distributed array of elements.
● Interpolation Search will go to different locations to start with, depending if
the searched value is closer to the end or the start of the array, unlike Binary
Search that is always looking for the middle.
● This technique is trying to find the exact location of the value, not the
middle, using the interpolation formula.

// The idea of formula is to return higher value of pos


// when element to be searched is closer to arr[hi]. And
// smaller value when closer to arr[lo]
arr[] ==> Array where elements need to be searched
x ==> Element to be search
lo ==> Starting index in arr[]
hi ==> Ending index in arr[]
Algorithm for Interpolation Search:

1. Start searching data from middle of the list.

2. If it is a match, return the index of the item, and exit.

3. If it is not a match, probe position.

4. Divide the list using probing formula and find the new middle.

5. If data is greater than middle, search in higher sub-list.

6. If data is smaller than middle, search in lower sub-list.

7. Repeat until match.


Interpolation Search:
Sorting:

Sorting Concepts:

🞆 Data can be arranged either in ascending order or in descending

order which is called sort order.

Example: 30 36 50 49 22

Ascending order: 22 30 36 49 50

Descending order: 50 49 36 30 22
Sorting Techniques with Time complexity:
🞆 Sorting is a technique to rearrange the elements in ascending or
descending order, which can be numerical or any user defined
order.
🞆 Example: consider a telephone directory which consists of four
fields; phone number, name, address, pin code.
🞆 The sorting algorithm are divided into two categories:
⚫ Internal sorts: The method uses only the primary memory
during process. if all the data that is to be sorted can be
accommodated at a time in memory is called internal sorting.
⚫ Example: Bubble sort, Insertion sort, Quick sort.

⚫ External sorts: sorting large amount of data requires external or


secondary memory. This process uses external memory such as
Hard disk, Floppy Disk, magnetic tape etc.
⚫ Example: Merge sort.
1. Bubble Sort

🞆 The bubble sort was originally written to bubble the


highest element in the list.
🞆 To arrange the elements in the ascending order, we
compare each item in the list with the next item to it and
swapping them if required.
🞆 The algorithm repeats these comparison until n-1 passes
where n is number of input items.

🞆 For correct order , the larger values ‘bubble’ to the end of


the list, while smaller values sink towards the beginning
of the list.
3. Quick Sort

🞆 It is most popular and fastest sorting method .


🞆 It follows divide and conquer method.
🞆 Dividing array into sub array is partition exchange sort.
And also known as backbone of quick sort.

Main aim is how to partition array .


🞆 Elements less than the pivot element.
🞆 Pivot element(central element).
🞆 Elements greater than the pivot element.
Quick Sort Algorithm

1. Choose the highest index value has pivot

2. Take two variables to point left and right of the list

excluding pivot

3. Left points to the low index

4. Right points to the high

5. While value at left is less than pivot move right

6. While value at right is greater than pivot move left

7. If both step 5 and step 6 does not match swap left and right

8. If left ≥ right, the point where they met is new pivot


4. Merge Sort

🞆 Merging is the process of combining two or


more sorted data lists into a third list such
that it is also sorted.
🞆 Merge sort follows Divide and Conquer
strategy.
- Divide :- divide an n element sequence into
n/2 subsequence.
- Conquer :- sort the two sequences
recursively. - Combine :- merge the two
sorted sequence into a single sequence.
🞆 In this two list are compared and the
smallest element is stored in the third array.
Algorithm:-
Step 1: start
Step 2: initially the data is considered as a
single array of n element.
Step 3: divide the array into n/2 sub-array each
of length 2i (I is 0 for 0th iteration). i.e. array is
divided into n sub-arrays each of 1 element.
Step 4: merge two consecutive pairs of sub-
arrays such that the resulting sub-array is also
sorted.
Step 5: The sub-array having no pairs is carried
a sit is
Step 6: step 3 and 4 are repeated till there is
only one sub-array remaining of size n.
Step 7: stop
Time Complexity: In Merge Sort
Worst Case: O(Nlog N),
Average Case: O(Nlog N),
Best Case: O(Nlog N),

You might also like