Fundamental of Data Structure - Ms.S.D.Patil
Fundamental of Data Structure - Ms.S.D.Patil
Structure
-Ms.S.D.Patil
Syllabus
SR. NO TOPIC
Unit 5 Stack
Unit 6 Queue
Searching
Searching :
Searching is a techniques of finding an element in a given list of elements.
• Searching techniques
1. Linear Search
2. Binary Search
3. Fibonacci Search
4. Index sequential search
Linear Search :
• The linear search is a sequential search. It compares each
element with the value being searched for.
• In the sequential search, we start searching for the key
element at the beginning of the list and continue until we find
the key element.
List1 = [ 34,23,5,6,7,1,22,8]
print(List1)
Key = int(input(“enter the key element:”))
for i in range (len(List1)):
if key == List1[i]:
print(“key element is found :”)
break
else:
print(“element is not found”)
PROF.SHOBHA PATIL,JSPM NTC
Linear search advantages
• Easy to understand.
• It operates on both sorted and unsorted list
• It doest not require array/list to be in order
• Easy to implement
Easy to understand and easy to implement Binary search is more complex than linear
search.
• Internal sorting: If the input data is such that it can be adjusted in the
main memory at once, it is called internal sorting.
• External sorting: If the input data is such that it cannot be adjusted in the
memory entirely at once, it needs to be stored in a hard disk, floppy disk,
or any other storage device. This is called external sorting.
Starting from the first index, compare the first and the second elements.If
the first element is greater than the second element, they are swapped.
Now, compare the second and the third elements. Swap them if they are
not in order.
• Bubble sort starts with very first two elements, comparing them to check which one
is greater.
• In this case, value 33 is greater than 14, so it is already in sorted locations. Next,
we compare 33 with 27.
• We find that 27 is smaller than 33 and these two values must be swapped . The
new array should look like this .
• We know then that 10 is smaller 35. We swap these values. We find that we have
reached the end of the array. After one iteration, the array should look like this
• Next, we compare 27 with 33. In this case, value 33 is greater than 27, so it is
already in sorted locations
• Next, we compare 33 with 10. In this case, value 10 is smaller than 33, so we swap
these values
• Next, we compare 33 with 35. In this case, value 35 is greater than 33, so it is already in sorted
locations. We find that we have reached the end of the array. After Second iteration, the array
should look like this
• Bubble sort starts with very first two elements, comparing 10 and 14 it is already in sorted locations
10 14 27 33 19 35 42 44
10 14 27 19 33 35 42 44
10 14 19 27 33 35 42 44
PROF.SHOBHA PATIL,JSPM NTC
Insertion algorithm
1. If it is the first element, it is already sorted.
mark first element as sorted;
2. Pick next element
3. Compare with all elements in the sorted sub-
list
4. Shift all the elements in the sorted sub-list
that is greater than the value to be sorted
5. Insert the value
6. Repeat until list is sorted
PROF.SHOBHA PATIL,JSPM NTC
Analysis of Insertion Sort
we search the whole list and find that 10 is the lowest value.
So we swap10 with leftmost element 14. so after one iteration 10 is appears in the first
position of the sorted list.
We find that 14 is lowest value in the unsorted list. So we swap14 with leftmost
element of unsorted list 33.
Aftertwo iterations, two least values are positioned at the beginning in a sorted sub-list.
The same process is applied to the rest of the items in the array.
• Merge sort first divides the array into equal halves unless the
atomic values are achieved and then combines them in a
sorted manner.
We further divide these arrays and we achieve atomic value which can no more be divided.
Now, we combine them in exactly the same manner as they were broken down
In the next iteration of the combining phase, we compare lists of two data values, and
merge them into a list of found data values placing all in a sorted order.
After the final merging, the list should look like this −
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 until
single sorted list is obtained
• Pivot :- You can choose any element from the array as the
pivot element.
quickSort(left, right)
if right-left <= 0
return
else pivot = A[right]
partition = partitionFunc(left, right, pivot)
quickSort(left,partition-1)
quickSort(partition+1,right)
end if
end procedure
• Radix sort
• Bucket Sort
• Complexity = O(m.n)
Where
• m = number of digits in the maximum number in the given
list.
• n = number of data element in list.
100 to 200 201 to 300 301 to 400 401 to 500 501to 600 601 to 700 7001to 800 801 to 900 901to 1000
• Visit the bucket in order & put all element into sequence
• 121,179,235,327,355,973