Sunteți pe pagina 1din 8

C & DS for I MCA Unit-6 (Searching and Sorting) VVIT

Searching
Searching refers to finding the location of a given element in a collection of elements.
The element for which the search is made is sometimes called as search key.

Searching Techniques:

We will now discuss two searching methods and analyze their performance they are
1) Linear Search
2) Binary Search

Linear Search (or) Sequential Search


This is the most natural searching method simply put it means to go through a list or a file
till the required record is found.

Algorithm:

Input: List of size N, Target value T.


Output: Position of T in the list, I
Begin
set found to false
set I to 0
while (I <= N) and (Found is false)
if list[I] = T
found = True
else
I=I+1
If found is false
T is not present in the list.
End.

Analysis:
• The fewest possible comparisons = 1, if the required item is the first item.
• The maximum comparisons = N when the required item is the last item in the list.
• Thus if the required item is in position I in the list, I comparisons are required.
• Hence the average no of comparisons done by sequential search is

= (1 + 2 + 3 + ---- + I + ---- + N)
N
= N (N + 1)
2*N

Staff: Janaki Ram Dept of IT Page:1


C & DS for I MCA Unit-6 (Searching and Sorting) VVIT

= N+1
2

Binary Search:
• The drawbacks of sequential search can be eliminated if it becomes possible to eliminate
large portions of the list from consideration in subsequent iterations.
• The binary search method just that, it halves the size of the list to search in each iteration.
• Binary Search requires sorted data.

Algorithm:
Input : Sorted data of size N, Target value T
Output : Position of T in the list = T
Begin
High = N
Low = 1
Found = false
While (Found is false and low <= High)
Mid = (low + high) / 2
If T == List[mid]
I = mid
Found = True
Else if T < List [mid]
High = mid –1
Else
Low = mid + 1
End.
Analysis:
In general, the binary search method requires no more than [log2N]+1 comparisons.

Note: Please refer Lab Exercise 19 for implementations of Linear and Binary Search

Sorting

Sorting refers to the operation of arranging data in some order, such as increasing or
decreasing with numerical data, or alphabetically (lexicographically) with character data.
A list having a single element is trivially sorted.

Staff: Janaki Ram Dept of IT Page:2


C & DS for I MCA Unit-6 (Searching and Sorting) VVIT

Selection Sort:
Selection sort is a simple sorting algorithm in which after each pass the smallest elements
positions are fixed from left to right (assuming ascending order sort). Selection sort is noted for
its simplicity, and also has performance advantages over more complicated algorithms in certain
situations.

Steps of Selection Sort Algorithm:


1. Find the minimum value in the list
2. Swap it with the value in the first position
3. Repeat the steps above for the remainder of the list (starting at the second position and
advancing each time)

Illustration:

Consider the following unsorted list

64 25 12 22 11

Pass 1: Find the smallest element and fix it in first position.

11 25 12 22 64

After Pass 2: Find the second smallest element and fix it in second position.

11 12 25 22 64

After Pass 3:

11 12 22 25 64

After Pass 4:

11 12 22 25 64

Therefore the sorted list is:

11 12 22 25 64

Note:
• To sort a list of n elements, selection sort requires n-1 passes.
• It requires (n − 1) + (n − 2) + ... + 2 + 1 = n(n − 1) / 2 ≈ Θ(n2) comparisons

Note: Selection Sort is implemented as a part of Binary Search in Lab Exercise 19.

Staff: Janaki Ram Dept of IT Page:3


C & DS for I MCA Unit-6 (Searching and Sorting) VVIT

Bubble Sort:

Bubble sort is a simple sorting algorithm in which larger elements are pushed to the end
to the end of the list. The algorithm gets its name from the way larger elements "bubble" to the
end of the list.

Steps:
1. Move left right on the list, comparing each pair of adjacent items.
2. If the adjacent items are in proper order, then leave them as it is, if not swap them
3. The pass through the list is repeated until no swaps are needed, which indicates that the
list is sorted.
Illustration:

Consider the unsorted list:


51428

First Pass: Get the greatest element to the end of the list.

( 5 1 4 2 8 )  ( 1 5 4 2 8 ), Here, algorithm compares the first two elements, and swaps them.
( 1 5 4 2 8 )  ( 1 4 5 2 8 ), Swap since 5 > 4
( 1 4 5 2 8 )  ( 1 4 2 5 8 ), Swap since 5 > 2
( 1 4 2 5 8 )  ( 1 4 2 5 8 ), Now, since these elements are in order (8 > 5), do not swap them.

14258

After Second Pass:


12458

After Third Pass:


12458

After Fourth Pass:


12458

Therefore the sorted list is:


12458

Note: Bubble sort requires n-1 passes and the complexity is О(n²).

Note: Refer Lab Exercise 20 for bubble sort.

Staff: Janaki Ram Dept of IT Page:4


C & DS for I MCA Unit-6 (Searching and Sorting) VVIT

Insertion Sort:

Insertion sort is a simple sorting algorithm in which the sorted array (or list) is built one
entry at a time. The basic idea is to insert an element into a sorted list at appropriate position and
continuing the same for rest of elements of unsorted list.

Steps of Insertion Sort:


1. Data elements are grouped into two sections: a sorted section (consider first element of
unsorted list as sorted list) and an un-sorted section.
2. Take an element from the un-sorted section.
3. Insert the element into the sorted section at the correct position based on the value. Move
all the elements greater than inserting element towards right and then insert the element.
4. Repeat step 2 and 3 until no more elements left in the un-sorted section.

The idea of insertion sort comes from our daily life experiences. For example, when you play
cards with your friends, you will insert the next card you pick into the sorted cards in your hand.

Illustration:

Consider the unsorted list:


32 10 4 8 12 23
Assume the list 32 has a sorted list (as it has only one element).

Pass 1: Insert 10 from the unsorted list into sorted list 32.

32 10 4 8 12 23

10 32 4 8 12 23

Pass 2: Insert 4 from unsorted list into sorted list 10, 32

10 32 4 8 12 23

4 10 32 8 12 23

Pass 3: Insert 8 from unsorted list into sorted list 4, 10, 32

4 10 32 8 12 23

4 8 10 32 12 23

Pass 4: Insert 12 from unsorted list into sorted list 4, 8, 10, 32

4 8 10 32 12 23

4 8 10 12 32 23

Staff: Janaki Ram Dept of IT Page:5


C & DS for I MCA Unit-6 (Searching and Sorting) VVIT

Pass 5: Insert 23 from unsorted list into sorted list 4, 8, 10, 12, 32

4 8 10 12 32 23

4 8 10 12 23 32

Therefore sorted list is:

4 8 10 12 23 32
Note: Insertion sort need n-1 passes and the complexity is O(n2)

Please refer to lab exercise 21

Quick Sort:

Quicksort is a well-known sorting algorithm. It works on the principle divide and conquer.
Typically, quicksort is significantly faster in practice than other algorithms, because its inner
loop can be efficiently implemented on most architecture, and in most real-world data.
Basic idea is randomly select an element called pivot, fix it at its position in the final
sorted list and apply the quick sort on two unsorted list before and after it.

Steps:
1. Pick an element, called a pivot, from the list.
2. Reorder the list so that all elements which are less than the pivot come before the pivot and
so that all elements greater than the pivot come after it (equal values can go either way).
After this partitioning, the pivot is in its final position. This is called the partition operation.
3. Recursively sort the sub-list of lesser elements and the sub-list of greater elements.

The base cases (terminating condition) of the recursion are lists of size zero or one, which are
always sorted.

Illustration:

Consider an unsorted list

73925

Select 7 as pivot. Now adjust the list such that all small elements than 7 are before 7 and greater
are placed after 7.

73925 start comparing from the last element, since 5 is less than 7 swap.

53927 Now compare from left and keep moving until a greater element is found.

53729 Now again from compare from right, swap 2 and 7.

Staff: Janaki Ram Dept of IT Page:6


C & DS for I MCA Unit-6 (Searching and Sorting) VVIT

53279 Now again from left, and we find that all elements before 7 are less than it.
This fixes the position of 7 and leaves 2 unsorted lists 5 3 2 and 9. Since list 9 is a singleton set
no need to sort it further.

Apply quick sort on the list 5 3 2. Select 5 as pivot.

53279 Start comparing from right. i.e. from 2 of list 5 3 2. Swap 5 and 2.

23579 Now from left, since all elements are less than 5, we can fix it.

Now apply the algorithm on the left out list 2 3. Select 2 as pivot.

23579 Compare from right end to find 3 is rightly placed after 2. Fix 2.

We are now left with the list “3” (rest all are fixed in proper positions). Since it has only one
element we need not sort it.

Therefore the sorted list is:

23579

Please refer to lab exercise 20

Merge Sort

The basic idea behind the merge sort is to merge two sorted lists into a single sorted list.

Merging

1. Start scanning both sorted lists from left to right.


2. Select the smaller of both first elements in both lists; add it as first element to third list.
3. Now move the step forward in the list from which the element is deleted. Do not move
forward in the other list.
4. Now compare the first element of one list with second of other list and add the smallest to
third list.
5. Continue this process until all elements of the both lists.
6. If one of the lists has reached its end and other still has some elements, just add all these
elements to the third list.
7. Finally the third list will have the sorted merged sorted list.

Steps for applying merge sort on a single unsorted list:

1. If the list is of length 0 or 1, then it is already sorted. Otherwise:


2. Divide the unsorted list into two sublists of about half the size.
3. Sort each sublist recursively by re-applying merge sort.
4. Merge the two sublists back into one sorted list.

Staff: Janaki Ram Dept of IT Page:7


C & DS for I MCA Unit-6 (Searching and Sorting) VVIT

In effect, we break the list into different lists of one element each and merge those lists
separately to get sorted 2 (or 1) element lists, again merge those get sorted 4 (or 3) element lists
and so on until we get a single sorted list.

Merge sort incorporates two main ideas to improve its runtime:


→ A small list will take fewer steps to sort than a large list.
→ Fewer steps are required to construct a sorted list from two sorted lists than two unsorted
lists. You only have to traverse each list once if they're already sorted.

Illustration:

Consider the single unsorted list

85 24 63 45 17 31 96

Break the list into 7 sorted single element lists.

85 24 63 45 17 31 96

Now merge pairs of lists separately.

24 85 45 63 17 31 96

We are left with 4 sorted lists; merge 2 pairs to get 2 sorted lists.

24 45 63 85 17 31 96

We now left with only 2 sorted lists, merge them to get the final sorted list.

17 24 31 45 63 85 96

Note: Please refer to Lab Exercise 21 (b) for Merge Sort implementation.

Staff: Janaki Ram Dept of IT Page:8

S-ar putea să vă placă și