top of page
  • Subham Das

Insertion sort iterates, consuming one input element each repetition, and growing a sorted output list. At each iteration, insertion sort removes one element from the input data, finds the location it belongs within the sorted list, and inserts it there. It repeats until no input elements remain.

Sorting is typically done in-place, by iterating up the array, growing the sorted list behind it. At each array-position, it checks the value there against the largest value in the sorted list (which happens to be next to it, in the previous array-position checked). If larger, it leaves the element in place and moves to the next. If smaller, it finds the correct position within the sorted list, shifts all the larger values up to make a space, and inserts into that correct position.



# Program for Insertion Sort

a=[16,78,42,3,90,56,34]

for i in a :

j = a.index(i)

for j in range(j,0,-1) :

if a[j-1] > a[j] :

a[j-1],a[j] = a[j],a[j-1]

else :

break

j = j-1

print(a)






8 views0 comments
  • Subham Das

If a list of elements or records are in sorted in ascending order the Binary Search algorithm is applicable. Its is faster than Insertion search. In this case three index value is considered with assigning the first or the lower value as 0 and high or the last value as length of that list. And the adding the index of lower and higher divided by 2 (float division), we get mid index.


Binary search compares the target value to the middle element of the array. If they are not equal, the half in which the target cannot lie is eliminated and the search continues on the remaining half, again taking the middle element to compare to the target value, and repeating this until the target value is found. If the search ends with the remaining half being empty, the target is not in the array.



# Program for Binary Search.

# You can take list from user or assign a list but remember the list should be sorted.


list = [2,5,8,12,16,23,38,56,72,91]

# Hereke input from user of the searching element.


searchfor = eval(input("Enter the element for search:"))

flag = 0

low = 0

high = len(list)-1

while low<= high :

mid = (low+high)//2

if searchfor == list[mid] :

flag = 1

# Here position variable will help us to tell the the position of the element in the list.

position = mid+1

# Here break statement help to jump out of loop it the condition satisfies.

break

else :

if searchfor > list[mid] :

low = mid+1

else:

high = mid-1

if flag == 1 :

print(searchfor,"is in the List in postion:",position)

else :

print(searchfor,"is not in the List")












37 views0 comments
  • Subham Das

In python string is type(class) that is a sequence of character enclosed in double quotes(" "). It is immutable i.e. a string cannot be changed. A string is a data type used in programming, such as an integer and floating point unit, but is used to represent text rather than numbers. It is comprised of a set of characters that can also contain spaces and numbers. For example, the word "hamburger" and the phrase "I ate 3 hamburgers" are both strings. Even "12345" could be considered a string, if specified correctly. Typically, programmers must enclose strings in quotation marks for the data to recognized as a string and not a number or variable name.


Index operator in String


: , [ ]

The above two operator are prime operator for string indexing. String indexing is a process by which string members can be accessed by specifying the respective position of members by the index.This is specifying of index by positive integer.


Function of string in Python



14 views0 comments
logo12.PNG
bottom of page