0% found this document useful (0 votes)
4 views

UNIT III

The document discusses sorting and searching, defining sorting as the arrangement of data in a specific order, with real-life examples such as telephone directories and dictionaries. It provides a detailed explanation of the bubble sort algorithm, including a Python function to implement it and step-by-step iterations demonstrating how the sorting process works. The final output shows the sorted list from an initial unsorted list of integers.

Uploaded by

sdhivya2993
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
4 views

UNIT III

The document discusses sorting and searching, defining sorting as the arrangement of data in a specific order, with real-life examples such as telephone directories and dictionaries. It provides a detailed explanation of the bubble sort algorithm, including a Python function to implement it and step-by-step iterations demonstrating how the sorting process works. The final output shows the sorted list from an initial unsorted list of integers.

Uploaded by

sdhivya2993
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 5

UNIT III

SORTING AND SEARCHING

Sorting is defined as an arrangement of data in a certain order. Sorting techniques are used to
arrange data (mostly numerical) in an ascending or descending order.

Some of the real-life examples of sorting are:

Telephone Directory: It is a book that contains telephone numbers and addresses of people in
alphabetical order.

Dictionary: It is a huge collection of words along with their meanings in alphabetical order.

Contact List: It is a list of contact numbers of people in alphabetical order on a mobile phone.

BUBBLE SORT

compares two elements and swaps them until they are in the intended order

# Creating a bubble sort function

def bubble_sort(list1):

# Outer loop for traverse the entire list

for i in range(0,len(list1)-1):

for j in range(len(list1)-1):

if(list1[j]>list1[j+1]):

temp = list1[j]

list1[j] = list1[j+1]

list1[j+1] = temp
return list1

list1 = [5, 3, 8, 6, 7, 2]

print("The unsorted list is: ", list1)

# Calling the bubble sort function

print("The sorted list is: ", bubble_sort(list1))

OUTPUT

The unsorted list is: [5, 3, 8, 6, 7, 2]

The sorted list is: [2, 3, 5, 6, 7, 8]

Example -

We are creating a list of element, which stores the integer numbers

list1 = [5, 3, 8, 6, 7, 2]

First iteration

[5, 3, 8, 6, 7, 2]

It compares the first two elements and here 5>3 then swap with each other. Now we get new list
is -

[3, 5, 8, 6, 7, 2]

In second comparison, 5 < 8 then swapping happen -

[3, 5, 8, 6, 7, 2]

In third comparison, 8>6 then swap -

[3, 5, 6, 8, 7, 2]
In fourth comparison, 8>7 then swap -

[3, 5, 6, 7, 8, 2]

In fifth comparison, 8>2 then swap-

[3, 5, 6, 7, 2, 8]

Here the first iteration is complete and we get the largest element at the end. Now we need to
the len(list1) - 1

Second Iteration

[3, 5, 6, 7, 2, 8] - > [3, 5, 6, 7, 2, 8] here, 3<5 then no swap taken place

[3, 5, 6, 7, 2, 8] - > [3, 5, 6, 7, 2, 8] here, 5<6 then no swap taken place

[3, 5, 6, 7, 2, 8] - > [3, 5, 6, 7, 2, 8] here, 6<7 then no swap taken place

[3, 5, 6, 7, 2, 8] - > [3, 5, 6, 2, 7, 8] here 7>2 then swap their position. Now

[3, 5, 6, 2, 7, 8] - > [3, 5, 6, 2, 7, 8] here 7<8 then no swap taken place.

Third Iteration

[3, 5, 6, 2, 7, 8] - > [3, 5, 6, 7, 2, 8] here, 3<5 then no swap taken place

[3, 5, 6, 2, 7, 8] - > [3, 5, 6, 7, 2, 8] here, 5<6 then no swap taken place


[3, 5, 6, 2, 7, 8] - > [3, 5, 2, 6, 7, 8] here, 6<2 then swap their positions

[3, 5, 2, 6, 7, 8] - > [3, 5, 2, 6, 7, 8] here 6<7 then no swap taken place. Now

[3, 5, 2, 6, 7, 8] - > [3, 5, 2, 6, 7, 8] here 7<8 then swap their position.

It will iterate until the list is sorted.

Fourth Iteration -

[3, 5, 2, 6, 7, 8] - > [3, 5, 2, 6, 7, 8]

[3, 5, 2, 6, 7, 8] - > [3, 2, 5, 6, 7, 8]

[3, 2, 5, 6, 7, 8] - > [3, 2, 5, 6, 7, 8]

[3, 2, 5, 6, 7, 8] - > [3, 2, 5, 6, 7, 8]

[3, 2, 5, 6, 7, 8] - > [3, 2, 5, 6, 7, 8]

Fifth Iteration

[3, 2, 5, 6, 7, 8] - > [2, 3, 5, 6, 7, 8]


[2,45,0,11,9]

You might also like