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

JOC

The document provides an overview of various sorting algorithms including Selection Sort, Insertion Sort, Merge Sort, Quick Sort, and Bubble Sort, detailing their processes and algorithms. Each sorting method is explained with its time complexity, advantages, and sample code implementations. The document emphasizes the efficiency and application of these algorithms in sorting data.

Uploaded by

vaishnavig0704
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
4 views

JOC

The document provides an overview of various sorting algorithms including Selection Sort, Insertion Sort, Merge Sort, Quick Sort, and Bubble Sort, detailing their processes and algorithms. Each sorting method is explained with its time complexity, advantages, and sample code implementations. The document emphasizes the efficiency and application of these algorithms in sorting data.

Uploaded by

vaishnavig0704
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 17

Joy of Python using cloud computing

TOPIC : SEARCHING AND SORTING ALGORITHM

Vaishnavi Giradkar 7ETC19


Yeeyakshu Deo 7ETC14
Samruddhi Ingole 7ETC25
Shruti Kukde 7ETC38
Selection Sort
 WHAT IS 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.

Working of Selection Sort


• 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.
• In selection sort, the first smallest element is selected from the unsorted array and
placed at the first position. After that second smallest element is selected and placed in
the second position. The process continues until the array is entirely sorted.
• The average and worst-case 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.
 Algorithm
SELECTION SORT(arr, n)

Step 1: Repeat Steps 2 and 3 for i = 0 to n-1

Step 2: CALL SMALLEST(arr, i, n, pos)

Step 3: SWAP arr[i] with arr[pos]

[END OF LOOP]

Step 4: EXIT

SMALLEST (arr, i, n, pos)

Step 1: [INITIALIZE] SET SMALL = arr[i]

Step 2: [INITIALIZE] SET pos = i

Step 3: Repeat for j = i+1 to n

if (SMALL > arr[j])

SET SMALL = arr[j]

SET pos = j

[END OF if]

[END OF LOOP]

Step 4: RETURN pos


 Program for Selection Sort
Def Selection_Sort (MyList):
Output
i= Outer Loop Elements before Sorting
J= Inner Loop [12 , 34 , 2 , 7 , 45 , 90 , 89 , 9 , 1 ]
K= Index of the smallest Element Elements after Sorting
For i in range (len(MyList) – 1): [1 , 2 , 7 , 9 , 12 , 34 , 45 , 89 , 90]
k = i # ith element is assumed to be smallest
for j in range ( i + 1 , len (MyList)):
if (MyList [j] < MyList [k] ):
k=j
if (k ! = i):
temp = MyList [i]
MyList [i] = MyList [k]
MyList [k] = temp
MyList = [12, 34, 2, 7, 45 ,90, 89 , 9 ,1 ]
print(‘ Element before Sorting’)
Print(MyList)
Selection_Sort(MyList)
print(‘ Elements after sorting’)
print(MyList)
Insertion Sort
WHAT IS INSERTION SORT?
• Insertion sort works similar to the sorting of playing cards in hands. It is assumed that the first
card is already sorted in the card game, and then we select an unsorted card.
• If the selected unsorted card is greater than the first card, it will be placed at the right side;
otherwise, it will be placed at the left side. Similarly, all unsorted cards are taken and put in their
exact place.
• Time complexity of insertion sort in the average case and worst case is O(n2), where n is the
number of items. Insertion sort is less efficient than the other sorting algorithms like heap sort,
quick sort, merge sort, etc.
 Algorithm Insertion Sort

The simple steps of achieving the insertion sort are listed as follows -
Step 1 - If the element is the first element, assume that it is already sorted.
Return 1.
Step2 - Pick the next element, and store it separately in a key.
Step3 - Now, compare the key with all elements in the sorted array.
Step 4 - If the element in the sorted array is smaller than the current element,
then move to the next element. Else, shift greater elements in the array towards
the right.
Program of Insertion Sort
def insertionsort(arr):
for i in range(1 , len(arr)):
key = arr[i]
j = i-1

while j >= 0 and key < arr[j]:


arr[ j + 1 ] = arr[j]
j -= 1
arr[ j + 1 ] = key

def printArray(arr):
for i in range( len(arr)):
print ( arr [i] , end = “ “)
print()
If_name_==“_main_”:
arr = [ 12 ,11, 13, 5, 6 ]
insertionSort(arr)
printArray(arr)
Merge Sort
• Merge sort is similar to the quick sort algorithm as it uses the divide
and conquer approach to sort the elements.
• It is one of the most popular and efficient sorting algorithm. It divides
the given list into two equal halves, calls itself for the two halves and
then merges the two sorted halves. We have to define
the merge() function to perform the merging.
• The sub-lists are divided again and again into halves until the list
cannot be divided further.
• Then we combine the pair of one element lists into two-element lists,
sorting them in the process. The sorted two-element pairs is merged
into the four-element lists, and so on until we get the sorted list.
 Algorithm of Merge Sort
MERGE_SORT(arr, beg, end)

if beg < end


set mid = (beg + end)/2
MERGE_SORT(arr, beg, mid)
MERGE_SORT(arr, mid + 1, end)
MERGE (arr, beg, mid, end)
end of if

END MERGE_SORT
Program of Merge sort
def merge_sort(arr):
if len(arr) <= 1:
return arr
# Divide
mid = len(arr) // 2
left_half = merge_sort(arr[:mid])
right_half = merge_sort(arr[mid:])
# Merge
return merge(left_half, right_half)
def merge(left, right):
sorted_arr = []
i=j=0
# Compare elements and merge
while i < len(left) and j < len(right):
if left[i] < right[j]:
sorted_arr.append(left[i])
i += 1
else:
sorted_arr.append(right[j])
# Add remaining elements
sorted_arr.extend(left[i:])
sorted_arr.extend(right[j:])
return sorted_arr
# Example usage
arr = [38, 27, 43, 3, 9, 82, 10]
sorted_arr = merge_sort(arr)
print(sorted_arr)

Output : [3, 9, 10, 27, 38, 43, 82]


Quick Sort
• Sorting is a way of arranging items in a systematic manner.
• Quicksort is the widely used sorting algorithm that makes n log
n comparisons in average case for sorting an array of n
elements.
• It is a faster and highly efficient sorting algorithm.
• This algorithm follows the divide and conquer approach.
• Divide and conquer is a technique of breaking down the
algorithms into subproblems, then solving the subproblems, and
combining the results back together to solve the original
problem.
Algorithm of Quick sort
QUICKSORT (array A, start, end)
{
if (start < end)
{
p = partition(A, start, end)
QUICKSORT (A, start, p - 1)
QUICKSORT (A, p + 1, end)
}
}
Program of Quick sort
def quick_sort(arr):
if len(arr) <= 1:
return arr

# Choose a pivot (using the last element


here)
pivot = arr[-1]
left = []
right = []

# Partition the array


for i in range(len(arr) - 1):
if arr[i] <= pivot:
left.append(arr[i])
else:
right.append(arr[i])
# Recursively sort and combine
return quick_sort(left) + [pivot] +
quick_sort(right)
# Example usage
arr = [38, 27, 43, 3, 9, 82, 10]
sorted_arr = quick_sort(arr)
print(sorted_arr)
Bubble Sort
• 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.
• Although it is simple to use, it is primarily used as an
educational tool be
• cause the performance of bubble sort is poor in the real world. It
is not suitable for large data sets. The average and worst-case
complexity of Bubble sort is O(n2), where n is a number of
items.
Algorithm of Bubble sort

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
Program of Bubble Sort
def bubble_sort(arr):
n = len(arr)

# Traverse through all elements in the list


for i in range(n):
# Last i elements are already in place
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]

return arr

# Example usage
arr = [38, 27, 43, 3, 9, 82, 10]
sorted_arr = bubble_sort(arr)
print(sorted_arr)

Output : [3, 9, 10, 27, 38, 43, 82]

You might also like