KEMBAR78
SORTING | PDF | Computer Programming | Algorithms And Data Structures
0% found this document useful (0 votes)
18 views11 pages

SORTING

The document describes three sorting algorithms: Bubble Sort, Selection Sort, and Insertion Sort. Each algorithm is explained with its process, advantages, and a code example demonstrating its implementation. The document emphasizes that Bubble Sort is inefficient for large datasets, while Selection and Insertion Sorts are simpler and more efficient methods for sorting elements.

Uploaded by

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

SORTING

The document describes three sorting algorithms: Bubble Sort, Selection Sort, and Insertion Sort. Each algorithm is explained with its process, advantages, and a code example demonstrating its implementation. The document emphasizes that Bubble Sort is inefficient for large datasets, while Selection and Insertion Sorts are simpler and more efficient methods for sorting elements.

Uploaded by

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

Bubble Sort Algorithm

Bubble Sort is the simplest sorting algorithm that works by repeatedly swapping the adjacent elements if
they are in the wrong order. This algorithm is not suitable for large data sets as its average and worst-case
time complexity is quite high.

In Bubble Sort algorithm,


•traverse from left and compare adjacent elements and the higher one is placed at right side.
•In this way, the largest element is moved to the rightmost end at first.
•This process is then continued to find the second largest and place it and so on until the data is
sorted.
Input: arr[ ] = [6, 0, 3, 5]
#Bubble Sort
arr=[3,8,5,2,1]
n = len(arr)
print(n)
for i in range(n): #traverse through all the
elements
# Last i elements are already sorted, no
need to check them
for j in range(0, n-i-1):

# Swap if the element found is greater


than the next element
if arr[j] > arr[j+1]:
arr[j], arr[j+1] = arr[j+1], arr[j]
print("arr1 :",arr)
print(arr)
Selection Sort Algorithm
Selection sort is a simple and efficient sorting algorithm that works by repeatedly selecting
the smallest (or largest) element from the unsorted portion of the list and moving it to the
sorted portion of the list.
3RD PASS
arr[ ] = [64, 25, 12, 22, 11]

1ST PASS

4TH PASS

2ND PASS
5TH PASS
Selection Sort Dry Run
#SELECTION SORT
num=[64,25,12,22,11]
print(num)
for i in range(len(num)):
# Find the minimum element in the remaining unsorted array
mini = i
for j in range(i+1, len(num)):
if num[j] < num[mini]:
mini = j
# Swap the found minimum element with the first element of the unsorted part
num[i], num[mini] = num[mini], num[i]
print(num)
Insertion Sort Algorithm
Insertion sort is a simple sorting algorithm that works by iteratively
inserting each element of an unsorted list into its correct position in a sorted
portion of the list.
Algorithm :
1.Start with the second element (index 1) of the array.
2.Compare this element to the one before it. If the previous element is
greater, swap the two elements.
3.Continue comparing the element to the ones before it and swapping
as needed until the element is in its correct sorted position.
4.Move to the next element (index 2) and repeat steps 2-3 until the
entire array is sorted.
Insertion Sort Algorithm(DRY RUN)
list1=[]
#Insertion Sort n=int(input("enter no of elements"))
i=0
while i<n:
list1+=[int(input("enter element"))]
i+=1
for i in range(1,n):
temp=list1[i]
j=i-1
while temp<list1[j] and j>=0:
list1[j+1]=list1[j]
j-=1
list1[j+1]=temp
print(list1)

You might also like