1
- # Linear Search algorithm
2
- # Linear search is defined as the searching algorithm
3
- # where the list or data set is traversed from one end to find the desired value.
4
- # Time Complexity: The worst case time complexity of linear search is o(n), where n is the size of the array.
5
- # Space Complexity: Linear search requires a constant amount of memory to run.
6
- # Efficiency: Linear search is effeicient for small datasets but becomes inefficient for larger datasets.
7
- # In practice, linear search is often used as a subroutine in more complex algorithms.
8
- # Implementation: Linear search can be easily implemented using a loop,
9
- # with each iteration comparing the target value to the current element of the array.
10
- #
11
- # def search(arr, N, x):
12
- # for i in range(0, N):
13
- # if arr[i] == x:
14
- # return i
15
- # return -1
16
-
17
- # arr =[int(i) for i in input('Enter the array with comma').split(',')]
18
- # x = int(input('enter the number:'))
19
- # N = len(arr)
20
- # result = search(arr, N, x)
21
- # if(result == -1):
22
- # print("Element is not present in array")
23
- # else:
24
- # print("Element is present at index", result)
25
-
26
- # Phonebook Search: Linear search can be used to search through a phonebook to find a person’s name, given their phone number.
27
- # Spell Checkers: The algorithm compares each word in the document to a dictionary of correctly spelled words until a match is found.
28
- # Finding Minimum and Maximum Values: Linear search can be used to find the minimum and maximum values in an array or list.
29
- # Searching through unsorted data: Linear search is useful for searching through unsorted data.
30
-
31
-
32
- #sentinellinear search
33
- # def sentinelLinearSearch(array, key):
34
- # last = array[len(array) - 1]
35
- # array[len(array) - 1] = key
36
- # i = 0
37
- # while array[i] != key:
38
- # i += 1
39
- # array[len(array) - 1] = last
40
- # if i < len(array) - 1 or last == key:
41
- # return i
42
- # else:
43
- # return -1
44
-
45
- # array = [1, 2, 3, 4, 5, 6, 7, 8, 9]
46
- # key = 5
47
- # index = sentinelLinearSearch(array, key)
48
- # if index == -1:
49
- # print(f"{key} is not found in the array: {array}")
50
- # else:
51
- # print(f"{key} is found at index {index} in the array: {array}")
52
-
53
-
54
-
55
- # Binary search
56
-
57
- # Divide the search space into two halves by finding the middle index “mid”.
58
- # Compare the middle element of the search space with the key.
59
- # If the key is found at middle element, the process is terminated.
60
- # If the key is not found at middle element, choose which half will be used as the next search space.
61
- # If the key is smaller than the middle element, then the left side is used for next search.
62
- # If the key is larger than the middle element, then the right side is used for next search.
63
- # This process is continued until the key is found or the total search space is exhausted.
64
-
65
- # Python3 code to implement iterative Binary search
66
- # It returns location of x in given array arr
67
-
68
-
69
- # def binarySearch(arr, l, r, x):
70
- # while l<=r:
71
- # mid=l+(r-1)//2
72
- # if arr[mid]==x:
73
- # return mid
74
- # elif arr[mid]<x:
75
- # l=mid+1
76
- # else:
77
- # r=mid-1
78
- # return -1
79
- # # If we reach here, then the element
80
- # # was not present
81
- # arr = [int(i) for i in input('enter array: ').split(",")]
82
- # x = 10
83
- # result = binarySearch(arr, 0, len(arr)-1, x)
84
- # if result != -1:
85
- # print("Element is present at index", result)
86
- # else:
87
- # print("Element is not present in array")
88
-
89
-
90
- # Implementation of Recursive Binary Search Algorithm:
91
-
92
- # def binary(arr,l,r,x):
93
- # if r>=l:
94
- # mid=l+(r-1)//2
95
- # if arr[mid]==x:
96
- # return mid
97
- # elif arr[mid]>x:
98
- # return binary(arr,l,mid-1,x)
99
- # else:
100
- # return binary(arr,mid+1,r,x)
101
- # return -1
102
- # arr = [int(i) for i in input('enter array: ').split(",")]
103
- # x = 10
104
- # result = binary(arr, 0, len(arr)-1, x)
105
- # if result != -1:
106
- # print("Element is present at index", result)
107
- # else:
108
- # print("Element is not present in array")
109
-
110
- # Time Complexity:
111
- # Best Case: O(1)
112
- # Average Case: O(log N)
113
- # Worst Case: O(log N)
114
- # Auxiliary Space: O(1), If the recursive call stack is considered then the auxiliary space will be O(logN).
115
-
116
1
# class Node:
117
2
# def __init__(self,data):
118
3
# self.data=data
225
110
# arr = [1, 7, 4, 1, 10, 9, -2]
226
111
# sorted_arr = quicksort(arr)
227
112
# print("Sorted Array in Ascending Order:")
228
- # print(sorted_arr)
229
-
230
-
231
- def insertionSort (arr ):
232
- n = len (arr ) # Get the length of the array
233
-
234
- if n <= 1 :
235
- return # If the array has 0 or 1 element, it is already sorted, so return
236
-
237
- for i in range (1 , n ): # Iterate over the array starting from the second element
238
- key = arr [i ] # Store the current element as the key to be inserted in the right position
239
- j = i - 1
240
- while j >= 0 and key < arr [j ]: # Move elements greater than key one position ahead
241
- arr [j + 1 ] = arr [j ] # Shift elements to the right
242
- j -= 1
243
- arr [j + 1 ] = key # Insert the key in the correct position
244
-
245
- # Sorting the array [12, 11, 13, 5, 6] using insertionSort
246
- arr = [12 , 11 , 13 , 5 , 6 ]
247
- insertionSort (arr )
248
- print (arr )
249
-
113
+ # print(sorted_arr)
0 commit comments