Skip to content

Commit d6b918c

Browse files
committed
search sorting program
1 parent 18ae8ef commit d6b918c

File tree

2 files changed

+285
-1
lines changed

2 files changed

+285
-1
lines changed

advance.py

Lines changed: 36 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,4 +18,39 @@
1818

1919
# nums=[1,7,10,23]
2020
# for x in nums:
21-
# print(x)
21+
# print(x)
22+
23+
# What is the value of g(728) for the function below?
24+
# def g(y):
25+
# b = 0
26+
# while y >= 3:
27+
# (y,b) = (y/3,b+1)
28+
# return(b)
29+
#
30+
# print(g(728))
31+
32+
# What is f(90)-f(89), given the definition of f below?
33+
# def f(n):
34+
# s = 0
35+
# for i in range(2,n):
36+
# if n%i == 0 and i%2 == 1:
37+
# s = s+1
38+
# return(s)
39+
# print(f(90)-f(89))
40+
41+
# The function h(n) given above returns False for a positive number n if and only if:
42+
# def h(n):
43+
# s = True
44+
# for i in range(1,n+1):
45+
# if i*i == n:
46+
# s = False
47+
# return(s)
48+
# print(h(9))
49+
# n is a perfect square.
50+
51+
# def foo(m):
52+
# if m == 0:
53+
# return(0)
54+
# else:
55+
# return(m+foo(m-1))
56+
# The function terminates for non negative n with foo(n) = n(n+1)/2

search.py

Lines changed: 249 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,249 @@
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+
# class Node:
117+
# def __init__(self,data):
118+
# self.data=data
119+
# self.next=None
120+
121+
# class Sll:
122+
# def __init__(self):
123+
# self.head=None
124+
125+
# def tranversal(self):
126+
# if self.head is None:
127+
# print("linked list is empty")
128+
# else:
129+
# a=self.head
130+
# while a is not None:
131+
# print(a.data,end=" ")
132+
# a=a.next
133+
134+
# n1=Node(5)
135+
# sll=Sll()
136+
# sll.head=n1
137+
# n2=Node(10)
138+
# n1.next=n2
139+
# n3=Node(15)
140+
# n2.next=n3
141+
# n4=Node(20)
142+
# n3.next=n4
143+
# sll.tranversal()
144+
145+
# def search(arr,v):
146+
# l=len(arr)
147+
# for i in range(l):
148+
# if arr[i]==v:
149+
# return i
150+
# else:
151+
# return -1
152+
153+
# def search(arr,l,r,v):
154+
# while l<=r:
155+
# mid=l+(r-1)//2
156+
# if arr[mid]==v:
157+
# return mid
158+
# elif arr[mid]<r:
159+
# l=mid+1
160+
# else:
161+
# r=mid-1
162+
# return -1
163+
164+
165+
# def search(arr,l,r,v):
166+
# if l<=r:
167+
# mid=l+(r-1)//2
168+
# if arr[mid]==v:
169+
# return mid
170+
# elif arr[mid]>r:
171+
# return search(arr,l,mid-1,v)
172+
# else:
173+
# return search(arr,mid+1,r,v)
174+
# return -1
175+
176+
177+
178+
# arr=[int(x)for x in input('array').split(",")]
179+
# v=int(input())
180+
# result=search(arr,0,len(arr),v)
181+
# print(result)
182+
183+
# def partition(data,l,r):
184+
# pivot=data[r]
185+
# i=l-1#0
186+
# for j in range(l,r):
187+
# if data[j]<=pivot:
188+
# i=i+1
189+
# (data[i],data[j])=(data[j],data[i])
190+
# (data[i+1],data[r])=(data[r],data[i+1])
191+
# return i+1
192+
193+
194+
195+
# def quickSort(data,l,r):
196+
# if l<r:
197+
# pi=partition(data,l,r)
198+
# quickSort(data,l,pi-1)
199+
# quickSort(data,pi+1,r)
200+
201+
202+
203+
# data=[1,7,4,1,10,9,-2]
204+
# print("Unsorted Array")
205+
# print(data)
206+
# r=len(data)-1
207+
# print(r)
208+
# quickSort(data,0,r)
209+
# print('Sorted Array in Ascending Order:')
210+
# print(data)
211+
212+
213+
# Approach 2: Quicksort using list comprehension
214+
215+
# def quicksort(arr):
216+
# if len(arr) <= 1:
217+
# return arr
218+
# else:
219+
# pivot = arr[0]
220+
# left = [x for x in arr[1:] if x < pivot]
221+
# right = [x for x in arr[1:] if x >= pivot]
222+
# return quicksort(left) + [pivot] + quicksort(right)
223+
224+
# # Example usage
225+
# arr = [1, 7, 4, 1, 10, 9, -2]
226+
# sorted_arr = quicksort(arr)
227+
# 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+

0 commit comments

Comments
 (0)