File tree Expand file tree Collapse file tree 2 files changed +59
-0
lines changed Expand file tree Collapse file tree 2 files changed +59
-0
lines changed Original file line number Diff line number Diff line change
1
+ # Simple swapping of array elements using two pointer method
2
+
3
+ def reverse (arr ):
4
+ start = 0
5
+ end = len (arr ) - 1
6
+ temp = 0
7
+
8
+ while ( start < end ):
9
+ temp = arr [start ]
10
+ arr [start ]= arr [end ]
11
+ arr [end ]= temp
12
+ start += 1
13
+ end -= 1
14
+
15
+ # User input starts
16
+ def userInput ():
17
+ arr = []
18
+ n = int (input ("Enter number of elements:" ))
19
+ print ("Enter the elements" )
20
+ for i in range (0 ,n ):
21
+ element = int (input ())
22
+ arr .append (element )
23
+
24
+ print ("Array before reversing:" , arr )
25
+ reverse (arr )
26
+ print ("Array after reversing:" , arr )
27
+
28
+ userInput ()
Original file line number Diff line number Diff line change
1
+ def binarySearch (arr ,target ):
2
+ start = 0
3
+ end = len (arr ) - 1
4
+
5
+ while ( start <= end ):
6
+ mid = start + ( end - start ) // 2
7
+ if (target < arr [mid ]):
8
+ end = mid - 1
9
+ elif (target > arr [mid ]):
10
+ start = mid + 1
11
+ else :
12
+ return mid
13
+ return - 1
14
+
15
+ def userInput ():
16
+ arr = []
17
+ n = int (input ("Enter number of elements: " ))
18
+ print ("Enter the elements" )
19
+ for i in range (0 ,n ):
20
+ element = int (input ())
21
+ arr .append (element )
22
+ print (arr )
23
+ target = int (input ("Enter the target element: " ))
24
+
25
+ result = binarySearch (arr ,target )
26
+ if (result == - 1 ):
27
+ print ("Element not found" )
28
+ else :
29
+ print ("The element was found at index " , result )
30
+
31
+ userInput ()
You can’t perform that action at this time.
0 commit comments