PART-5
It means to arrange the number or items in a list in ascending or descending
order.
For Example: If there are unsorted number in a list as shown
below:
9 5 7 3 8 1
If we want the number to be sort in Ascending order means from lowest
number to highest number
1 3 5 7 8 9
If we want the number to be sort in Descending order means from Highest
number to lowest number
9 8 7 5 3 1
SELECTION SORT BUBBLE SORT INSERTION SORT
MERGE SORT QUICK SORT
BUBBLE SORT
Bubble Sort is used to arrange elements or items in a list in ascending or
descending order. It is the simplest sorting algorithm that works by repeatedly
swapping the adjacent elements if they are in wrong order.
How Bubble Sort Works?
Lets Understand the concept with the help of example:
0 1 2 3 4
14 5 8 3 -9no=
How Bubble Sort Works?
no=[14,5,8,3,-9]
Declare the list and initialize the list with number as shown above:
0 1 2 3 4
14 5 8 3 -9no=
So what is the First step
Next step is :
k=len(no)
Find the length of list in a variable k:
Next step is :
for x in range(0,k):
Now start the OUTER for loop from 0 index number to
k
0 1 2 3 4
14 5 8 3 -9no=
x k
OUTER LOOP
for y in range(0,k-x-1):INNER LOOP
INNER loop starts from 0 index number to k-x-1:
Now we check inside the inner loop that first number > second number
If (no[y] > no[y+1] ): if condition TRUE
for x in range(0,k):
for y in range(0,k-x-1):
If (no[y] > no[y+1] ):
tmp no[y]=
no[y] no[y+1]=
no[y+1] = tmp
0 1 2 3 4
14 5 8 3 -9
no=
Its an outer loop
inner loop
if condition TRUE
Swapping the values
tmp=14
no[0]=no[0+1]
no[0+1]=tmp
no[0]=5
no[1]=14
for x in range(0,k):
for y in range(0,k-x-1):
If (no[y] > no[y+1] ):
tmp no[y]=
no[y] no[y+1]=
no[y+1] = tmp
0 1 2 3 4
14 5 8 3 -9
no=
Its an outer loop
inner loop
Let’s Understand how its work
x=0 x=1 x=2 x=3 x=4
k-x-1
y=0
y=1
y=2
y=3
innerloop
5-0-1=4
k-x-1
y=0
y=1
y=2
5-1-1=3
k-x-1
y=0
y=1
5-2-1=2
k-x-1
y=0
5-3-1=1
Pass:1 Pass:2 Pass:3 Pass:4 Pass:5
Every time inner loop y starts from 0 to k-x-1)
k-x-1
5-4-1=0
y=?
PASS 1 Started
IT MEANS THAT OUTER LOOP VARIABLE X=0
OUTER LOOP X=0
INNER LOOP
y=0 y=1 y=2 y=3
k-x-1=5-0-1
Less than 4
Now Les Us understand
Bubble Sort Works using
Diagram.
A=[14,5,8,3,-9]
k=len(A)
for x in range(0,k):
print("Phase: ", x+1)
for y in range(0,k-x-1):
if A[y]>A[y+1]:
tmp=A[y]
A[y]=A[y+1]
A[y+1]=tmp
print(A)
print("Final Result: ",A)
0 1 2 3 4
14 5 8 3 -9
no=
x=0Pass:1
y=0
14 5 8 3 -9
145 8 3 -9
145 8 3 -9
145 8 3 -9
y=0 y+1=1
If(14>5): TRUE Values Swap
y=1
If(14>8): TRUE Values Swap
y=1 y+1=2
y=2 y+1=3
If(14>3): TRUE Values Swap
y=3 y+1=4
If(14>-9): TRUE Values Swap
y=2
y=3
145 8 3 -9
Pass 1 numbers are:
Less
than 4
145 8 3 -9
After the PASS 1 Completed
sortedUnsorted
PASS 2 Started
IT MEANS THAT OUTER LOOP VARIABLE X=1
OUTER LOOP X=1
INNER LOOP
y=0 y=1 y=2
k-x-1=5-1-1
Less than 3
Now Les Us understand
Bubble Sort Works using
Diagram.
A=[14,5,8,3,-9]
k=len(A)
for x in range(0,k):
print("Phase: ", x+1)
for y in range(0,k-x-1):
if A[y]>A[y+1]:
tmp=A[y]
A[y]=A[y+1]
A[y+1]=tmp
print(A)
print("Final Result: ",A)
0 1 2 3 4
5 8 3 -9 14
no=
x=1Pass:2
y=0
145 8 3 -9
145 8 3 -9
145 83 -9
y=0 y+1=1
If(5>8):
FALSE Values NOT Swapped
y=1
If(8>3): TRUE Values Swapped
y=1 y+1=2
y=2 y+1=3
If(8>-9): TRUE Values Swapped
y=2
Less
than 3
145 83 -9
Pass 2 numbers are:
145 83 -9
After the PASS 2 Completed
Unsorted sorted
PASS 3 Started
IT MEANS THAT OUTER LOOP VARIABLE X=2
OUTER LOOP X=2
INNER LOOP
y=0 y=1
k-x-1=5-2-1
Less than 2
Now Les Us understand
Bubble Sort Works using
Diagram.
A=[14,5,8,3,-9]
k=len(A)
for x in range(0,k):
print("Phase: ", x+1)
for y in range(0,k-x-1):
if A[y]>A[y+1]:
tmp=A[y]
A[y]=A[y+1]
A[y+1]=tmp
print(A)
print("Final Result: ",A)
0 1 2 3 4
5 3 -9 8 14
no=
x=2Pass:3
y=0
145 83 -9
145 83 -9
y=0 y+1=1
If(5>3): TRUE Values Swapped
y=1
If(5>-9): TRUE Values Swapped
y=1 y+1=2
Less
than 2
145 83 -9
Pass 3 numbers are:
145 83 -9
After the PASS 3 Completed
Unsorted sorted
PASS 4 Started
IT MEANS THAT OUTER LOOP VARIABLE X=3
OUTER LOOP X=3
INNER LOOP
y=0
k-x-1=5-3-1
Less than 1
Now Les Us understand
Bubble Sort Works using
Diagram.
A=[14,5,8,3,-9]
k=len(A)
for x in range(0,k):
print("Phase: ", x+1)
for y in range(0,k-x-1):
if A[y]>A[y+1]:
tmp=A[y]
A[y]=A[y+1]
A[y+1]=tmp
print(A)
print("Final Result: ",A)
0 1 2 3 4
3 -9 5 8 14
no=
x=3Pass:4
y=0
145 83 -9
y=0 y+1=1
If(3>-9): TRUE Values Swapped
Less
than 1
145 83-9
Pass 4 numbers are:
145 83-9
After the PASS 4 Completed
Unsorted sorted
PASS 5 Started
IT MEANS THAT OUTER LOOP VARIABLE X=4
OUTER LOOP X=4
INNER LOOP
y=?
k-x-1=5-4-1
Less than 0
So It means that inner loop no going to work
145 83-9
So finally numbers are sorted
bubble sort
sorted
Final code of bubble sort
A=[14,5,8,3,-9]
k=len(A)
for x in range(0,k):
print(“Pass: ", x+1)
for y in range(0,k-x-1):
if A[y]>A[y+1]:
tmp=A[y]
A[y]=A[y+1]
A[y+1]=tmp
print(A)
print("Final Result: ",A)
For loop with pass no’s also
Pass: 1
[5, 14, 8, 3, -9]
[5, 8, 14, 3, -9]
[5, 8, 3, 14, -9]
[5, 8, 3, -9, 14]
Pass: 2
[5, 8, 3, -9, 14]
[5, 3, 8, -9, 14]
[5, 3, -9, 8, 14]
Pass: 3
[3, 5, -9, 8, 14]
[3, -9, 5, 8, 14]
Pass: 4
[-9, 3, 5, 8, 14]
Pass: 5
Final Result: [-9, 3, 5, 8, 14]
--output----
A=[14,5,8,3,-9]
k=len(A)
for x in range(0,k):
for y in range(0,k-x-1):
if A[y]>A[y+1]:
tmp=A[y]
A[y]=A[y+1]
A[y+1]=tmp
print("Final Result: ",A)
Final code of bubble sort
For loop
--output----
Final Result: [-9, 3, 5, 8, 14]

LIST IN PYTHON[BUBBLE SORT]

  • 1.
  • 2.
    It means toarrange the number or items in a list in ascending or descending order. For Example: If there are unsorted number in a list as shown below: 9 5 7 3 8 1 If we want the number to be sort in Ascending order means from lowest number to highest number 1 3 5 7 8 9 If we want the number to be sort in Descending order means from Highest number to lowest number 9 8 7 5 3 1
  • 3.
    SELECTION SORT BUBBLESORT INSERTION SORT MERGE SORT QUICK SORT
  • 4.
    BUBBLE SORT Bubble Sortis used to arrange elements or items in a list in ascending or descending order. It is the simplest sorting algorithm that works by repeatedly swapping the adjacent elements if they are in wrong order. How Bubble Sort Works? Lets Understand the concept with the help of example: 0 1 2 3 4 14 5 8 3 -9no=
  • 5.
    How Bubble SortWorks? no=[14,5,8,3,-9] Declare the list and initialize the list with number as shown above: 0 1 2 3 4 14 5 8 3 -9no= So what is the First step Next step is : k=len(no) Find the length of list in a variable k:
  • 6.
    Next step is: for x in range(0,k): Now start the OUTER for loop from 0 index number to k 0 1 2 3 4 14 5 8 3 -9no= x k OUTER LOOP for y in range(0,k-x-1):INNER LOOP INNER loop starts from 0 index number to k-x-1: Now we check inside the inner loop that first number > second number If (no[y] > no[y+1] ): if condition TRUE
  • 7.
    for x inrange(0,k): for y in range(0,k-x-1): If (no[y] > no[y+1] ): tmp no[y]= no[y] no[y+1]= no[y+1] = tmp 0 1 2 3 4 14 5 8 3 -9 no= Its an outer loop inner loop if condition TRUE Swapping the values tmp=14 no[0]=no[0+1] no[0+1]=tmp no[0]=5 no[1]=14
  • 8.
    for x inrange(0,k): for y in range(0,k-x-1): If (no[y] > no[y+1] ): tmp no[y]= no[y] no[y+1]= no[y+1] = tmp 0 1 2 3 4 14 5 8 3 -9 no= Its an outer loop inner loop Let’s Understand how its work x=0 x=1 x=2 x=3 x=4 k-x-1 y=0 y=1 y=2 y=3 innerloop 5-0-1=4 k-x-1 y=0 y=1 y=2 5-1-1=3 k-x-1 y=0 y=1 5-2-1=2 k-x-1 y=0 5-3-1=1 Pass:1 Pass:2 Pass:3 Pass:4 Pass:5 Every time inner loop y starts from 0 to k-x-1) k-x-1 5-4-1=0 y=?
  • 9.
    PASS 1 Started ITMEANS THAT OUTER LOOP VARIABLE X=0 OUTER LOOP X=0 INNER LOOP y=0 y=1 y=2 y=3 k-x-1=5-0-1 Less than 4
  • 10.
    Now Les Usunderstand Bubble Sort Works using Diagram. A=[14,5,8,3,-9] k=len(A) for x in range(0,k): print("Phase: ", x+1) for y in range(0,k-x-1): if A[y]>A[y+1]: tmp=A[y] A[y]=A[y+1] A[y+1]=tmp print(A) print("Final Result: ",A) 0 1 2 3 4 14 5 8 3 -9 no= x=0Pass:1 y=0 14 5 8 3 -9 145 8 3 -9 145 8 3 -9 145 8 3 -9 y=0 y+1=1 If(14>5): TRUE Values Swap y=1 If(14>8): TRUE Values Swap y=1 y+1=2 y=2 y+1=3 If(14>3): TRUE Values Swap y=3 y+1=4 If(14>-9): TRUE Values Swap y=2 y=3 145 8 3 -9 Pass 1 numbers are: Less than 4
  • 11.
    145 8 3-9 After the PASS 1 Completed sortedUnsorted
  • 12.
    PASS 2 Started ITMEANS THAT OUTER LOOP VARIABLE X=1 OUTER LOOP X=1 INNER LOOP y=0 y=1 y=2 k-x-1=5-1-1 Less than 3
  • 13.
    Now Les Usunderstand Bubble Sort Works using Diagram. A=[14,5,8,3,-9] k=len(A) for x in range(0,k): print("Phase: ", x+1) for y in range(0,k-x-1): if A[y]>A[y+1]: tmp=A[y] A[y]=A[y+1] A[y+1]=tmp print(A) print("Final Result: ",A) 0 1 2 3 4 5 8 3 -9 14 no= x=1Pass:2 y=0 145 8 3 -9 145 8 3 -9 145 83 -9 y=0 y+1=1 If(5>8): FALSE Values NOT Swapped y=1 If(8>3): TRUE Values Swapped y=1 y+1=2 y=2 y+1=3 If(8>-9): TRUE Values Swapped y=2 Less than 3 145 83 -9 Pass 2 numbers are:
  • 14.
    145 83 -9 Afterthe PASS 2 Completed Unsorted sorted
  • 15.
    PASS 3 Started ITMEANS THAT OUTER LOOP VARIABLE X=2 OUTER LOOP X=2 INNER LOOP y=0 y=1 k-x-1=5-2-1 Less than 2
  • 16.
    Now Les Usunderstand Bubble Sort Works using Diagram. A=[14,5,8,3,-9] k=len(A) for x in range(0,k): print("Phase: ", x+1) for y in range(0,k-x-1): if A[y]>A[y+1]: tmp=A[y] A[y]=A[y+1] A[y+1]=tmp print(A) print("Final Result: ",A) 0 1 2 3 4 5 3 -9 8 14 no= x=2Pass:3 y=0 145 83 -9 145 83 -9 y=0 y+1=1 If(5>3): TRUE Values Swapped y=1 If(5>-9): TRUE Values Swapped y=1 y+1=2 Less than 2 145 83 -9 Pass 3 numbers are:
  • 17.
    145 83 -9 Afterthe PASS 3 Completed Unsorted sorted
  • 18.
    PASS 4 Started ITMEANS THAT OUTER LOOP VARIABLE X=3 OUTER LOOP X=3 INNER LOOP y=0 k-x-1=5-3-1 Less than 1
  • 19.
    Now Les Usunderstand Bubble Sort Works using Diagram. A=[14,5,8,3,-9] k=len(A) for x in range(0,k): print("Phase: ", x+1) for y in range(0,k-x-1): if A[y]>A[y+1]: tmp=A[y] A[y]=A[y+1] A[y+1]=tmp print(A) print("Final Result: ",A) 0 1 2 3 4 3 -9 5 8 14 no= x=3Pass:4 y=0 145 83 -9 y=0 y+1=1 If(3>-9): TRUE Values Swapped Less than 1 145 83-9 Pass 4 numbers are:
  • 20.
    145 83-9 After thePASS 4 Completed Unsorted sorted
  • 21.
    PASS 5 Started ITMEANS THAT OUTER LOOP VARIABLE X=4 OUTER LOOP X=4 INNER LOOP y=? k-x-1=5-4-1 Less than 0 So It means that inner loop no going to work
  • 22.
    145 83-9 So finallynumbers are sorted bubble sort sorted
  • 23.
    Final code ofbubble sort A=[14,5,8,3,-9] k=len(A) for x in range(0,k): print(“Pass: ", x+1) for y in range(0,k-x-1): if A[y]>A[y+1]: tmp=A[y] A[y]=A[y+1] A[y+1]=tmp print(A) print("Final Result: ",A) For loop with pass no’s also Pass: 1 [5, 14, 8, 3, -9] [5, 8, 14, 3, -9] [5, 8, 3, 14, -9] [5, 8, 3, -9, 14] Pass: 2 [5, 8, 3, -9, 14] [5, 3, 8, -9, 14] [5, 3, -9, 8, 14] Pass: 3 [3, 5, -9, 8, 14] [3, -9, 5, 8, 14] Pass: 4 [-9, 3, 5, 8, 14] Pass: 5 Final Result: [-9, 3, 5, 8, 14] --output----
  • 24.
    A=[14,5,8,3,-9] k=len(A) for x inrange(0,k): for y in range(0,k-x-1): if A[y]>A[y+1]: tmp=A[y] A[y]=A[y+1] A[y+1]=tmp print("Final Result: ",A) Final code of bubble sort For loop --output---- Final Result: [-9, 3, 5, 8, 14]