Showcase your coding talent
Join the most intelligent community of young coders. Browse, save & play with codes to understand how they work, and code your own ideas to earn badges & scholarships.

Tekie.in

Bubble Sort

posted 8 days ago

utilities
mathematics
for loop

Bubble Sort is the simplest sorting algorithm that works by repeatedly swapping the adjacent elements if they are in wrong order.

'''

Bubble Sort is the simplest sorting algorithm that works by repeatedly swapping the adjacent elements if they are in wrong order.

Example: 

First Pass: 

( 5 1 4 2 8 ) –> ( 1 5 4 2 8 ), Here, algorithm compares the first two elements, and swaps since 5 > 1. 

( 1 5 4 2 8 ) –>  ( 1 4 5 2 8 ), Swap since 5 > 4 

( 1 4 5 2 8 ) –>  ( 1 4 2 5 8 ), Swap since 5 > 2 

( 1 4 2 5 8 ) –> ( 1 4 2 5 8 ), Now, since these elements are already in order (8 > 5), algorithm does not swap them.

Second Pass: 

( 1 4 2 5 8 ) –> ( 1 4 2 5 8 ) 

( 1 4 2 5 8 ) –> ( 1 2 4 5 8 ), Swap since 4 > 2 

( 1 2 4 5 8 ) –> ( 1 2 4 5 8 ) 

( 1 2 4 5 8 ) –>  ( 1 2 4 5 8 ) 

Now, the array is already sorted, but our algorithm does not know if it is completed. The algorithm needs one whole pass without any swap to know it is sorted.

Third Pass: 

( 1 2 4 5 8 ) –> ( 1 2 4 5 8 ) 

( 1 2 4 5 8 ) –> ( 1 2 4 5 8 ) 

( 1 2 4 5 8 ) –> ( 1 2 4 5 8 ) 

( 1 2 4 5 8 ) –> ( 1 2 4 5 8 )

'''

#declaring a list and count variable

list_ = []

count = 0



#getting values in list

for i in range(1,int(input('how many no. are in the list : '))+1):

    num = int(input(f'num{i} = '))

    list_.append(num)

print(f'\nList = {list_}')



#main function starts here

for i in range(len(list_)-1):

    flag = 0

    for j in range(len(list_)-i-1):

        temp = 0

        count += 1

        if list_[j] > list_[j+1]:

            temp = list_[j+1]

            list_[j+1] = list_[j]

            list_[j] = temp

            flag = 1

    if flag == 0:

        break

        

print(f'Sorted List = {list_} \nTurns = {count}\n')

Tekie.in

Fun with friends

posted 8 days ago

games
if-else

This is a happy brithday test for whoever likes football. Remove the 3rd question. Change the persons name to be whoever your friend is or 'wierdo'.

let=("Dear Vandit,\nThis is Chaitan here. Happy 9th birthday. May you have the best day ever! I hope you like this quiz. You are one of the best football players I'v ever met. You have a lot of potentail in football. And once again, Happy Birthday!\nChaitan")
#question 1
messi='messi'
ronaldo='ronaldo'
right_count=0
count_neg=0
question=input('Who do you like more messi or ronaldo,chose one!')
if question==messi:
 right_count=right_count+1
 print('good choice.')
 print(right_count)
if question==ronaldo:
  count_neg=count_neg+1
  print('Nice choice!')
  print(count_neg)
 #question 2
captain='captain'
striker='striker'
question2=input('Would you rather be a captain of a football team or a striker of a football team?')
if question2==captain:
    right_count=right_count+1
    print(right_count)
    print('You will be a very good leader')
if question2==striker:
    count_neg=count_neg+1
    print('Good luck on your shots!')
    print(count_neg)
#question 3
question3=int(input('Which number are you in the Kc football team?'))
if question3==3 or 7 or 10 or 6:
    right_count=right_count+1
    print('Congragulations you passed the test and you are Vandit. Happy Birthday!')
    print(let)
else:
    count_neg=count_neg+1
    print('You failed the test and you are not Vandit!')