0% found this document useful (0 votes)
23 views10 pages

Bubble Selection Sort

nothing

Uploaded by

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

Bubble Selection Sort

nothing

Uploaded by

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

1.

1 Sorting Techniques with Time Complexity:

1. Bubble sort Algorithm


Bubble sort works on the repeatedly swapping of adjacent elements until they are not in the
intended order. It is called bubble sort because the movement of array elements is just like
the movement of air bubbles in the water. Bubbles in water rise up to the surface; similarly,
the array elements in bubble sort move to the end in each iteration.

Bubble short is majorly used where

1.complexity does not matter


2.simple and short code is preferred
Algorithm

In the algorithm given below, suppose arr is an array of n elements. The


assumed swap function in the algorithm will swap the values of given array elements.

begin BubbleSort(arr)
for all array elements
if arr[i] > arr[i+1]
swap(arr[i], arr[i+1])
end if
end for
return arr
end BubbleSort
Bubble sort Algorithm
# Bubble sort in Python
Outer for loop i=0 to 4
def bubbleSort(array): first pass : i=0 ,
j=0,
# loop to access each array element
j=1, 4 comparisons
for i in range(len(array)):
j=2,
# loop to compare array elements j=3
for j in range(0, len(array) - i - 1): Second pass : i=1 ,
j=0,
# compare two adjacent elements j=1,
# change > to < to sort in descending order j=2, 3 comparisons
if array[j] > array[j + 1]:
Third pass : i=2 , j=0,
# swapping elements if elements
# are not in the intended order j=1, 2 comparisons
temp = array[j]
array[j] = array[j+1] Fourth pass : i=3 , j=0,
array[j+1] = temp 1 comparisons

data = [2, 45, 0, 11, 9] or data = array.array('i',[5,88,99,22]) Fifth pass : i=4 , j=0,
0 comparisons
bubbleSort(data)

print('Sorted Array in Ascending Order:')


print(data)
2.Selection Sort
In selection sort, the smallest value among the unsorted elements of the array is selected in every pass and
inserted to its appropriate position into the array. It is also the simplest algorithm. It is an in-place
comparison sorting algorithm. In this algorithm, the array is divided into two parts, first is sorted part, and
another one is the unsorted part. Initially, the sorted part of the array is empty, and unsorted part is the
given array. Sorted part is placed at the left, while the unsorted part is placed at the right.

The complexity of selection sort is O(n2), where n is the number of items. Due to this, it is not suitable for
large data sets.
Space complexity – O(1)

Selection sort is generally used when

1. A small array is to be sorted


2. Swapping cost doesn't matter
3. It is compulsory to check all elements
Algorithm

1.SELECTION SORT(arr, n)
2.
3.Step 1: Repeat Steps 2 and 3 for i = 0 to n-1
4.Step 2: CALL SMALLEST(arr, i, n, pos)
5.Step 3: SWAP arr[i] with arr[pos]
6.[END OF LOOP]
7.Step 4: EXIT
8.
9.SMALLEST (arr, i, n, pos)
10.Step 1: [INITIALIZE] SET SMALL = arr[i]
11.Step 2: [INITIALIZE] SET pos = i
12.Step 3: Repeat for j = i+1 to n
13.if (SMALL > arr[j])
14. SET SMALL = arr[j]
15.SET pos = j
16.[END OF if]
17.[END OF LOOP]
18.Step 4: RETURN pos
# Selection sort in Python
# time complexity O(n*n)
#sorting by finding min_index
def selectionSort(array, size):

for ind in range(size):


min_index = ind

for j in range(ind + 1, size):


# select the minimum element in every iteration
if array[j] < array[min_index]:
min_index = j
# swapping the elements to sort the array
(array[ind], array[min_index]) = (array[min_index], array[ind])

arr = [-2, 45, 0, 11, -9,88,-97,-202,747]


size = len(arr)
selectionSort(arr, size)
print('The array after sorting in Ascending Order by selection sort is:')
print(arr)
Working of Selection sort Algorithm

Now, let's see the working of the Selection sort Algorithm.


To understand the working of the Selection sort algorithm, let's take an unsorted array. It will
be easier to understand the Selection sort via an example.
Let the elements of array are -

Now, for the first position in the sorted array, the entire array is to be scanned sequentially.

At present, 12 is stored at the first position, after searching the entire array, it is found that 8 is the
smallest value.

So, swap 12 with 8. After the first iteration, 8 will appear at the first position in the sorted array.
For the second position, where 29 is stored presently, we again sequentially scan the rest of the
items of unsorted array. After scanning, we find that 12 is the second lowest element in the
array that should be appeared at second position.

Now, swap 29 with 12. After the second iteration, 12 will appear at the second position in
the sorted array. So, after two iterations, the two smallest values are placed at the
beginning in a sorted way.
The same process is applied to the rest of the array elements.
Now, we are showing a pictorial representation of the entire sorting process.

Now, the array is completely sorted

You might also like